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 條評論)
暫無評論

返回頂部

主站蜘蛛池模板: 吃奶摸下的激烈免费视频播放 | 久久久久亚洲AV无码专区网站| 91香蕉国产线观看免费全集| 男人边吃奶边做性视频| 把腿扒开做爽爽视频在线看 | 亚洲国产成人超福利久久精品| 99久久免费国产精精品| 激情六月在线视频观看 | 国产一国产二国产三国产四国产五| 久久在精品线影院精品国产| 久久精品国产99国产精偷| 久久综合狠狠综合久久97色| 极品色天使在线婷婷天堂亚洲 | 久久无码人妻一区二区三区午夜| 91抖音在线观看| 日本高清乱理论片| 国产三级精品三级在专区中文| 亚洲欧美中文字幕5发布| 33333在线亚洲| 樱桃视频影院在线播放免费下载| 国产最新凸凹视频免费| 久久天天躁狠狠躁夜夜躁2014| 这里只有精品网| 成人艳情一二三区| 免费无码又爽又刺激毛片| www性久久久com| 欧美重口另类在线播放二区| 国产精品户外野外| 久九九久福利精品视频视频| 青青青青青国产免费手机看视频| 日日碰狠狠添天天爽超碰97| 午夜爽爽爽男女污污污网站| japanese21hdxxxx喷潮| 精品福利一区二区三区免费视频 | 欧美交换性一区二区三区| 日韩卡一卡2卡3卡4| 国产v亚洲v天堂无码网站| yellow字幕网在线91pom国产| 波多野结衣中文字幕一区二区三区| 国产精品美女一区二区| 久久综合色综合|