為控件綁定監聽器主要分為以下步驟:
1、獲取代表控件的對象
2、定義一個類,實現監聽器接口
3、生成監聽器對象
4、為控件綁定監聽器對象
實例:Button按鈕----監聽器OnClickListener
<TextView android:id="@+id/textView" android:layout_width="match_parent" android:layout_height="wrap_content" android:textColor="#ff0000" android:text="I am one" /> <Button android:id="@+id/button" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="button"/>
....
protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); textView =(TextView)findViewById(R.id.textView); textView.setText("set text success"); //獲取代表控件的對象 button =(Button)findViewById(R.id.button); //生成監聽器對象 ButtonListener buttonListener =new ButtonListener(); //為控件綁定監聽器對象 button.setOnClickListener(buttonListener); } //定義監聽類,實現監聽器接口 class ButtonListener implements OnClickListener{ @Override public void onClick(View arg0) { // TODO Auto-generated method stub textView.setText("onclick ok"); } }
實例: CheckBox復選框----監聽器OnCheckedChangeListener
<CheckBox android:id="@+id/eat" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="吃飯" />
....
protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.linelayout); //獲取代表控件的對象 eatbox = (CheckBox)findViewById(R.id.eat); //生成監聽器對象 CheckBoxListener checkBoxListener = new CheckBoxListener(); //為控件綁定監聽器對象 eatbox.setOnCheckedChangeListener(checkBoxListener); }
//定義監聽類,實現監聽器接口 class CheckBoxListener implements OnCheckedChangeListener{ @Override public void onCheckedChanged(CompoundButton arg0, boolean arg1) { //第一參數為CheckBox對象,第二參數為是否選中 if(arg0.getId()==R.id.eat){ System.out.println("eat"); } if(arg1){ System.out.println("oncheck"); } } }
實例:Radio 單選按鈕 ----監聽器OnCheckedChangeListener
<RadioGroup android:id="@+id/sex" android:layout_width="wrap_content" android:layout_height="wrap_content" android:orientation="horizontal" > <RadioButton android:id="@+id/femal" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="女" /> <RadioButton android:id="@+id/mal" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="男" /> </RadioGroup>
...
private RadioGroup radioGroup; private RadioButton radiofemal; private RadioButton radiomal; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); //獲取代表控件的對象 radioGroup = (RadioGroup)findViewById(R.id.radioGroup); radiofemal = (RadioButton)findViewById(R.id.radiofemal); radiomal = (RadioButton)findViewById(R.id.radiomal); //生成監聽器對象 RadioGroupListener listener = new RadioGroupListener(); //為控件綁定監聽器對象 radioGroup.setOnCheckedChangeListener(listener); } //定義監聽類,實現監聽器接口 class RadioGroupListener implements OnCheckedChangeListener{ @Override public void onCheckedChanged(RadioGroup arg0, int arg1) { // 參數一:哪個radiogroup,第二個哪個radiobutton if(arg1 == radiofemal.getId()){ System.out.println("選擇了femal"); }else{ System.out.println("選擇了mal"); } } }
OnCheckedChangeListener在兩個包內都有,使用的時候需要注意下,
android.widget.RadioGroup.OnCheckedChangeListener;
class RadioGroupListener implements OnCheckedChangeListener{ @Override public void onCheckedChanged(RadioGroup arg0, int arg1) { // TODO Auto-generated method stub } }
android.widget.CompoundButton.OnCheckedChangeListener;
class RadioButtonListener implements android.widget.CompoundButton.OnCheckedChangeListener{ @Override public void onCheckedChanged(CompoundButton arg0, boolean arg1) { // TODO Auto-generated method stub } }