Android 开发笔记___RadioButton

从来好事天生俭,自古瓜儿苦后甜。这篇文章主要讲述Android 开发笔记___RadioButton相关的知识,希望能为你提供帮助。

Android 开发笔记___RadioButton

文章图片
Android 开发笔记___RadioButton

文章图片

 
horizontal
1 < LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 2android:layout_width="match_parent" 3android:layout_height="match_parent" 4android:orientation="vertical" 5android:padding="10dp" > 6 7< TextView 8android:layout_width="match_parent" 9android:layout_height="wrap_content" 10android:text="请选择您的性别" 11android:textColor="#000000" 12android:textSize="17sp" /> 13 14< RadioGroup 15android:id="@+id/rg_sex" 16android:layout_width="match_parent" 17android:layout_height="wrap_content" 18android:orientation="horizontal" > 19 20< RadioButton 21android:id="@+id/rb_male" 22android:layout_width="0dp" 23android:layout_height="wrap_content" 24android:layout_weight="1" 25android:checked="false" 26android:text="男" 27android:textColor="#000000" 28android:textSize="17sp" /> 29 30< RadioButton 31android:id="@+id/rb_female" 32android:layout_width="0dp" 33android:layout_height="wrap_content" 34android:layout_weight="1" 35android:checked="false" 36android:text="女" 37android:textColor="#000000" 38android:textSize="17sp" /> 39< /RadioGroup> 40 41< TextView 42android:id="@+id/tv_sex" 43android:layout_width="match_parent" 44android:layout_height="wrap_content" 45android:textColor="#000000" 46android:textSize="17sp" /> 47 48< View 49android:layout_width="match_parent" 50android:layout_height="20dp" /> 51 52 < /LinearLayout>

1 package com.example.alimjan.hello_world; 2 3 import android.content.Context; 4 import android.content.Intent; 5 import android.os.Bundle; 6 import android.support.v7.app.AppCompatActivity; 7 import android.widget.RadioGroup; 8 import android.widget.TextView; 9 10 /** 11* Created by alimjan on 7/2/2017. 12*/ 13 14 public class class_3_2_3 extends AppCompatActivity implements RadioGroup.OnCheckedChangeListener { 15 16private final static String TAG = "RadioHorizontalActivity"; 17private TextView tv_sex; 18 19@Override 20protected void onCreate(Bundle savedInstanceState) { 21super.onCreate(savedInstanceState); 22setContentView(R.layout.code_3_2_3); 23tv_sex = (TextView) findViewById(R.id.tv_sex); 24RadioGroup rg_sex = (RadioGroup) findViewById(R.id.rg_sex); 25rg_sex.setOnCheckedChangeListener(this); 26} 27 28@Override 29public void onCheckedChanged(RadioGroup group, int checkedId) { 30if (checkedId == R.id.rb_male) { 31tv_sex.setText("哇哦,你是个帅气的男孩"); 32} else if (checkedId == R.id.rb_female) { 33tv_sex.setText("哇哦,你是个漂亮的女孩"); 34} 35} 36 37public static void startHome(Context mContext) { 38Intent intent = new Intent(mContext, class_3_2_3.class); 39mContext.startActivity(intent); 40} 41 }

【Android 开发笔记___RadioButton】vertical
1 < LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 2android:layout_width="match_parent" 3android:layout_height="match_parent" 4android:orientation="vertical" 5android:padding="10dp" > 6 7< TextView 8android:layout_width="match_parent" 9android:layout_height="wrap_content" 10android:text="请选择您的婚姻状况" 11android:textColor="#000000" 12android:textSize="17sp" /> 13 14< RadioGroup 15android:id="@+id/rg_marry" 16android:layout_width="match_parent" 17android:layout_height="wrap_content" 18android:orientation="vertical" > 19 20< RadioButton 21android:id="@+id/rb_unmarried" 22android:layout_width="wrap_content" 23android:layout_height="wrap_content" 24android:padding="5dp" 25android:button="@drawable/radio_selector" 26android:text="未婚" 27android:textColor="#000000" 28android:textSize="17sp" /> 29 30< RadioButton 31android:id="@+id/rb_married" 32android:layout_width="wrap_content" 33android:layout_height="wrap_content" 34android:padding="5dp" 35android:button="@null" 36android:drawableLeft="@drawable/radio_selector" 37android:drawablePadding="10dp" 38android:text="已婚" 39android:textColor="#000000" 40android:textSize="17sp" /> 41< /RadioGroup> 42 43< TextView 44android:id="@+id/tv_marry" 45android:layout_width="match_parent" 46android:layout_height="wrap_content" 47android:textColor="#000000" 48android:textSize="17sp" /> 49 50 < /LinearLayout>

1 package com.example.alimjan.hello_world; 2 3 import android.content.Context; 4 import android.content.Intent; 5 import android.os.Bundle; 6 import android.support.v7.app.AppCompatActivity; 7 import android.widget.RadioGroup; 8 import android.widget.TextView; 9 10 /** 11* Created by alimjan on 7/2/2017. 12*/ 13 14 public class class_3_2_3_2 extends AppCompatActivity implements RadioGroup.OnCheckedChangeListener { 15 16private final static String TAG = "RadioVerticalActivity"; 17private TextView tv_marry; 18 19@Override 20protected void onCreate(Bundle savedInstanceState) { 21super.onCreate(savedInstanceState); 22setContentView(R.layout.code_3_2_3_2); 23tv_marry = (TextView) findViewById(R.id.tv_marry); 24RadioGroup rg_marry = (RadioGroup) findViewById(R.id.rg_marry); 25rg_marry.setOnCheckedChangeListener(this); 26} 27 28@Override 29public void onCheckedChanged(RadioGroup group, int checkedId) { 30if (checkedId == R.id.rb_married) { 31tv_marry.setText("哇哦,祝你早生贵子"); 32} else if (checkedId == R.id.rb_unmarried) { 33tv_marry.setText("哇哦,你的前途不可限量"); 34} 35} 36 37public static void startHome(Context mContext) { 38Intent intent = new Intent(mContext, class_3_2_3_2.class); 39mContext.startActivity(intent); 40} 41 42 }

 

    推荐阅读