Android動(dòng)態(tài)RadioButton含代碼

    Android動(dòng)態(tài)RadioButton

    而不是通過(guò)從調(diào)色板拖放創(chuàng)建RadioButton,Android還可以讓您以編程的方式(動(dòng)態(tài)地)創(chuàng)建它。愛(ài)掏網(wǎng) - it200.com要?jiǎng)?chuàng)建動(dòng)態(tài)RadioButton,我們需要使用 android.view.ViewGroup.LayoutParams ,它配置視圖的寬度和高度,并實(shí)現(xiàn) setOnCheckedChangeListener() 方法和 RadioGroup 類。愛(ài)掏網(wǎng) - it200.com

    動(dòng)態(tài)RadioButton的示例

    讓我們看一個(gè)動(dòng)態(tài)RadioButton的示例。愛(ài)掏網(wǎng) - it200.com

    activity_main.xml

    文件:activity_main.xml

    <?xml version="1.0" encoding="utf-8"?>
    <RelativeLayout 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:id="@+id/relativeLayout"
        tools:context="com.example.test.dynamicradiobutton.MainActivity">
    
    </RelativeLayout>
    

    活動(dòng)類

    文件:MainActivity.java

    package com.example.test.dynamicradiobutton;
    
    import android.support.v7.app.AppCompatActivity;
    import android.os.Bundle;
    import android.widget.RadioButton;
    import android.widget.RadioGroup;
    import android.widget.RelativeLayout;
    
    import android.widget.RelativeLayout.LayoutParams;
    import android.widget.Toast;
    
    public class MainActivity extends AppCompatActivity {
        RadioGroup rg;
        RelativeLayout rl;
        RadioButton rb1,rb2;
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
    
            rg = new RadioGroup(this);
            rl = (RelativeLayout) findViewById(R.id.relativeLayout);
            rb1 = new RadioButton(this);
            rb2 = new RadioButton(this);
    
            rb1.setText("Male");
            rb2.setText("Female");
            rg.addView(rb1);
            rg.addView(rb2);
            rg.setOrientation(RadioGroup.HORIZONTAL);
    
            RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams((int) LayoutParams.WRAP_CONTENT,(int)LayoutParams.WRAP_CONTENT);
            params.leftMargin =150;
            params.topMargin = 100;
    
            rg.setLayoutParams(params);
            rl.addView(rg);
    
            rg.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
                @Override
                public void onCheckedChanged(RadioGroup group, int checkedId) {
                    RadioButton radioButton = (RadioButton) findViewById(checkedId);
                    Toast.makeText(getApplicationContext(),radioButton.getText(),Toast.LENGTH_LONG).show();
                }
            });
        }
    }
    

    聲明:所有內(nèi)容來(lá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)論

    返回頂部

    主站蜘蛛池模板: 美女视频黄a视频全免费网站一区| 国产香蕉一区二区在线网站| 国产波霸爆乳一区二区 | 在线观看国产一区| 国产免费一区二区三区在线观看| 精品无码综合一区二区三区| 红桃AV一区二区三区在线无码AV| 国产中的精品一区的| 一区国严二区亚洲三区| 麻豆va一区二区三区久久浪| 久久一区二区精品| 一区二区三区免费视频播放器| 精品国产福利一区二区| 无码人妻精品一区二区三区夜夜嗨 | 日本在线观看一区二区三区| 亚洲精品精华液一区二区| 欲色aV无码一区二区人妻| 精品一区精品二区制服| 久久久老熟女一区二区三区| 无码一区二区三区视频| 无码精品人妻一区二区三区免费看 | 中文字幕亚洲一区| 国产午夜精品一区二区三区小说| 国产综合精品一区二区| 日本一区中文字幕日本一二三区视频 | 色婷婷香蕉在线一区二区| 精品黑人一区二区三区| 国产日韩一区二区三区| 无码人妻久久一区二区三区免费丨| 色噜噜狠狠一区二区| 国产精品视频免费一区二区| 亚洲日韩中文字幕无码一区| 在线播放精品一区二区啪视频| 国产一区二区三区手机在线观看| 内射少妇一区27P| 日产一区日产2区| 亚洲蜜芽在线精品一区| 无码少妇一区二区| 国产一区二区电影| 国产一区二区女内射| 日韩精品无码一区二区三区AV|