Android RadioButton含代碼

    Android RadioButton

    RadioButton 是一種具有兩種狀態的按鈕,可以選擇”選中”或者”未選中”。愛掏網 - it200.com若單個單選按鈕未選中,我們可以點擊它使其變為選中狀態。愛掏網 - it200.com一旦單選按鈕被選中,用戶將無法將其設為未選中狀態。愛掏網 - it200.com

    RadioButton 通常與 RadioGroup 一起使用。愛掏網 - it200.comRadioGroup 包含多個單選按鈕,標記一個單選按鈕為選中狀態將使其他所有單選按鈕變為未選中狀態。愛掏網 - it200.com

    在本示例中,我們將單獨實現一個單選按鈕,以及一個包含 RadioGroup 的單選按鈕。愛掏網 - it200.com

    activity_main.xml

    文件: 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"  
        android:orientation="vertical"  
        tools:context="example.javatpoint.com.radiobutton.MainActivity">  
    
        <TextView  
            android:id="@+id/textView1"  
            android:layout_width="fill_parent"  
            android:layout_height="wrap_content"  
            android:layout_marginTop="30dp"  
            android:gravity="center_horizontal"  
            android:textSize="22dp"  
            android:text="Single Radio Buttons" />  
    
    
    
        <!--   Default RadioButtons  -->  
    
        <RadioButton  
            android:id="@+id/radioButton1"  
            android:layout_width="fill_parent"  
            android:layout_height="wrap_content"  
            android:layout_gravity="center_horizontal"  
            android:text="Radio Button 1"  
            android:layout_marginTop="20dp"  
    
            android:textSize="20dp" />  
        <RadioButton  
            android:id="@+id/radioButton2"  
            android:layout_width="fill_parent"  
            android:layout_height="wrap_content"  
            android:text="Radio Button 2"  
            android:layout_marginTop="10dp"  
    
            android:textSize="20dp" />  
    
    
        <View  
            android:layout_width="fill_parent"  
            android:layout_height="1dp"  
            android:layout_marginTop="20dp"  
            android:background="#B8B894" />  
    
        <TextView  
            android:id="@+id/textView2"  
            android:layout_width="fill_parent"  
            android:layout_height="wrap_content"  
            android:layout_marginTop="30dp"  
            android:gravity="center_horizontal"  
            android:textSize="22dp"  
            android:text="Radio button inside RadioGroup" />  
    
    
        <!--   Customized RadioButtons  -->  
    
    
        <RadioGroup  
            android:layout_width="wrap_content"  
            android:layout_height="wrap_content"  
            android:id="@+id/radioGroup">  
    
            <RadioButton  
                android:id="@+id/radioMale"  
                android:layout_width="fill_parent"  
                android:layout_height="wrap_content"  
                android:text="  Male"  
                android:layout_marginTop="10dp"  
                android:checked="false"  
                android:textSize="20dp" />  
    
            <RadioButton  
                android:id="@+id/radioFemale"  
                android:layout_width="fill_parent"  
                android:layout_height="wrap_content"  
                android:text="   Female"  
                android:layout_marginTop="20dp"  
                android:checked="false"  
    
                android:textSize="20dp" />  
        </RadioGroup>  
    
        <Button  
            android:layout_width="wrap_content"  
            android:layout_height="wrap_content"  
            android:text="Show Selected"  
            android:id="@+id/button"  
            android:onClick="onclickbuttonMethod"  
            android:layout_gravity="center_horizontal" />  
    
    
    </LinearLayout>  
    

    Activity類

    文件:MainActivity.java

    package example.javatpoint.com.radiobutton;
    
    import android.support.v7.app.AppCompatActivity;
    import android.os.Bundle;
    import android.view.View;
    import android.widget.Button;
    import android.widget.RadioButton;
    import android.widget.RadioGroup;
    import android.widget.Toast;
    
    public class MainActivity extends AppCompatActivity {
        Button button;
        RadioButton genderradioButton;
        RadioGroup radioGroup;
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
            radioGroup=(RadioGroup)findViewById(R.id.radioGroup);
        }
        public void onclickbuttonMethod(View v){
            int selectedId = radioGroup.getCheckedRadioButtonId();
            genderradioButton = (RadioButton) findViewById(selectedId);
            if(selectedId==-1){
                Toast.makeText(MainActivity.this,"Nothing selected", Toast.LENGTH_SHORT).show();
            }
            else{
                Toast.makeText(MainActivity.this,genderradioButton.getText(), Toast.LENGTH_SHORT).show();
            }
    
        }
    }
    

    輸出

    聲明:所有內容來自互聯網搜索結果,不保證100%準確性,僅供參考。如若本站內容侵犯了原著者的合法權益,可聯系我們進行處理。
    發表評論
    更多 網友評論0 條評論)
    暫無評論

    返回頂部

    主站蜘蛛池模板: 亚洲国产成人久久一区WWW| 国产拳头交一区二区| 色婷婷av一区二区三区仙踪林| 濑亚美莉在线视频一区| 国产成人无码一区二区三区| 国产主播一区二区三区| 国产精品夜色一区二区三区| 色噜噜AV亚洲色一区二区| 国产成人无码aa精品一区| 亚洲大尺度无码无码专线一区| 天堂va视频一区二区| 亚洲天堂一区二区三区| 自慰无码一区二区三区| 国产乱码伦精品一区二区三区麻豆| 在线播放一区二区| 日本不卡免费新一区二区三区| 国产成人高清亚洲一区91| 国产精品亚洲一区二区无码| 手机福利视频一区二区| 欧美日韩精品一区二区在线视频| 性色AV一区二区三区| 国产成人AV一区二区三区无码| 国产婷婷一区二区三区| 亚洲爽爽一区二区三区| 三级韩国一区久久二区综合| 日韩十八禁一区二区久久| 免费观看日本污污ww网站一区| 亚洲一区二区三区免费观看| 亚洲av乱码一区二区三区| 无码福利一区二区三区| 蜜芽亚洲av无码一区二区三区 | 人妻AV一区二区三区精品| 久久99热狠狠色精品一区| 久久免费区一区二区三波多野| 久久精品综合一区二区三区| 久久精品中文字幕一区| 一区二区三区四区免费视频| 伊人久久精品一区二区三区| 国产精品综合一区二区| 久久久国产一区二区三区| 性无码免费一区二区三区在线 |