一区二区日本_久久久久久久国产精品_无码国模国产在线观看_久久99深爱久久99精品_亚洲一区二区三区四区五区午夜_日本在线观看一区二区

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在线精品一区二区 | 亚洲午夜精品视频 | 久久久.com | 日韩一区二区在线视频 | 伊人精品在线 | 天堂影院av | 97伦理影院 | 手机看片在线播放 | 网站黄色在线免费观看 | 日韩欧美精品在线 | 天天干天天爽 | 人人九九精 | 欧美综合一区二区三区 | 国产在线中文字幕 | 亚洲色图第一页 | 资源首页二三区 | 国产精品国产成人国产三级 | 日日夜夜天天 | 国产小视频在线 | 精品av | 一级在线观看 | 精品在线一区 | 欧美欧美欧美 | 欧美三区视频 | 欧美日韩一区精品 | 伊人网综合在线 | jlzzjlzz欧美大全 | 欧美激情久久久 | 亚洲精品一| 中文字幕视频在线观看免费 | 91看片网| 日韩免费一区二区 | www.久久艹 | 精品国产欧美一区二区 | 欧美一区不卡 | 成人av网站在线观看 | 狠狠的干 |