• 博客园logo
  • 会员
  • 众包
  • 新闻
  • 博问
  • 闪存
  • 赞助商
  • HarmonyOS
  • Chat2DB
    • 搜索
      所有博客
    • 搜索
      当前博客
  • 写随笔 我的博客 短消息 简洁模式
    用户头像
    我的博客 我的园子 账号设置 会员中心 简洁模式 ... 退出登录
    注册 登录
火磷
Memory will fade,but not notes.
博客园    首页    新随笔    联系   管理    订阅  订阅
Android开发--RadioButton的应用

1.简介

RadioButton为单选按钮,当一个按钮被选中后,点击其他按钮无法使上一个按钮变为未选中状态。RadioGroup是可以容纳多个RadioButton的容器,

通过使用RadioGroup,可以实现每次只有一个处于按钮被选中状态。

2.构建

我们可以从From Widgets中选择单选按钮,构建图二所示界面。

      

XML代码:

 1 <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
 2     xmlns:tools="http://schemas.android.com/tools"
 3     android:id="@+id/RelativeLayout1"
 4     android:layout_width="fill_parent"
 5     android:layout_height="fill_parent"
 6     android:orientation="vertical" >
 7 
 8 
 9     <TextView
10           android:id="@+id/textView"
11          android:layout_marginTop="10dp"
12          android:layout_width="match_parent"
13          android:layout_height="250dp"
14          android:text="@string/tx"    >   
15      </TextView>
16      
17     <RadioGroup 
18         android:id="@+id/rg"
19         android:layout_width="fill_parent"
20         android:layout_height="wrap_content"
21         android:orientation="vertical">
22         
23     <RadioButton 
24         android:id="@+id/radioButton1"
25         android:layout_width="fill_parent"
26         android:layout_height="100dp"
27         android:text="@string/rb1"/>
28     
29     <RadioButton
30         android:id="@+id/radioButton2"
31         android:layout_width="fill_parent"
32         android:layout_height="74dp"
33         android:text="@string/rb2" />
34 
35     </RadioGroup>
36 </RelativeLayout>

3.代码

 1 public class Activity1 extends Activity {
 2     private RadioButton rbutton1;
 3     private RadioButton rbutton2;
 4     private RadioGroup rgroup;
 5     private TextView tv;
 6     @Override
 7     protected void onCreate(Bundle savedInstanceState) {
 8         super.onCreate(savedInstanceState);
 9         setContentView(R.layout.act1);
10         rbutton1 = (RadioButton) findViewById(R.id.radioButton1);
11         rbutton2 = (RadioButton) findViewById(R.id.radioButton2);
12         rgroup = (RadioGroup) findViewById(R.id.rg);
13         tv = (TextView) findViewById(R.id.textView);
14         //为RadioGroup设置监听器
15         rgroup.setOnCheckedChangeListener(new OnCheckedChangeListener() {
16             public void onCheckedChanged(RadioGroup group, int checkedId) {
17                 if (checkedId == rbutton1.getId()) {
18                     DisplayToast("you have chosen " + rbutton1.getText());
19                 } else {
20                     DisplayToast("you have chosen " + rbutton2.getText());
21                 }
22             }
23         });
24     }
25       //自定义位置Toast
26     public void DisplayToast(String string) {
27         //最后一项表示内容显示时间
28         Toast mToast = Toast.makeText(getApplicationContext(), string, Toast.LENGTH_LONG);
29         //内容在底部显示,前一项表示左右移动,后一项表示上下移动
30         mToast.setGravity(Gravity.BOTTOM, 0, 200);
31         mToast.show();
32     }

4.效果

posted on 2016-02-03 14:45  火磷  阅读(352)  评论(0)    收藏  举报
刷新页面返回顶部
博客园  ©  2004-2025
浙公网安备 33010602011771号 浙ICP备2021040463号-3