Android?studio實現(xiàn)單選按鈕
本文實例為大家分享了Android studio實現(xiàn)單選按鈕的具體代碼,供大家參考,具體內(nèi)容如下
創(chuàng)建空activity
編輯activity_main.xml文件
代碼如下:
<?xml version="1.0" encoding="utf-8"?> <androidx.appcompat.widget.LinearLayoutCompat ? ? 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="wrap_content" ? ? android:orientation="vertical" ? ? android:gravity="center" ? ? tools:context=".MainActivity"> ? ? <TextView ? ? ? ? android:id="@+id/chooseTxt" ? ? ? ? android:layout_width="match_parent" ? ? ? ? android:layout_height="wrap_content" ? ? ? ? android:gravity="center" ? ? ? ? android:text="@string/text1" ? ? ? ? android:textColor="@color/colorblack" ? ? ? ? android:textSize="30sp" /> ? ? ? ? //定義RaidGroup是要注意屬性添加的位置 ? ? <RadioGroup ? ? ? ? android:id="@+id/radioGroup" ? ? ? ? android:layout_width="match_parent" ? ? ? ? android:layout_height="wrap_content" ? ? ? ? android:orientation="vertical" ? ? ? ? android:gravity="center" ? ? ? ? > ? ? ? ? <RadioButton ? ? ? ? ? ? android:id="@+id/radioButton1" ? ? ? ? ? ? android:layout_width="wrap_content" ? ? ? ? ? ? android:layout_height="match_parent" ? ? ? ? ? ? android:button="@null" ? ? ? ? ? ? android:drawableRight="@android:drawable/btn_radio" ? ? ? ? ? ? android:text="@string/text2" ? ? ? ? ? ? /> ? ? ? ? <RadioButton ? ? ? ? ? ? android:id="@+id/radioButton2" ? ? ? ? ? ? android:layout_width="wrap_content" ? ? ? ? ? ? android:layout_height="match_parent" ? ? ? ? ? ? android:text="@string/text3" ? ? ? ? ? ? /> ? ? </RadioGroup> ? ? <Button ? ? ? ? android:id="@+id/ClearBtn" ? ? ? ? android:layout_width="match_parent" ? ? ? ? android:layout_height="wrap_content" ? ? ? ? android:text="@string/text4" /> ? ? <Button ? ? ? ? android:id="@+id/AddBtn" ? ? ? ? android:layout_width="match_parent" ? ? ? ? android:layout_height="match_parent" ? ? ? ? android:text="@string/text5" ? ? ? ? ?/> </androidx.appcompat.widget.LinearLayoutCompat>
還有strings.xml文件,代碼如下:
<resources> ? ? <string name="app_name">My App</string> ? ? <string name="text1">我選擇的是...?</string> ? ? <string name="text2">按鈕1</string> ? ? <string name="text3">按鈕2</string> ? ? <string name="text4">清除選中</string> ? ? <string name="text5">添加子項</string> </resources>
再是MainActivity.java文件,代碼如下:
package com.example.myapp;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup.LayoutParams;
import android.widget.Button;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.RadioGroup.OnCheckedChangeListener;
import android.widget.TextView;
public class MainActivity extends AppCompatActivity {
? ? private RadioGroup ?radioGroup;
? ? private RadioButton radioButton1;
? ? private RadioButton radioButton2;
? ? private Button radioClearBtn;
? ? private Button radioAddBtn;
? ? private TextView chooseTxt;
? ? @Override
? ? protected void onCreate(Bundle savedInstanceState) {
? ? ? ? super.onCreate(savedInstanceState);
? ? ? ? setContentView(R.layout.activity_main);
? ? ? ? radioButton1 = findViewById(R.id.radioButton1);
? ? ? ? radioButton2 = findViewById(R.id.radioButton2);
? ? ? ? radioGroup= findViewById(R.id.radioGroup);
? ? ? ? //設(shè)置選中變換監(jiān)聽
? ? ? ? radioGroup.setOnCheckedChangeListener(onCheckedChangeListener);
? ? ? ? //分別為兩個按鈕設(shè)置點擊監(jiān)聽
? ? ? ? radioClearBtn = findViewById(R.id.ClearBtn);
? ? ? ? radioClearBtn.setOnClickListener(onClickListener);
? ? ? ? radioAddBtn = findViewById(R.id.AddBtn);
? ? ? ? radioAddBtn.setOnClickListener(onClickListener);
? ? ? ? chooseTxt = findViewById(R.id.chooseTxt);
? ? }
? ? //onCheckedChangeListener()方法
? ? private ?OnCheckedChangeListener onCheckedChangeListener=new OnCheckedChangeListener() {
? ? ? ? @Override
? ? ? ? public void onCheckedChanged(RadioGroup group, int checkedId) {
? ? ? ? //定義id并賦值被選中的單選按鈕的id
? ? ? ? ? ? int id = group.getCheckedRadioButtonId();
? ? ? ? ? ? switch (id) {
? ? ? ? ? ? ? ? case R.id.radioButton1:
? ? ? ? ? ? ? ? ? ? chooseTxt.setText("我選擇的是:" + radioButton1.getText());
? ? ? ? ? ? ? ? ? ? break;
? ? ? ? ? ? ? ? case R.id.radioButton2:
? ? ? ? ? ? ? ? ? ? chooseTxt.setText("我選擇的是:" + radioButton2.getText());
? ? ? ? ? ? ? ? ? ? break;
? ? ? ? ? ? ? ? default:
? ? ? ? ? ? ? ? ? ? chooseTxt.setText("我選擇的是:新增");
? ? ? ? ? ? ? ? ? ? break;
? ? ? ? ? ? ?}
? ? ? ? }
? ? };
? ? private OnClickListener onClickListener = new OnClickListener() {
? ? ? ? @Override
? ? ? ? public void onClick(View view) {
? ? ? ? ? ? ? ? switch (view.getId()) {
? ? ? ? ? ? ? ? ? ? case R.id.ClearBtn:
? ? ? ? ? ? ? ? ? ? ? ? radioGroup.check(-1);//清除選項
? ? ? ? ? ? ? ? ? ? ? ? chooseTxt.setText("我選擇的是...?");
? ? ? ? ? ? ? ? ? ? ? ? break;
? ? ? ? ? ? ? ? ? ? case R.id.AddBtn:
? ? ? ? ? ? ? ? ? ? ? ? RadioButton newRadio = new RadioButton(MainActivity.this);
? ? ? ? ? ? ? ? ? ? ? ? //將新增的radiobutton加入到radioGroup中
? ? ? ? ? ? ? ? ? ? ? ? newRadio.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.MATCH_PARENT));
? ? ? ? ? ? ? ? ? ? ? ? newRadio.setText("新增");
? ? ? ? ? ? ? ? ? ? ? ? radioGroup.addView(newRadio, radioGroup.getChildCount());
? ? ? ? ? ? ? ? ? ? ? ? break;
? ? ? ? ? ? ? ? ? ? default:
? ? ? ? ? ? ? ? ? ? ? ? break;
? ? ? ? ? ? ? ? }
? ? ? ? }
? ? };
}運行結(jié)果如下:





