一区二区日本_久久久久久久国产精品_无码国模国产在线观看_久久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 條評論)
暫無評論

返回頂部

主站蜘蛛池模板: 亚洲综合在线网 | 国产精品精品3d动漫 | 久草日韩 | 夜夜爽99久久国产综合精品女不卡 | 天堂网色| 亚洲精品日韩视频 | 2018国产大陆天天弄 | 国产不卡一区在线观看 | 中文字幕在线观看 | 欧美bondage紧缚视频 | 成人不卡| 国际精品鲁一鲁一区二区小说 | 欧美三级视频在线观看 | 亚洲精品电影在线观看 | 中文字幕免费在线 | 欧美成人精品一区二区三区 | 免费精品视频在线观看 | 国产视频一区在线 | 国产原创在线观看 | 午夜久久久 | 精品视频久久久久久 | 国产精品视频在线免费观看 | 国产精品久久 | 美女久久久久 | 精品网| 在线91 | 天天干亚洲 | 一区二区三区成人 | 久久国产精品72免费观看 | 天天干天天插天天 | 欧美日韩在线观看一区二区三区 | 中文字幕 国产精品 | 国产精品欧美一区喷水 | 激情 婷婷 | 在线免费观看黄视频 | 成人福利在线 | 国产高清精品一区 | 91夜色在线观看 | 中文字幕在线视频观看 | 在线免费观看毛片 | 操人视频在线观看 |