Android 控件之单选框(Toast)
2015-11-30 22:25
471 查看
Android 控件之单选框(Toast)
单选框RadioButton,几个单选项实在一个GroupButton内的,有过前端开发或者学过简单网页制作的人会比较容易理解
效果如下:
三个按钮只能单选,布局文件代码如下:
接下来在activity中根据id调用并设置监听事件,注意此时设置监听器不是为按钮设置,而是为RadioGroup设置,相应代码如下:
System.out.println 主要是调试使用。
Toast在这里的作用就是选中之后会出现一个小提示。
单选框RadioButton,几个单选项实在一个GroupButton内的,有过前端开发或者学过简单网页制作的人会比较容易理解
效果如下:
三个按钮只能单选,布局文件代码如下:
<RadioGroup android:id="@+id/Group1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:orientation="vertical" > <RadioButton android:id="@+id/radioButton1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignLeft="@+id/textView1" android:layout_below="@+id/textView1" android:layout_marginTop="68dp" android:text="@string/radioButton1" /> <RadioButton android:id="@+id/radioButton2" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignLeft="@+id/radioButton1" android:layout_below="@+id/radioButton1" android:layout_marginTop="17dp" android:text="@string/radioButton2" /> <RadioButton android:id="@+id/radioButton3" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignLeft="@+id/radioButton2" android:layout_centerVertical="true" android:text="@string/radioButton3" /> </RadioGroup>一个RadioGroup包裹三个按钮,只有包裹之后才能实现单选,有的开发工具会自动生成按钮 ,但是没有RadioGroup,需要手动添加。
android:orientation="vertical"
RadioGroup的这个属性控制按钮竖向排列
接下来在activity中根据id调用并设置监听事件,注意此时设置监听器不是为按钮设置,而是为RadioGroup设置,相应代码如下:
public class Activity01 extends Activity { private RadioGroup radioGroup; private RadioButton radioButton1,radioButton2,radioButton3; private CheckBox number1,number2,number3; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_activity01); radioGroup=(RadioGroup)findViewById(R.id.Group1); radioButton1=(RadioButton)findViewById(R.id.radioButton1); radioButton2=(RadioButton)findViewById(R.id.radioButton2); radioButton3=(RadioButton)findViewById(R.id.radioButton3); number1=(CheckBox)findViewById(R.id.number1); number1=(CheckBox)findViewById(R.id.number1); number1=(CheckBox)findViewById(R.id.number1); radioGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() { @Override public void onCheckedChanged(RadioGroup arg0, int checkedId) { // TODO Auto-generated method stub if(radioButton1.getId()==checkedId){ System.out.println("radioButton1"); Toast.makeText(Activity01.this, "radioButton1", Toast.LENGTH_LONG).show();; }else if(radioButton2.getId()==checkedId){ System.out.println("radioButton2"); Toast.makeText(Activity01.this, "radioButton2", Toast.LENGTH_LONG).show(); }else if(radioButton3.getId()==checkedId){ System.out.println("radioButton3"); Toast.makeText(Activity01.this, "radioButton3", Toast.LENGTH_LONG).show(); } } }); }在这里注意不是先写OnClickListener然后调用,而是先设置监听事件,然后根据提示完成。
System.out.println 主要是调试使用。
Toast在这里的作用就是选中之后会出现一个小提示。
相关文章推荐
- android框架搭建——封装一个属于自己的数据存储工具类(sharedPreferences篇)
- Activity的启动模式
- android长连接心跳机制
- 学习安卓基础之android 拖动和拖动原理
- Android Studio频繁假死解决方法记录
- Android常用控件之进度条(Progressbar)
- android 音视频直播
- Android第三方开源对话消息提示框:SweetAlertDialog(sweet-alert-dialog)
- 正则表达式匹配日期格式yyyy-mm-dd(支持闰年)
- Android之AutoCompleteTextView控件使用
- 有符号数转无符号数
- Fedora 23建立wifi热点(Android手机可用)
- 【Android】RecyclerView的OnItemClickListener点击事件处理
- Android代码混淆常见配置
- android的EditText重新获取焦点问题
- Android 中 Handler 内存泄露解决办法实例
- 九宫格解锁可用修改版
- 胖虎谈ImageLoader框架(三)
- “私人订制”——栏目管理中算法的使用
- android 学习Layout布局的使用