6.2按钮、单选按钮、多选按钮的监听点击事件


   上一篇文章介绍常用的控件,本篇介绍如何定义按钮的监听事件。

1.Button的三种监听事件

注:findViewById()方法用来实现在Activity与布局界面的控件实现关联,官方说法我也不清楚,个人喜欢用关联来阐述用法。

①设置onClick属性

实现要求:实现按钮onClick属性点击事件,按钮1被点击后,按钮上显示已被点击字样
定义顺序
  ①控件设置onClick属性
  ②Activity文件中定义同名方法
  ③在方法中编写交互代码

①创建按钮;设置onClick属性并为按钮设置id属性,两个属性值都是自定义,不能是纯数字和数字开头

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <Button
        android:id="@+id/But"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="按钮1"
        android:textSize="30sp"
        android:onClick="Click1"/>

</LinearLayout>

②在java/第一个目录/MainActivity文件中定义交互代码;创建同名的onClick属性值的方法Click1

    public void Click1(View view){

    }

如图:
在这里插入图片描述

③定义Button组件,使用findViewById

Button button = findViewById(R.id.But);

  有java基础的可能会知道为什么代码和图片上的定义方式有点差距:上面代码的定义方式适合Activity中一个方法调用按钮时,可以这样使用,不存在全局变量问题
  在Activity中如果存在多种方法需要调用变量时,建议下面定义方式,可以全局调用,也可以选中对象使用Android Studio快捷键Ctrl + Alt + F将对象设置为全局变量 。
  例如:光标选中button,同时点击Ctrl + Alt + F,会出现提示,继续点回车,就会设置为全部变量。
在这里插入图片描述

④在方法中定义按钮button调用setText()方法指定字样

    public void Click1(View view){
        button.setText("已被点击");
    }

如图:
在这里插入图片描述

⑤运行结果:
在这里插入图片描述在这里插入图片描述

②匿名内部类

实现要求:实现按钮setOnClickListener()方法点击事件,按钮2被点击后,按钮上显示已被点击字样
定义顺序
  ①button按钮设置id属性
  ②Activity中findViewById()方法找到按钮
  ③定义setOnClickListener()监听方法
  ④编写交互代码


①定义如下布局;一定要定义id属性

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <Button
        android:id="@+id/But"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="按钮2"
        android:textSize="30sp" />

</LinearLayout>

②在Activity中定义findViewById()方法找到按钮

Button button=findViewById(R.id.But);

如图:
在这里插入图片描述

③使用setOnClickListener()方法设置监听事件;并在匿名方法中定义交互代码

    button.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            button.setText("已被点击");
        }
    });

如图:
在这里插入图片描述

④运行结果:
在这里插入图片描述在这里插入图片描述

③继承OnClickListener接口

实现要求:实现当前类继承onClickListener接口点击事件,3个按钮被点击后,按钮上分别显示已被点击字样
定义顺序
  ①定义布局界面
  ②findViedById()方法关联按钮
  ③当前类实现onClickListener接口,创建关联的onClick()方法
  ④设置按钮setOnClickListener()方法指定当前类
  ⑤在onClick()方法中编译交互代码

①创建多个按钮组件;定义id属性

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">

    <Button
        android:id="@+id/But1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="按钮1"
        android:textSize="30sp" />

    <Button
        android:id="@+id/But2"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="按钮2"
        android:textSize="30sp" />

    <Button
        android:id="@+id/But3"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="按钮3"
        android:textSize="30sp" />
</LinearLayout>

②在Activity中使用findViewById()方法

	Button button1 = findViewById(R.id.But1);
    Button button2 = findViewById(R.id.But2);
    Button button3 = findViewById(R.id.But3);

如图:
在这里插入图片描述

③指定当前类实现OnClickListener接口
在这里插入图片描述
红色波浪线是没有实现方法;将 光标放到红色波浪线上Alt + Enter(回车键),点击Implement methods,都会出现如图:
在这里插入图片描述
选择第一个,出现如图方法就可以操作下一步:
在这里插入图片描述

④指定当前对象的setOnClickListener()属性为当前类

    button1.setOnClickListener(this);
    button2.setOnClickListener(this);
    button3.setOnClickListener(this);

在这里插入图片描述

⑤在onClick()方法中定义交互代码;多个按钮,适合Java中的Switch判断语句

    public void onClick(View v) {
        switch (v.getId()){
            case R.id.But1:
                button1.setText("已被点击");
                break;
            case R.id.But2:
                button2.setText("已被点击");
                break;
            case R.id.But3:
                button3.setText("已被点击");
                break;
        }
    }

如图:注意在switch中的case R.id.xxx:xxx是布局文件中按钮的id属性,不是当前Activity中自定义的button1,button2
在这里插入图片描述

⑥运行结果:
在这里插入图片描述在这里插入图片描述

2.RadioButton的监听事件

实现要求:选中单选按钮,实现显示性别
定义顺序
  ①定义布局界面
  ②Activity界面使用findViewById()方法关联控件
  ③使用setOnCheckedChangeListener()方法实现匿名内部类
  ④在方法中编写if语句判断性别

设置setOnCheckedChangeListener()方法通过匿名内部类实现监听事件:
①定义布局,每个控件都要设置id属性

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">

    <RadioGroup
        android:id="@+id/SEX"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal">

        <RadioButton
            android:id="@+id/BOY"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="男" />

        <RadioButton
            android:id="@+id/GIRL"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="女" />
    </RadioGroup>

    <TextView
        android:id="@+id/TEXTVIEW"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:paddingLeft="5dp"/>
