《步驟化》RadioGroup和RadioButton的簡單使用

如題:界面上有兩個按鈕,隨意點擊一個,文本框顯示不同內容
思路:利用RadioGroup,RadioButton和TextView解決
編譯工具:android studio 3.5.2
註釋很清楚了,廢話少說,直接上步驟:
佈局activity_main.xml頁面

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout 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"
    tools:context=".MainActivity">
    <RadioGroup
        android:id="@+id/radioGroup"
        android:layout_width="106dp"
        android:layout_height="140dp"
        android:layout_marginStart="112dp"
        android:layout_marginLeft="112dp"
        android:layout_marginTop="164dp"
        android:layout_marginBottom="232dp"
        app:layout_constraintBottom_toTopOf="@+id/textView"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent">
        <RadioButton
            android:id="@+id/radioButton"
            android:layout_width="97dp"
            android:layout_height="53dp"
            android:text="女" />
        <RadioButton
            android:id="@+id/radioButton2"
            android:layout_width="97dp"
            android:layout_height="58dp"
            android:text="男" />
    </RadioGroup>
    <TextView
        android:id="@+id/textView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginStart="136dp"
        android:layout_marginLeft="136dp"
        android:layout_marginBottom="170dp"
        android:hint="內容在此顯示"
        android:text=""
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/radioGroup" />
</androidx.constraintlayout.widget.ConstraintLayout>

主代碼MainActivity.java

package com.example.a201911181x;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.TextView;
public class MainActivity extends AppCompatActivity {
    private RadioButton radioButton,radioButton2;
    private RadioGroup radioGroup;
    private TextView textView;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        radioGroup=(RadioGroup) findViewById(R.id.radioGroup);
        radioButton=(RadioButton) findViewById(R.id.radioButton);
        radioButton2=(RadioButton) findViewById(R.id.radioButton2);
        textView=(TextView) findViewById(R.id.textView);
        //提供了3種解決辦法,每種方法在本程序的運行結果幾乎一致,但是在以後實際的運用中,可能產生的效果略有不同,具體您適合哪種,請自行檢測
        //第一種(運行該方法前請用//將其他兩種屏蔽):利用if..else..
        //radioGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
           // @Override
            //public void onCheckedChanged(RadioGroup group, int checkedId) {
                //if(checkedId==R.id.radioButton){
                    //textView.setText("你選擇了女");
               // }else {
                    //textView.setText("你選擇了男");
                //}
           // }
       // });

        //第二種方法(運行該方法前請用//將其他兩種屏蔽):利用if...else if...
       radioGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
           //其實我也搞不明白這個和第一種有什麼很大的區別,可能在其他的運用中有不同的效果
           @Override
           public void onCheckedChanged(RadioGroup group, int checkedId) {
               if(checkedId==radioButton.getId()){
                   textView.setText("你選擇了女");
               }else if(checkedId==radioButton2.getId()){
                   textView.setText("你選擇了男");
               }
           }
       });

        //第三種方法(運行該方法前請用//將前兩種屏蔽):利用switch...case..
        //radioGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            //@Override
            //public void onCheckedChanged(RadioGroup group, int checkedId) {
                //switch (radioGroup.getCheckedRadioButtonId()){
                    //case R.id.radioButton:
                       // textView.setText("你選擇了女");
                       // break;
                    //case R.id.radioButton2:
                        //textView.setText("你選擇了男");
                        //break;
               // }
           // }
       // });
    }
}

結語:代碼比較簡單,嗯…喜歡哪種就用哪種唄

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章