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

Android自定義RadioButton含代碼

Android自定義RadioButton

與默認的Android RadioButton用戶界面不同,我們還可以實現自定義的單選按鈕。愛掏網 - it200.com自定義RadioButton可以使用戶界面更加吸引人。愛掏網 - it200.com

我們來看一個自定義RadioButton的示例。愛掏網 - 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:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    android:orientation="vertical"
    tools:context="com.example.test.customradiobutton.MainActivity">



    <TextView
        android:id="@+id/tv"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="30dp"
        android:gravity="center_horizontal"
        android:textSize="25dp"
        android:text="Customized Radio Buttons" />


    <!--   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:button="@drawable/custom_radio_button"
            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:button="@drawable/custom_radio_button"
            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>

custom_radio_button.xml

現在在drawable目錄下的另一個文件(custom_radio_button.xml)中實現一個選擇器,并放置兩個不同的選中和未選中的按鈕圖片。愛掏網 - it200.com

文件:checkbox.xml

<?xml version="1.0" encoding="utf-8"?>

<selector xmlns:android="http://schemas.android.com/apk/res/android">

    <item android:state_checked="true" android:drawable="@drawable/checkedradiobutton" />
    <item android:state_checked="false" android:drawable="@drawable/unchekedradiobutton" />

</selector>

Activity類

文件:MainActivity.java

package com.example.test.customradiobutton;

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大神在线看| 丁香婷婷久久久综合精品国产 | 日本黄色大片免费看 | 天堂一区在线观看 | 中文字幕在线一区二区三区 | 国产 欧美 日韩 一区 | 久久久人成影片免费观看 | www日日日 | 激情五月婷婷综合 | 美女黄频| 久久久久国产一区二区三区 | 欧美精品一区三区 | 国产成人精品一区二区三区 | 91在线中文字幕 | 亚洲男人网| www.99精品| 欧美h | 操操日| 精品伊人久久 | 特一级黄色毛片 | 中文在线a在线 | 一级毛片在线播放 | 国产亚洲精品成人av久久ww | 精品日本久久久久久久久久 | 一区二区三区不卡视频 | 少妇无套高潮一二三区 | 日韩在线免费视频 | 午夜精品影院 | 亚洲精品视频在线播放 | 亚洲精品天堂 |