美文网首页
【Android】Button控件笔记

【Android】Button控件笔记

作者: 感同身受_ | 来源:发表于2019-05-17 22:19 被阅读0次

    二、 Button:

    1. id(注册ID):
    2. text(按钮名称):
    3. textAllCaps(切换大小写):
    4. margin(边距):指定控件在上下左右方向上的偏移距离
      layout_marginStart和layout_marginLeft的作用是一样的
      layout_marginEnd和layout_marginRight的作用是一样的
      【注】layout_marginTop、layout_marginLeft单独指定控件在某方向上偏移的距离

    如按钮边框和屏幕上边界的距离:
    android:layout_marginTop="5dp"

    Button按钮设置点击的监听方式

    1. 使用匿名内部类的形式进行设置。

    Button button1 = findViewById(R.id.button_1);
    button1.setOnClickListener(new View.OnClickListener(){
        @Override
        public void onClick(View v){
        }
    });
    

    2. 在XML文件中定义onClick属性,属性中对应的值需要在java代码中编写对应的方法名

    【注】参数中的View必须存在,方法类似于 public void XXX(View v)

    3. Activity实现onClickListener

    在给Button按钮设置点击的监听事件的时候,直接让当前的Activity实现onClickListener接口,这样传入的监听对象就可以直接使用当前的Activity.this

    4. 让其他的类实现onClickListener接口

    
    public class FirstActivity extends BaseActivity {
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.first_layout);
            //找到控件的id,也叫给控件初始化
            Button button2 = findViewById(R.id.button_0);
            MyListener listener = new MyListener();
            //给Button按钮设置点击的监听
            button2.setOnClickListener(listener);
        }
        //定义一个类来实现onClickListener接口
        class MyListener implements View.OnClickListener{
            //参数v:就是触发点击的View控件。在这里就被点击的Button按钮
            @Override
            public void onClick(View v){
                switch(v.getId()){
                    case R.id.button_0:
                        Log.d("FirstActivity","点击事件0完成");
                        break;
                    case R.id.button_1:
                        Log.d("FirstActivity","点击事件1完成");
                        break;
                    default:
                        break;
                }
        }
    }
    

    CheckBox用来进行多选的按钮

    【注】:

    1. CheckBox是选择框,只有选中或未选中两种状态,一般会使用在多个选项都可以选择的情况下。例如:选择自身爱好。
    2. 使用CheckBox一般会用在复选操作上。表示可以有多个选项提供进行勾选。

    RadioButton单选框

    1. 如果需要让几个单选按钮之间存在互斥的效果,那么需要将这些按钮定义在一个RadioGroup当中(可实现界面滑动小圆点框)
    2. OnCheckedChangeListener是定义在CompoundButton下的监听对象,因为CheckBox是CompoundButton子类,所以可以直接使用。
    RadioButton rb = findViewById(R.id.btn_nan);
    rb.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
        @Override
        public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
            
        }
    });
    

    【注】

    1. CompoundButton是RadioButton的父类,也是CheckBox的父类,RadioButton、CheckBox自身可以设置onCheckChangeListener(RadioButton、CheckBox是调用的CompounButton中的OnCheckedChangeListener)
    2. OnCheckedChangeListener是定义在CompoundButton当中的监听,所以CheckBox和RadioButton都可以进行调用
    3. RadioGroup中也定义OnCheckChangeListener对象。所以直接给RadioButton设置监听。(RadioGroup是调用的RadioGroup中的OnCheckChangeListener)
    4. 区分:虽然都是调用的OnCheckChangeListener方法,但是OnCheckChangeListener所属的类不同。

    相关文章

      网友评论

          本文标题:【Android】Button控件笔记

          本文链接:https://www.haomeiwen.com/subject/ckvaaqtx.html