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ā)表評(píng)論
    更多 網(wǎng)友評(píng)論0 條評(píng)論)
    暫無(wú)評(píng)論

    返回頂部

    主站蜘蛛池模板: 久久国产一区二区| 日本免费一区二区三区四区五六区 | 日韩一区二区三区不卡视频| 亚洲日韩精品一区二区三区| 国产成人一区二区三区免费视频 | 在线观看国产一区二三区| 国产精品无码一区二区三区毛片 | 国产成人AV区一区二区三| 国产精品高清一区二区三区| 成人区人妻精品一区二区不卡视频| 日韩一区二区三区射精| 精品一区二区三区在线观看视频 | 精品少妇ay一区二区三区| 精品国产香蕉伊思人在线在线亚洲一区二区 | 国偷自产视频一区二区久| 欧美日韩综合一区二区三区| 亚洲蜜芽在线精品一区| 美女视频在线一区二区三区| 国精无码欧精品亚洲一区| 国产福利电影一区二区三区,亚洲国模精品一区 | 成人日韩熟女高清视频一区| 精品一区二区三区免费观看 | 中文人妻无码一区二区三区| 亚洲AV永久无码精品一区二区国产| 国产在线观看精品一区二区三区91| 亚洲乱码av中文一区二区| 午夜福利无码一区二区| 午夜DV内射一区区| 国产在线观看一区二区三区 | 亚洲精品伦理熟女国产一区二区| 亚洲高清一区二区三区| 九九无码人妻一区二区三区| 毛片一区二区三区无码| 国产人妖视频一区在线观看| 日韩一区二区三区精品| 中文字幕一区二区三| 无码一区二区三区| 亚洲一区二区三区高清在线观看 | 日韩一区二区三区无码影院| 国产免费一区二区三区| 国产精品日韩一区二区三区|