Android RadioButton
16lz
2021-01-23
RadioButton 单选按钮 实现单选功能
package com.example.kn_day04_2radiobutton;
import android.app.Activity; import android.os.Bundle; import android.util.Log; import android.view.View; import android.view.View.OnClickListener; import android.widget.Button; import android.widget.RadioButton; import android.widget.RadioGroup; import android.widget.RadioGroup.OnCheckedChangeListener;
/** * RadioButton单选按钮 * 实现单选功能 * @author KNOWN * */ public class MainActivity extends Activity {
/* * RadioButton单选按钮 * * 要想让几个radiobutton之间具备单选效果 * 必须将他们RadioButton包裹在同一个RadioGroup中 * * */ RadioGroup rg; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); rg = (RadioGroup)findViewById(R.id.radiogroup1); Button btn1 = (Button)findViewById(R.id.btn1); Button btn2 = (Button)findViewById(R.id.btn2); //设置默认选中项,参数对应被选择的RadioButton的id rg.check(R.id.rb1); //设置RadioGroup选项切换时的响应事件 rg.setOnCheckedChangeListener(new OnCheckedChangeListener() { /* * group代表当前的RadioGroup对象 * checkedId代表当前被选中的RadioButton的id * 当id值为-1的时候,代表没有被选择的选项 * */ @Override public void onCheckedChanged(RadioGroup group, int checkedId) { // TODO Auto-generated method stub if( checkedId!=-1){ RadioButton rb = (RadioButton)findViewById(checkedId); Log.i("改变后选中的是:", rb.getText().toString()); }else { Log.i("改变后选中的是:", "无"); } } }); /** * 获取用户选择值 */ btn1.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { if( rg.getCheckedRadioButtonId()!=-1){ RadioButton rb = (RadioButton)findViewById(rg.getCheckedRadioButtonId()); Log.i("当前选中的是:", rb.getText().toString()); } } }); /** * 清空选中项 */ btn2.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { //清空选中项 rg.clearCheck(); Log.i("当前选中的是:", "无"); } }); } } ******** 对应布局文件acvity_maiin.xml
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"
tools:context="com.example.kn_day04_2radiobutton.MainActivity" >
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_marginTop="30dp"
android:layout_marginLeft="30dp"
android:layout_marginStart="30dp"
android:textSize="28sp"
android:text="请选择你的角色" />
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:checkedButton="@+id/rb1"
android:layout_below="@+id/textview1"
android:layout_alignLeft="@+id/textview1"
android:layout_alignStart="@+id/textview1"
android:layout_marginTop="30dp"
>
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="28sp"
android:text="法师"
/>
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="28sp"
android:text="圣骑士"
/>
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="28sp"
android:text="野蛮人"
/>
RadioButton 单选按钮 实现单选功能
package com.example.kn_day04_2radiobutton;
import android.app.Activity; import android.os.Bundle; import android.util.Log; import android.view.View; import android.view.View.OnClickListener; import android.widget.Button; import android.widget.RadioButton; import android.widget.RadioGroup; import android.widget.RadioGroup.OnCheckedChangeListener;
/** * RadioButton单选按钮 * 实现单选功能 * @author KNOWN * */ public class MainActivity extends Activity {
/* * RadioButton单选按钮 * * 要想让几个radiobutton之间具备单选效果 * 必须将他们RadioButton包裹在同一个RadioGroup中 * * */ RadioGroup rg; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); rg = (RadioGroup)findViewById(R.id.radiogroup1); Button btn1 = (Button)findViewById(R.id.btn1); Button btn2 = (Button)findViewById(R.id.btn2); //设置默认选中项,参数对应被选择的RadioButton的id rg.check(R.id.rb1); //设置RadioGroup选项切换时的响应事件 rg.setOnCheckedChangeListener(new OnCheckedChangeListener() { /* * group代表当前的RadioGroup对象 * checkedId代表当前被选中的RadioButton的id * 当id值为-1的时候,代表没有被选择的选项 * */ @Override public void onCheckedChanged(RadioGroup group, int checkedId) { // TODO Auto-generated method stub if( checkedId!=-1){ RadioButton rb = (RadioButton)findViewById(checkedId); Log.i("改变后选中的是:", rb.getText().toString()); }else { Log.i("改变后选中的是:", "无"); } } }); /** * 获取用户选择值 */ btn1.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { if( rg.getCheckedRadioButtonId()!=-1){ RadioButton rb = (RadioButton)findViewById(rg.getCheckedRadioButtonId()); Log.i("当前选中的是:", rb.getText().toString()); } } }); /** * 清空选中项 */ btn2.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { //清空选中项 rg.clearCheck(); Log.i("当前选中的是:", "无"); } }); } } ******** 对应布局文件acvity_maiin.xml
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"
tools:context="com.example.kn_day04_2radiobutton.MainActivity" >
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_marginTop="30dp"
android:layout_marginLeft="30dp"
android:layout_marginStart="30dp"
android:textSize="28sp"
android:text="请选择你的角色" />
android:layout_height="wrap_content"
android:orientation="horizontal"
android:checkedButton="@+id/rb1"
android:layout_below="@+id/textview1"
android:layout_alignLeft="@+id/textview1"
android:layout_alignStart="@+id/textview1"
android:layout_marginTop="30dp"
>
android:layout_height="wrap_content"
android:textSize="28sp"
android:text="法师"
/>
android:layout_height="wrap_content"
android:textSize="28sp"
android:text="圣骑士"
/>
android:layout_height="wrap_content"
android:textSize="28sp"
android:text="野蛮人"
/>
更多相关文章
- Android 单选按钮Radio的使用
- Android下图片或按钮等可拖动到任意位置的效果实现源码
- android 实现透明按钮
- Android RadioGroup RadioButton 自定义样式------按钮式
- Android之Button按钮
- 窗体两个按钮各占一半
- 自定义单选按钮(RadioButton)的样式
- Android按钮控件之RadioGroup和RadioButton
- android单选按钮RadioGroup的详细使用