zoukankan      html  css  js  c++  java
  • Android:为控件绑定监听器

    为控件绑定监听器主要分为以下步骤:

    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
                
            }
            
        }
  • 相关阅读:
    用AVIFile函数制做AVI文件基本步骤
    RHEL5下源码安装Mysql
    RHEL 6.2/i686配置使用CentOS YUM源
    教你选择最稳定的 MySQL 版本
    RHEL 6.2/i686桌面版解决风扇狂转的问题 安装官方闭源ATI显卡驱动
    Ubuntu 11.10下解决JUK播放MP3乱码的方法
    Ubuntu 10.04下SVN+Apache安装、配置与使用
    Ubuntu 11.10安装(卸载)ATI闭源驱动导致黑屏进不了系统的解决办法
    ubuntu 11.10下创建eclipse桌面快捷方式
    Ubuntu 11.10与Windows双系统的硬盘安装方法
  • 原文地址:https://www.cnblogs.com/tinyphp/p/3780530.html
Copyright © 2011-2022 走看看