</LinearLayout>

②在Activity中使用findViewById()方法关联控件

package com.example.radiobutton;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.provider.MediaStore;
import android.widget.Button;
import android.widget.RadioGroup;
import android.widget.TextView;

public class MainActivity extends AppCompatActivity {
    RadioGroup radioGroup;
    TextView textView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.linearlayout);

        radioGroup = findViewById(R.id.SEX);
        textView = findViewById(R.id.TEXTVIEW);
        
    }
}

③使用setOnCheckedChangeListener()方法设置匿名点击事件

radioGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
    @Override
    public void onCheckedChanged(RadioGroup group, int checkedId) {

    }
});

在这里插入图片描述

④在方法中使用if语句判断;isChecked()方法是否被点击

radioGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
    @Override
    public void onCheckedChanged(RadioGroup group, int checkedId) {
        if (checkedId == R.id.BOY){ //如果“男”被选中
            textView.setText("性别:男"); //文本框显示
        }else{ //不是男那就是女
            textView.setText("性别:女");
        }
    }
});

在这里插入图片描述

⑤运行结果:
在这里插入图片描述在这里插入图片描述在这里插入图片描述

3.CheckBox监听事件

实现要求:选中多选按钮,实现显示爱好
定义顺序
  ①定义布局界面
  ②Activity界面使用findViewById()方法关联控件
  ③使用CompoundButton.onCheckedChangeListener实现接口;创建onCheckedChanged监听方法
  ④使用setOnCheckedChangeListener(this)方法指定当前类
  ⑤在onCheckedChanged方法中编译判断代码
  ⑥指定按钮监听事件
  ⑦在按钮监听事件中编译实现结果代码

通过定义接口实现监听事件:
①定义布局
布局管理器也持支嵌套关系,不必要的情况下尽量不要超过2层或2层以上,繁琐麻烦。

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content">

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="选择类型:" />

        <CheckBox
            android:id="@+id/movie1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="科幻" />

        <CheckBox
            android:id="@+id/movie2"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="恐怖" />

        <CheckBox
            android:id="@+id/movie3"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="喜剧" />
    </LinearLayout>

    <Button
        android:id="@+id/BUTTON"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="提交" />

    <TextView
        android:id="@+id/TEXTVIEW"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />
</LinearLayout>

效果如图:
在这里插入图片描述

②Activity中定义、关联控件

package com.example.checkbox;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.TextView;

public class MainActivity extends AppCompatActivity{
    CheckBox m1, m2, m3; //复选框组件
    Button but; //提交按钮
    TextView re; //结果显示框
    String hobbys = ""; //字符串;存储爱好

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.linearlayout);

        m1 = findViewById(R.id.movie1);
        m2 = findViewById(R.id.movie2);
        m3 = findViewById(R.id.movie3);

        but = findViewById(R.id.BUTTON);
        re = findViewById(R.id.TEXTVIEW);
    }
}

在这里插入图片描述

③定义接口;实现onCheckedChanged方法

public class MainActivity extends AppCompatActivity
                implements CompoundButton.OnCheckedChangeListener{
    CheckBox m1, m2, m3; //复选框组件
    Button but; //提交按钮
    TextView re; //结果显示框
    String hobbys = ""; //字符串;存储爱好

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.linearlayout);

        m1 = findViewById(R.id.movie1);
        m2 = findViewById(R.id.movie2);
        m3 = findViewById(R.id.movie3);

        but = findViewById(R.id.BUTTON);
        re = findViewById(R.id.TEXTVIEW);
    }

    @Override
    public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {

    }
}

在这里插入图片描述

④实现当前监听按钮的setOnCheckedChangeListener()方法指向当前类

	 m1.setOnCheckedChangeListener(this);
	 m2.setOnCheckedChangeListener(this);
	 m3.setOnCheckedChangeListener(this);

在这里插入图片描述

⑤在监听方法内编译代码
contains()方法判断是字符串是否包含某个字符串,返回类型是boolean

   @Override
   public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
       String str = buttonView.getText().toString(); //监听的复选按钮转换为字符串
       if (isChecked == true){ //如果爱好类型被选中
           if (!hobbys.contains(str)){ //但是存储爱好里面没有包含选中的按钮字符串
               hobbys = hobbys + " " +str; //将按钮字符串添加到爱好里面
           }
       }else { //如果爱好类型没有被选中
           if (hobbys.contains(str)){ //但是存储爱好里面却包含着按钮字符串
               hobbys = ""; //清空
               re.setText(""); //将显示结果清空
           }
       }
   }

在这里插入图片描述

⑥设置按钮的匿名内部类监听事件

  but.setOnClickListener(new View.OnClickListener() {
      @Override
      public void onClick(View v) {
          
      }
  });

在这里插入图片描述

⑦在按钮监听事件中编译结果代码

  but.setOnClickListener(new View.OnClickListener() {
      @Override
      public void onClick(View v) {
          re.setText(hobbys);
      }
  });

在这里插入图片描述

⑧运行结果:
在这里插入图片描述在这里插入图片描述在这里插入图片描述

  • 3
    点赞
  • 18
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值