Answers:
先前的某些答案不正确。它们适用于其他小部件和视图,但是Spinner小部件的文档明确指出:
微调框不支持项目点击事件。调用此方法将引发异常。
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()
方法调用中。
onItemSelected(...)
不打。(我知道,因为我只是很难找到
Spinner spnLocale = (Spinner)findViewById(R.id.spnLocale);
spnLocale.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
public void onItemSelected(AdapterView<?> adapterView, View view, int i, long l) {
// Your code here
}
public void onNothingSelected(AdapterView<?> adapterView) {
return;
}
});
注意:请记住一件事。
Spinner OnItemSelectedListener
事件将执行两次:
尝试通过使用标志变量来区分两者。
您可以AdapterView.OnItemSelectedListener
在Activity中实现类。
然后使用下面的行 onCreate()
Spinner spin = (Spinner) findViewById(R.id.spinner);
spin.setOnItemSelectedListener(this);
然后重写这两种方法:
public void onItemSelected(AdapterView<?> parent, View v, int position, long id) {
selection.setText(items[position]);
}
public void onNothingSelected(AdapterView<?> parent) {
selection.setText("");
}
https://stackoverflow.com/q/1714426/811625
您可以通过简单的检查来避免调用OnItemSelectedListener():将当前选择索引存储在整数变量中,然后在执行任何操作之前在onItemSelected(..)中进行检查。
例如:
Spinner spnLocale;
spnLocale = (Spinner)findViewById(R.id.spnLocale);
int iCurrentSelection = spnLocale.getSelectedItemPosition();
spnLocale.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
public void onItemSelected(AdapterView<?> adapterView, View view, int i, long l) {
if (iCurrentSelection != i){
// Your code here
}
iCurrentSelection = i;
}
public void onNothingSelected(AdapterView<?> adapterView) {
return;
}
});
事业iCurrentSelection
应该是在对象范围内为这个工作!
iCurrentSelection
在此匿名类中声明了该变量,它将正常工作。您可以将其初始化为-1,以便在第一次调用时执行代码。
找到您的微调器名称并找到ID,然后实施此方法。
spinnername.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
}
});
您可以在onCreate或onStart中设置OnItemSelectedListener无关紧要-它仍将在Activity创建或启动期间分别调用。
因此我们可以在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;
}
});
spinner1.setOnItemSelectedListener(
new AdapterView.OnItemSelectedListener() {
//add some code here
}
);
为当前的微调框选择一个全局变量:
int currentItem = 0;
spinner_counter = (Spinner)findViewById(R.id.spinner_counter);
String[] value={"20","40","60","80","100","All"};
aa=new ArrayAdapter<String>(this,R.layout.spinner_item_profile,value);
aa.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinner_counter.setAdapter(aa);
spinner_counter.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
if(currentItem == position){
return; //do nothing
}
else
{
TextView spinner_item_text = (TextView) view;
//write your code here
}
currentItem = position;
}
@Override
public void onNothingSelected(AdapterView<?> parent) {
}
});
//R.layout.spinner_item_profile
<?xml version="1.0" encoding="utf-8"?>
<TextView android:id="@+id/spinner_item_text"
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@drawable/border_close_profile"
android:gravity="start"
android:textColor="@color/black"
android:paddingLeft="5dip"
android:paddingStart="5dip"
android:paddingTop="12dip"
android:paddingBottom="12dip"
/>
//drawable/border_close_profile
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item>
<shape android:shape="rectangle">
<solid android:color="#e2e3d7" />
</shape>
</item>
<item android:left="1dp"
android:right="1dp"
android:top="1dp"
android:bottom="1dp">
<shape android:shape="rectangle">
<solid android:color="@color/white_text" />
</shape>
</item>
</layer-list>
如果您想要一个真正的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;
}
}
对象是您的朋友,请使用它们。
spinner.setOnItemSelectedListener(
new AdapterView.OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> arg0, View arg1,
int arg2, long arg3) {
// TODO Auto-generated method stub
}
@Override
public void onNothingSelected(AdapterView<?> arg0) {
// TODO Auto-generated method stub
}
//add some code here
}
);
这将启动微调器和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
}
});