我希望在两个微调器值相同时显示一个toast,并将spinner1值恢复为默认值,与spinner two相同
这是我的java代码
package com.test16.sp2;
import android.app.Activity;
import android.os.Bundle;
import android.view.*;
import android.widget.*;
import android.widget.AdapterView.OnItemSelectedListener;
public class Test16sp2Activity extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Spinner spin1=(Spinner)findViewById(R.id.spinner1);
Spinner spin2=(Spinner)findViewById(R.id.spinner2);
ArrayAdapter<CharSequence> adapter1= ArrayAdapter.createFromResource(this, R.array.Planet_array1, android.R.layout.simple_spinner_item);
ArrayAdapter<CharSequence> adapter2= ArrayAdapter.createFromResource(this, R.array.Planet_array2, android.R.layout.simple_spinner_item);
adapter1.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
adapter2.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spin1.setAdapter(adapter1);
spin2.setAdapter(adapter2);
spin1.setOnItemSelectedListener(new MyOnItemSelectedListener());
spin2.setOnItemSelectedListener(new MyOnItemSelectedListener());
}
public class MyOnItemSelectedListener implements OnItemSelectedListener {
Spinner spin1=(Spinner)findViewById(R.id.spinner1);
Spinner spin2=(Spinner)findViewById(R.id.spinner2);
public void onItemSelected(AdapterView<?> parent,
View view, int pos, long id) {
if(spin1==spin2)
{
Toast.makeText(parent.getContext(), "your source and destination are same", Toast.LENGTH_LONG).show();
}
}
public void onNothingSelected(AdapterView<?> parent) {
// Do nothing.
}
}
}
休息所有文件与android开发人员的行星示例http://developer.android.com/resources/tutorials/views/hello-spinner.html
相同答案 0 :(得分:0)
您正在比较Spinners,而不是Spinner值。查看spin1
和spin2
变量的属性,您应该找到.getSelected()