Android Studio | ImageButton(图标按钮)ToggleButton(切换按钮) Switch(滑动切换按钮) RadioButton(单选按钮)的案例(附可运行的demo)


按钮合集案例

 

本文主要展现了Android Studio 以下的几类按钮:

  • 普通按钮
  • ImageButton(图标按钮)
  • ToggleButton(切换按钮)
  • Switch(滑动切换按钮)
  • RadioButton(单选按钮)

  的使用定义和三种不同的事件触发调用方法。

 

以下是阿里云盘的源代码demo链接,有需要实际运行的同学可自行下载运行调试。

        点击跳转下载

 

demo演示视频:

https://www.bilibili.com/video/BV1P34y1m72R?share_source=copy_web

 

代码部分示例:

xml代码

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context=".ButttonStyle">
<Button
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="hello world">

</Button>
    <ImageButton
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:background="#FDFDFD"
        android:src="@drawable/bilibili">
//图标按钮
    </ImageButton>

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="一键三连"
        android:drawableLeft="@drawable/bilibili">
//带图标的文字按钮
    </Button>
    <ToggleButton

        android:id="@+id/btn_toggle"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textOff="关"
        android:textOn="开"
        android:drawableLeft="@drawable/bilibili"
        android:onClick="showMSG"
        >
        //按钮开关
    </ToggleButton>
    <Switch
        android:id="@+id/btn_switch"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textOn="开"
        android:textOff="关"
        android:onClick="showMSG_switch"
        >
        //滑动按钮开关
    </Switch>

    <EditText
        android:id="@+id/edt"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:inputType="text"
        android:text="请输入:"
        android:textSize="20sp"
        >
    </EditText>

    <RadioGroup
        android:id="@+id/sex"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content">
        <RadioButton
            android:id="@+id/boy"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/boy">
        </RadioButton>
//单选按钮
        <RadioButton
            android:id="@+id/girl"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/girl">
        </RadioButton>

    </RadioGroup>
    <Button
        android:id="@+id/btn_queding"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="确定"
        android:layout_gravity="center"
        android:textSize="20sp">

    </Button>
    <TextView
        android:id="@+id/textinfo"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="                     "
        android:textSize="20dp"
        android:gravity="center">
    </TextView>
</LinearLayout>

java代码

package com.example.mybilibili;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.Switch;
import android.widget.TextView;
import android.widget.ToggleButton;

public class ButttonStyle extends AppCompatActivity {
    TextView textView;
    RadioButton r1;//定义各类按钮
    RadioButton r2;
    EditText name;
    Button btn_queeren;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        //在xml视图中找到要控制的对象
        setContentView(R.layout.activity_buttton_style);
        textView=findViewById(R.id.textinfo);
        name=findViewById(R.id.edt);
        btn_queeren=findViewById(R.id.btn_queding);
        r1=findViewById(R.id.boy);
        r2=findViewById(R.id.girl);
        //这里的单选按钮是一个组,所以group也要定义
        RadioGroup sex =findViewById(R.id.sex);

        btn_queeren.setOnClickListener(new mClick());//设置确认按钮的点击事件

        sex.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            //设置单选框的事件监听
            @Override
            public void onCheckedChanged(RadioGroup radioGroup, int i) {
                //这里的i表示第几个单选框被选中了,下标从0开始;
                RadioButton r=findViewById(i);
                //找到i后实例化RadioButton 为r,把当前选中的单选框的值传给textView
                textView.setText("您输入的性别为"+r.getText());
            }
        });
    }


    public void showMSG(View view){
        //自定义一个点击事件,当按钮开关被点击后在textView 中反映其状态
        if (((ToggleButton)view).isChecked()){
            textView.setText("ToggleButton is ON");
        }else{
            textView.setText("ToggleButton is OFF");
        }
    }

    public void showMSG_switch(View view){
        //自定义一个点击事件,当滑动按钮开关被点击后在textView 中反映其状态
        if (((Switch)view).isChecked()){
            textView.setText("Switch is ON");
        }else{
            textView.setText("Switch is OFF");
        }
    }



    class mClick implements View.OnClickListener{
        //利用接口重写方法实现确认按钮点击后在TextView上更新信息
        @Override
        public void onClick(View view) {
        CharSequence str="",name2="";
        name2=name.getText();
            if (r1.isChecked()){
                str=r1.getText();
            }
            if (r2.isChecked()){
                str=r2.getText();
            }
            textView.setText("您输入的信息为\n姓名"+name2+"\n性别"+str);
        }
    }
}

 


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM