我有一个功能,当单击一个按钮时,某个变量将根据微调器的选择输入一个edittext的内容。它运作良好。
我正在尝试使用onSelectedItem来填充edittext,而不必单击按钮但它不能正常工作
Spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
String selection = String.valueOf(Spinner.getSelectedItem());
if (selection.equals("test")) {
editText3.setText("" + testvalue + "", TextView.BufferType.EDITABLE);
} else if (selection.equals("dog")) {
editText3.setText("" + dogvalue + "", TextView.BufferType.EDITABLE);
} else if (selection.equals("cat")) {
editText3.setText("" + catvalue + "", TextView.BufferType.EDITABLE);
}
}
@Override
public void onNothingSelected(AdapterView<?> parent) {
}
});
}
答案 0 :(得分:0)
您确定要将对象用于您的小部件Spinner吗?尝试将其更改为:
Spinner spinner=findViewById(R.id.spinner);
spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
String selection = spinner.getSelectedItem().toString();
if (selection.equals("test")) {
editText3.setText("" + testvalue + "", TextView.BufferType.EDITABLE);
} else if (selection.equals("dog")) {
editText3.setText("" + dogvalue + "", TextView.BufferType.EDITABLE);
} else if (selection.equals("cat")) {
editText3.setText("" + catvalue + "", TextView.BufferType.EDITABLE);
}
}
@Override
public void onNothingSelected(AdapterView<?> parent) {
}
});
}