Android自定義RadioButton含代碼

Android自定義RadioButton

與默認(rèn)的Android RadioButton用戶界面不同,我們還可以實(shí)現(xiàn)自定義的單選按鈕。愛掏網(wǎng) - it200.com自定義RadioButton可以使用戶界面更加吸引人。愛掏網(wǎng) - it200.com

我們來看一個(gè)自定義RadioButton的示例。愛掏網(wǎng) - 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

現(xiàn)在在drawable目錄下的另一個(gè)文件(custom_radio_button.xml)中實(shí)現(xiàn)一個(gè)選擇器,并放置兩個(gè)不同的選中和未選中的按鈕圖片。愛掏網(wǎng) - 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();
        }

    }
}

輸出

聲明:所有內(nèi)容來自互聯(lián)網(wǎng)搜索結(jié)果,不保證100%準(zhǔn)確性,僅供參考。如若本站內(nèi)容侵犯了原著者的合法權(quán)益,可聯(lián)系我們進(jìn)行處理。
發(fā)表評論
更多 網(wǎng)友評論0 條評論)
暫無評論

返回頂部

主站蜘蛛池模板: 国产91青青成人a在线| 在公交车上被站着被c| 天天综合网色中文字幕| 四虎影在永久地址在线观看| 中国一级毛片视频免费看| 韩国精品欧美一区二区三区| 无码视频一区二区三区| 午夜免费福利在线观看| 中文字幕在线观看第二页| 精品国产午夜福利在线观看| 新梅金瓶2之爱奴国语| 免费国产午夜高清在线视频| 777米奇影视盒| 日本高清视频色wwwwww色| 啊~又多了一根手指| 99久久精品免费看国产一区二区三区 | 91久久打屁股调教网站| 日韩夜夜高潮夜夜爽无码| 啊轻点灬大ji巴太粗太长了欧美| a级毛片免费播放| 模特侨依琳大尺度流出| 国产69精品久久久久9999| 99精品众筹模特私拍在线| 李宗60集奇奥网全集| 四虎永久在线精品国产馆v视影院 四虎永久在线精品影院 | 伊人久久精品线影院| 夜夜爽免费视频| 樱花草www日本在线观看| 国产v亚洲v天堂无码| 99精品视频免费观看| 晚上睡不着来b站一次看过瘾| 国产孕妇孕交大片孕| 一本精品中文字幕在线| 欧美俄罗斯乱妇| 国产成人精品视频一区二区不卡 | 里番本子侵犯肉全彩3d| 日产国产欧美视频一区精品| 亚洲色图黄色小说| 高清一级做a爱过程免费视频| 日本视频在线观看免费| 俄罗斯小小幼儿视频大全|