当前位置: 移动技术网 > IT编程>开发语言>Java > 小白开发程序之路(2-6)复选框CheckBox(常用属性、自定义样式、监听事件)

小白开发程序之路(2-6)复选框CheckBox(常用属性、自定义样式、监听事件)

2020年07月23日  | 移动技术网IT编程  | 我要评论

实战

MainActivity.java

package com.example.myapplication;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;

public class MainActivity extends AppCompatActivity {
    private Button mbtnTextView;
    private Button mbtnButton;
    private  Button mbtnEditText;
    private Button mbtnRadioButton;
    private  Button mbtnCheckBox;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        mbtnTextView=findViewById(R.id.btn_textview);
        mbtnButton=findViewById(R.id.btn_button);
        mbtnEditText=findViewById(R.id.btn_edittext);
        mbtnRadioButton=findViewById(R.id.btn_radiobutton);
        mbtnCheckBox=findViewById(R.id.btn_checkbox);
        setListeners();//这个方法给每个按钮加上了点击事件onclick,而onclick的构造器能够根据调用他的对象的名字创建相应的点击事件
    }

    private void setListeners(){
        Onclick onclick=new Onclick();
        mbtnRadioButton.setOnClickListener(onclick);
        mbtnEditText.setOnClickListener(onclick);
        mbtnTextView.setOnClickListener(onclick);
        mbtnButton.setOnClickListener(onclick);
        mbtnCheckBox.setOnClickListener(onclick);
    }
    private class Onclick implements View.OnClickListener{
        @Override
        public void onClick(View view) {//onclick的构造器
            Intent intent=null;
            switch(view.getId()){
                case R.id.btn_textview:
                    //跳转至TextView演示界面
                    intent=new Intent(MainActivity.this,TextViewActivity.class);
                    break;
                case R.id.btn_button:
                    //跳转至Button演示界面
                    intent=new Intent(MainActivity.this,ButtonActivity.class);
                    break;
                case R.id.btn_edittext:
                    //跳转至EditText演示界面
                    intent=new Intent(MainActivity.this, EditTextActivity.class);
                    break;
                case R.id.btn_radiobutton:
                    //跳转至RadioButton演示界面
                    intent=new Intent(MainActivity.this,RadioButtonActivity.class);
                    break;
                case R.id.btn_checkbox:
                    //跳转至CheckBox演示界面
                    intent=new Intent(MainActivity.this,CheckBoxActivity.class);
                    break;
            }
            startActivity(intent);
        }
    }
}

activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity"
    android:orientation="vertical">
    <Button
        android:id="@+id/btn_textview"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="TextView"
        android:textAllCaps="false"/>
    <Button
        android:id="@+id/btn_button"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="Textbutton"
        android:textAllCaps="false"/>
    <Button
        android:id="@+id/btn_edittext"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="EditText"
        android:textAllCaps="false"/>
        <!--textAllCaps即内容全部大写    -->
    <Button
        android:id="@+id/btn_radiobutton"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="RadioButton"
        android:textAllCaps="false"/>
    <!--textAllCaps即内容全部大写    -->
    <Button
        android:id="@+id/btn_checkbox"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="CheckBox"
        android:textAllCaps="false"/>

</LinearLayout>

CheckBoxActivity.java

package com.example.myapplication;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.Toast;

public class CheckBoxActivity extends AppCompatActivity {
    private CheckBox mCb5,mCb6;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_check_box);
        mCb5=findViewById(R.id.cb_5);
        mCb6=findViewById(R.id.cb_6);
        mCb5.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton compoundButton, boolean b) {
                Toast.makeText(CheckBoxActivity.this,b?"5选中":"5未选中",Toast.LENGTH_LONG).show();
                //这里用了一个三元判断式,如果b是true就弹出“5选中”,如果b是false就弹出“5未选中”,注意视频中的变量名是isChecked,这里是b
            }
        });
        mCb6.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton compoundButton, boolean b) {
                Toast.makeText(CheckBoxActivity.this,b?"6选中":"6未选中",Toast.LENGTH_SHORT).show();
            }
        });
    }
}

activity_check_box.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent">
    <TextView
        android:id="@+id/tv_title"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="你会哪些移动开发"
        android:textSize="20sp"
        android:textColor="#337CD3"/>
    <CheckBox
        android:id="@+id/cb_1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Android"
        android:textSize="20sp"
        android:layout_below="@+id/tv_title"
        android:layout_marginTop="10dp"/>
    <CheckBox
        android:id="@+id/cb_2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Ios"
        android:textSize="20sp"
        android:layout_below="@+id/cb_1"
        android:layout_marginTop="10dp" />
    <CheckBox
        android:id="@+id/cb_3"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="H5"
        android:textSize="20sp"
        android:layout_below="@+id/cb_2"
        android:layout_marginTop="10dp"/>
    <CheckBox
        android:id="@+id/cb_4"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="else"
        android:textSize="20sp"
        android:layout_below="@+id/cb_3"
        android:layout_marginTop="10dp"/>
    <!--基础设置    -->
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical"
        android:layout_below="@id/cb_4">
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="你的兴趣:"
            android:textColor="#ee1122"
            android:textSize="20sp"/>
        <CheckBox
            android:id="@+id/cb_5"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="编程"
            android:textSize="20sp"
            android:layout_marginTop="10dp"
            android:button="@drawable/bg_checkbox"
            android:paddingLeft="10dp"/>
        <CheckBox
            android:id="@+id/cb_6"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="健身"
            android:textSize="20sp"
            android:paddingLeft="10dp"
            android:button="@drawable/bg_checkbox"/>
    </LinearLayout>
    <!--自定义图案,新建一个drawable文件,设置选中与未选中时的图片
        将button设为这个drawable文件,即设置按钮样式
        paddingLeft是Text与Button的距离
        drawableLeft可以在控件左侧插入一张指定图片-->
</RelativeLayout>

bg_checkbox.xml

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:state_checked="false" android:drawable="@drawable/icon_checkbox_false"/>
    <!--未选中时的图片    -->
    <item android:state_checked="true" android:drawable="@drawable/icon_checkbox_true"/>
    <!--选中时的图片-->
</selector>

效果

在这里插入图片描述

本文地址:https://blog.csdn.net/weixin_45941945/article/details/107521929

如对本文有疑问, 点击进行留言回复!!

相关文章:

验证码:
移动技术网