以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
Android使用自定義View實現(xiàn)360手機衛(wèi)士波浪球進(jìn)度的效果
360衛(wèi)士的波浪球進(jìn)度的效果,一般最常用的方法就是畫線的方式,先繪sin線或貝塞爾曲線,然后從左到右繪制豎線,然后再裁剪圓區(qū)域2018-05-05
Android形狀圖形與狀態(tài)列表圖形及九宮格圖片超詳細(xì)講解
這篇文章主要介紹了Android形狀圖形與狀態(tài)列表圖形及九宮格圖片的應(yīng)用方法,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)吧2022-09-09
Android可循環(huán)顯示圖像的Android Gallery組件用法實例
這篇文章主要介紹了Android可循環(huán)顯示圖像的Android Gallery組件用法,結(jié)合實例形式分析了Gallery組件的功能,使用方法及相關(guān)注意事項,需要的朋友可以參考下2016-04-04
詳解 Android中Libgdx使用ShapeRenderer自定義Actor解決無法接收到Touch事件的問題
這篇文章主要介紹了詳解 Android中Libgdx使用ShapeRenderer自定義Actor解決無法接收到Touch事件的問題的相關(guān)資料,希望通過本文能幫助到大家解決這樣的問題,需要的朋友可以參考下2017-09-09
Android ListView和Adapter數(shù)據(jù)適配器的簡單介紹
這篇文章主要介紹了Android ListView和Adapter數(shù)據(jù)適配器的簡單介紹,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2018-04-04

