Spinner.setOnItemSelectedListener()和Spinner.setOnTouchListener()
注:写此博客的目的是为了避免以后可以翻查。
spinner_Unit_LJQ.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener(){
@Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
Log.d("mark", "onItemSelected() is invoked!");
Log.i("text",spinner_Unit_LJQ.getSelectedItem().toString());
if(spinner_Unit_LJQ.getSelectedItem().toString()=="C")
Output.setText("Tempreture Unit 'C' has been selected.");
else if(spinner_Unit_LJQ.getSelectedItem().toString()=="F")
Output.setText("Tempreture Unit 'F' has been selected.");
else if(spinner_Unit_LJQ.getSelectedItem().toString()=="K")
Output.setText("Tempreture Unit 'K' has been selected.");
}
@Override
public void onNothingSelected(AdapterView<?> parent){
}
});
spinner_Unit_LJQ.setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
Log.d("mark", "onTouch() is invoked!");
return false;
}
});
Notes:
1.Spinner把每一行数据看做一项,所以在获取每一行的时候,自然而然就是getSelectedItem方法了。
2.其次是setOnItemSelectedListener()和setOnTouchListener()方法:
onItemSelected()是在其中一项数据发生变化时,才会执行。eg.在Spinner中,重复选择一项数据的话,此函数不会执行。
onTouch()方法在每次点击该Spinner时,会执行两次。