为控件绑定监听器主要分为以下步骤:
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 } }