How can I know if an item in spinner has been selected in android??
可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试):
问题:
回答1:
See here: http://developer.android.com/resources/tutorials/views/hello-spinner.html
You have to implement an OnItemSelectedListener
.
Here the example from the link:
public class MyOnItemSelectedListener implements OnItemSelectedListener {
public void onItemSelected(AdapterView<?> parent,
View view, int pos, long id) {
Toast.makeText(parent.getContext()), "The planet is " +
parent.getItemAtPosition(pos).toString(), Toast.LENGTH_LONG).show();
}
public void onNothingSelected(AdapterView parent) {
// Do nothing.
}
}
spinner.setOnItemSelectedListener(new MyOnItemSelectedListener());
You could also use an anonymous inner method if you don't want to explicitly declare a class for your listener.
回答2:
for the spinner, you need to implement OnItemSelectedListener.