我正在创建一个关注列表,例如this screen。
问题是,当我的活动开始时,它会自动选择位置零。我想限制微调器仅在用户单击列表中的任何项目时执行onclick
侦听器。当我的活动开始时,它会自动显示Toast。您选择“关闭电源”但我没有单击任何项目,因此默认情况下如何停止选择微调器位置。
public class SpinnerActivity extends Activity {
// declare three arrays for content of spinner
String[] strings = {"Power off","Restart"};
String[] subs = {"Shut phone down","Close all apps and restart phone" };
int arr_images[] = { R.drawable.pic1,
R.drawable.pic2};
Button button;
TextView textview;
Spinner spinner;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_spinner);
button = (Button)findViewById(R.id.button1);
textview = (TextView)findViewById(R.id.textView);
spinner = (Spinner)findViewById(R.id.spinner1);
spinner.setAdapter(new MyAdapter(this, R.layout.row, strings));
spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
public void onItemSelected(AdapterView<?> adapterView, View view, int i, long l) {
// TODO Auto-generated method stub
Toast.makeText(SpinnerActivity.this,"You Selected
:"+adapterView.getSelectedItem().toString(),Toast.LENGTH_SHORT).show();
}
// If no option selected
public void onNothingSelected(AdapterView<?> arg0) {
// TODO Auto-generated method stub
}
});
}
// button onclick
public void button_click(View view) {
textview.setText(spinner.getSelectedItem().toString());
}
// Adapter class for spinner control
public class MyAdapter extends ArrayAdapter<String>{
public MyAdapter(Context context, int textViewResourceId, String[] objects) {
super(context, textViewResourceId, objects);
}
@Override
public View getDropDownView(int position, View convertView,ViewGroup parent) {
return getCustomView(position, convertView, parent);
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
return getCustomView(position, convertView, parent);
}
public View getCustomView(int position, View convertView, ViewGroup parent) {
LayoutInflater inflater=getLayoutInflater();
View row=inflater.inflate(R.layout.row, parent, false);
TextView label=(TextView)row.findViewById(R.id.company);
label.setText(strings[position]);
TextView sub=(TextView)row.findViewById(R.id.sub);
sub.setText(subs[position]);
ImageView icon=(ImageView)row.findViewById(R.id.image);
icon.setImageResource(arr_images[position]);
return row;
}
}
}