私はeditTextを持っており、スピナーを開くためにsetOnFocusChangeListener
を設定しました。 スピナーから項目を選択すると、選択した項目が同じ編集テキストに表示されます。editTextFocusChangeのテキストを編集するためにスピナーの選択項目を設定する方法 - Android
以下は私が試したコードです。
List<String> tal = new ArrayList<String>();
tal.add("1");
tal.add("2");
tal.add("3");
tal.add("4");
tal.add("5");
ArrayAdapter<String> dataAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, tal);
dataAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinner.setAdapter(dataAdapter);
editText.setOnFocusChangeListener(new View.OnFocusChangeListener() {
@Override
public void onFocusChange(View arg0, boolean hasFocus) {
// TODO Auto-generated method stub
if(hasFocus){
spinner.performClick();
}
}
});
editText.setText(spinner.getSelectedItem().toString()); //this is taking the first value of the spinner by default.
のXml:
<EditText
android:hint="Select A Value"
android:id="@+id/editText"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@drawable/rounded_edittext"
android:layout_weight="0.05"
android:singleLine="true"/>
<Spinner android:visibility="gone"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/spinner" />
[AndroidでonItemSelectedを使用するにはどうすればいいですか?](http://stackoverflow.com/questions/20151414/how-can-i-use-onitemselected-in-android) –