RadioButton的两种显示方式



在页面显示效果,单选按钮


第一种方式: 实现onCheckedChangeListener接口

layout.xml文件

<?xml version="1.0" encoding="utf-8"?>
 <LinearLayout xmlns:android="
http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    >
 <TextView 
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:text="请选择您的性别:"
    android:textSize="9pt"
    />
 <RadioGroup
      android:id="@+id/radioGroup"
      android:contentDescription="性别"
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      >
    <RadioButton
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/radioMale"
         android:text="男"
         android:checked="true"
         />
    <RadioButton
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/radioFemale"
        android:text="女"
        />   
 </RadioGroup>
<TextView 
    android:id="@+id/tvSex"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:text="您的性别是:男"
    android:textSize="9pt"
    />
</LinearLayout>


MainActivity.java 文件


import android.app.Activity;
import android.os.Bundle;

import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.RadioGroup.OnCheckedChangeListener;
import android.widget.TextView;

public class MainActivity extends Activity implements OnCheckedChangeListener {
 private RadioGroup group;// 一个单选按钮组
 private TextView tv;
 private RadioButton rb;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        //根据ID找到该文本控件
        tv = (TextView)this.findViewById(R.id.tvSex);
        group = (RadioGroup) findViewById(R.id.group);
        // 绑定选中状态监听器
        group.setOnCheckedChangeListener(this);
       
       
    }
   
    // RadioGroup的选中状态监听器
    // group:代表被操作的组
    // checkedId:被选中按钮的id
 @Override
 public void onCheckedChanged(RadioGroup group, int checkedId) {
       //获取变更后的选中项的ID
        int radioButtonId = group.getCheckedRadioButtonId();
        //根据ID获取RadioButton的实例
        RadioButton rb = (RadioButton) findViewById(radioButtonId);
        //更新文本内容,以符合选中项
  switch (checkedId) {
  case R.id.male_btn:
   tv.setText("您的性别为:"+rb.getText());
   break;
  case R.id.female_btn:
   tv.setText("您的性别为:"+rb.getText());
   break;
  case R.id.luo_btn:
   tv.setText("您的性别为:"+rb.getText());
   break;
  default:
   break;
  }
  

 }

   
}

第二种方式:绑定匿名监听器
layout .xml文件
<?xml version="1.0" encoding="utf-8"?>
 <LinearLayout xmlns:android="
http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    >
 <TextView 
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:text="请选择您的性别:"
    android:textSize="9pt"
    />
 <RadioGroup
      android:id="@+id/radioGroup"
      android:contentDescription="性别"
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      >
    <RadioButton
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/radioMale"
         android:text="男"
         android:checked="true"
         />
    <RadioButton
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/radioFemale"
        android:text="女"
        />   
 </RadioGroup>
<TextView 
    android:id="@+id/tvSex"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:text="您的性别是:男"
    android:textSize="9pt"
    />
</LinearLayout>
MainActivvity.java文件

import android.os.Bundle;
import android.app.Activity;

import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.RadioGroup.OnCheckedChangeListener;
import android.widget.TextView;

public class MainActivity extends Activity  {

 TextView tv = null;//根据不同选项所要变更的文本控件
     @Override
    public void onCreate(Bundle savedInstanceState) {
           super.onCreate(savedInstanceState);
          
           setContentView(R.layout.activity_main);
          
           //根据ID找到该文本控件
           tv = (TextView)this.findViewById(R.id.tvSex);
          //根据ID找到RadioGroup实例
          RadioGroup group = (RadioGroup)this.findViewById(R.id.radioGroup);
          //绑定一个匿名监听器       
          group.setOnCheckedChangeListener(new OnCheckedChangeListener() {
             
              public void onCheckedChanged(RadioGroup arg0, int arg1) {
                  // TODO Auto-generated method stub
                  //获取变更后的选中项的ID
                  int radioButtonId = arg0.getCheckedRadioButtonId();
                  //根据ID获取RadioButton的实例
                  RadioButton rb = (RadioButton) findViewById(radioButtonId);
                  //更新文本内容,以符合选中项
                  tv.setText("您的性别是:" + rb.getText());
              }
          });
      }
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
C#中的RadioButton是一种用户界面控件,它通常用于提供多个选项供用户选择。RadioButton控件通常与其他控件(如Label或GroupBox)一起使用,以便为用户提供可选的选项。 当用户点击RadioButton时,它会自动选中,并且其他与之相关联的RadioButton会自动取消选中状态。这种行为使得RadioButton适用于单选的场景,例如选择性别、选择支付方式等。 在C#中,可以通过以下步骤来使用RadioButton控件: 1. 在窗体上拖放一个RadioButton控件,或者在代码中动态创建一个RadioButton对象。 2. 设置RadioButton的属性,例如Text(显示在控件旁边的文本)、Checked(是否选中)、Enabled(是否可用)等。 3. 可以将多个RadioButton放置在同一个容器(如Panel或GroupBox)中,并设置它们的GroupName属性为相同的值,以便实现单选效果。 4. 可以通过事件处理程序来响应RadioButton的CheckedChanged事件,以便在用户选择不同选项时执行相应的操作。 以下是一个简单的示例代码,演示了如何创建和使用RadioButton控件: ```csharp using System; using System.Windows.Forms; namespace RadioButtonExample { public partial class Form1 : Form { public Form1() { InitializeComponent(); } private void radioButton1_CheckedChanged(object sender, EventArgs e) { if (radioButton1.Checked) { MessageBox.Show("You selected Option 1"); } } private void radioButton2_CheckedChanged(object sender, EventArgs e) { if (radioButton2.Checked) { MessageBox.Show("You selected Option 2"); } } } } ``` 在上述示例中,我们创建了两个RadioButton控件,并将它们放置在同一个容器(如Panel)中。每个RadioButton都有一个CheckedChanged事件处理程序,当用户选择不同选项时,会弹出相应的消息框。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值