当所选项目发生变化时,如何为微调器设置事件侦听器?
基本上我想做的是类似这样的事情:
spinner1.onSelectionChange = handleSelectionChange;
void handleSelectionChange(Object sender){
//handle event
}
当所选项目发生变化时,如何为微调器设置事件侦听器?
基本上我想做的是类似这样的事情:
spinner1.onSelectionChange = handleSelectionChange;
void handleSelectionChange(Object sender){
//handle event
}
当前回答
你在onCreate或onStart中设置OnItemSelectedListener并不重要——它仍然会在活动创建或启动期间被调用(分别)。 所以我们可以在onCreate中设置它(而不是在onStart中!) 只需添加一个标志来计算第一次初始化:
private Spinner mSpinner;
private boolean mSpinnerInitialized;
然后在onCreate(或onCreateView)中只需:
mSpinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
public void onItemSelected(AdapterView<?> adapterView, View view, int i, long l) {
if (!mSpinnerInitialized) {
mSpinnerInitialized = true;
return;
}
// do stuff
}
public void onNothingSelected(AdapterView<?> adapterView) {
return;
}
});
其他回答
这是可行的 初始化旋转器和findviewbyid并使用它将工作
Spinner schemeStatusSpinner;
schemeStatusSpinner = (Spinner) dialog.findViewById(R.id.spinner);
schemeStatusSpinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> parentView, View selectedItemView, int position, long id) {
// your code here
if(schemeStatusSpinner.getSelectedItemId()==4){
reasonll.setVisibility(View.VISIBLE);
}
else {
reasonll.setVisibility(View.GONE);
}
}
@Override
public void onNothingSelected(AdapterView<?> parentView) {
// your code here
}
});
spinner1.setOnItemSelectedListener(
new AdapterView.OnItemSelectedListener() {
//add some code here
}
);
对于kotlin,您可以使用:
spinner.onItemSelectedListener = object : AdapterView.OnItemSelectedListener {
override fun onItemSelected(parent: AdapterView<*>?, view: View?, position: Int, id: Long) {
}
override fun onNothingSelected(p0: AdapterView<*>?) {
}
}
注意:对于onItemSelected方法的参数,我使用自定义变量名
前面的一些答案是不正确的。它们适用于其他小部件和视图,但Spinner小部件的文档明确指出:
旋转器不支持项目单击 事件。调用此方法将引发 一个例外。
最好使用OnItemSelectedListener()代替:
spinner.setOnItemSelectedListener(new OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> parentView, View selectedItemView, int position, long id) {
// your code here
}
@Override
public void onNothingSelected(AdapterView<?> parentView) {
// your code here
}
});
这对我很有用。
注意,onItemSelected方法也被调用时,视图正在构建,所以你可以考虑把它放在onCreate()方法调用。
如果你想要一个true onChangedListener()。将初始值存储在处理程序中,并检查它是否已更改。它很简单,不需要全局变量。如果页面上有多个转轮,则有效。
String initialValue = // get from Database or your object
mySpinner.setOnItemSelectedListener(new SpinnerSelectedListener(initialValue));
...
protected class SpinnerSelectedListener implements AdapterView.OnItemSelectedListener {
private SpinnerSelectedListener() {
super();
}
public SpinnerSelectedListener(String initialValue) {
this();
this.initialValue = initialValue;
}
private String initialValue;
// getter and setter removed.
@Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
final String newValue = (String) spinHeight.getItemAtPosition(position);
if (newValue.equals(initialValue) == false) {
// Add your code here. The spinner has changed value.
// Maybe useful.
// initialValue = newValue;
}
}
@Override
public void onNothingSelected(AdapterView<?> parent) {
// Maybe useful.
// initialValue = null;
}
}
对象是你的朋友,使用它们。