当所选项目发生变化时,如何为微调器设置事件侦听器?
基本上我想做的是类似这样的事情:
spinner1.onSelectionChange = handleSelectionChange;
void handleSelectionChange(Object sender){
//handle event
}
当所选项目发生变化时,如何为微调器设置事件侦听器?
基本上我想做的是类似这样的事情:
spinner1.onSelectionChange = handleSelectionChange;
void handleSelectionChange(Object sender){
//handle event
}
当前回答
为当前旋转器的选择取一个全局变量:
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>
其他回答
为当前旋转器的选择取一个全局变量:
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>
spinner1.setOnItemSelectedListener(
new AdapterView.OnItemSelectedListener() {
//add some code here
}
);
你在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;
}
});
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
}
);
如果你想要一个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;
}
}
对象是你的朋友,使用它们。