Android组件(widget)Raidobutton

  
RadioButton的隶属关系

public class RadioButton extends CompoundButton

    java.lang.Object
         android.view.View
             android.widget.TextView
                android.widget.Button
                    android.widget.CompoundButton
                        android.widget.RadioButton
    

RadioButton 就是我们常常讲的 “单选按钮”它只存在两种状态即 选择 和 未选 两种状态,用户的操作方式也就是点击一下它,与复选按钮相反,用户一旦选中就必须选择其中的一个,多个单选按钮通常与RadioGroup同时使用。当一个单选按钮组包含几个按钮的时候,选中其中一个的同时取消其他单选按钮,也就是上面我说到的,一旦选择后就必须是有一个选项是选中的,如果是单独使用的话 一旦选择则无法取消选择!(上图中是选中的RadioButton,此时是选中的男,而如果选择女的话,男就会取消选中!)
下面将我的例子代码贴到下面:
          =main.xml代码====
     
<?xml version="1.0" encoding="utf-8"?>
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >
    <TextView
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="@string/hello" />
    <RadioGroup 
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
       >
<RadioButton 
   android:layout_width="fill_parent"
   android:layout_height="wrap_content"
   android:text="男"
   />
<RadioButton 
   android:layout_width="fill_parent"
   android:layout_height="wrap_content"
   android:text="女"
   />
</RadioGroup>
</LinearLayout>

            ==string.xml代码===
<?xml version="1.0" encoding="utf-8"?>
        <resources>
            <string name="hello">性别选择</string>
            <string name="app_name">组件案例</string>
        </resources>


下面的例子是当我们选中某个单选按钮后程序给我们返回的信息案例:
    开始例子之前先看下常用监听事件:
下面是各种常用控件的事件监听的使用
EditText(编辑框)的事件监听---OnKeyListener
RadioGroup、RadioButton(单选按钮)的事件监听---OnCheckedChangeListener
CheckBox(多选按钮)的事件监听---OnCheckedChangeListener
Spinner(下拉列表)的事件监听---OnItemSelectedListener
Menu(菜单)的事件处理---onMenuItemSelected
Dialog(对话框)的事件监听---DialogInterface.OnClickListener()
=======

====   ViewDemoActivity ========
package cn.view.demo;
import android.app.Activity;
import android.os.Bundle;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.Toast;


public class ViewDemoActivity extends Activity { 
private RadioGroup group;
private RadioButton nan,nv;
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
         group = (RadioGroup) this.findViewById(R.id.radio_gro);
         nan = (RadioButton) this.findViewById(R.id.radio_nan);
         nv = (RadioButton) this.findViewById(R.id.radio_nv);
         group.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
public void onCheckedChanged(RadioGroup group, int checkedId) {
if(checkedId ==nan.getId()){
showmsg("你好先生,设置成功"+nan.getText());
}else if(checkedId==nv.getId()){
showmsg("你好女士,设置成功"+nan.getText()); 
} 
} 
});
     }
private void showmsg(String str) { 
Toast.makeText(this, str, Toast.LENGTH_SHORT).show(); 
}
}
=======main.xml========
main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >
    <TextView
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="@string/hello" />
    <RadioGroup 
        android:id="@+id/radio_gro"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
       >
<RadioButton 
    android:id="@+id/radio_nan"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:text="男"
    />
<RadioButton 
    android:id="@+id/radio_nv"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:text="女"
    />
</RadioGroup>
</LinearLayout>




这就是 RaidoButton 相关的学习!
其实上面的方法还可以使用 接口俩实现,只要 Activity 实现OnCheckedChangeListener 接口就可以在单独的方法中(onCheckedChanged() )实现上面的操作!
 
power by 白璐.中国  
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
在使用 Prism 框架时,我们可以通过以下步骤来获取 Prism RadioButton 的选中值: 1. 首先,在 XAML 中创建一个 RadioButton 组,并为每个 RadioButton 设置不同的值和一个共同的 Command: ```xml <RadioButton IsChecked="{Binding SelectedOption, Mode=TwoWay, Converter={StaticResource EnumToBooleanConverter}, ConverterParameter=Option1}"> Option 1 </RadioButton> <RadioButton IsChecked="{Binding SelectedOption, Mode=TwoWay, Converter={StaticResource EnumToBooleanConverter}, ConverterParameter=Option2}"> Option 2 </RadioButton> <RadioButton IsChecked="{Binding SelectedOption, Mode=TwoWay, Converter={StaticResource EnumToBooleanConverter}, ConverterParameter=Option3}"> Option 3 </RadioButton> <Button Content="Get Selected Value" Command="{Binding GetSelectedOptionCommand}" /> ``` 2. 在 ViewModel 中,创建一个属性来保存选中的值: ```csharp private Option _selectedOption; public Option SelectedOption { get { return _selectedOption; } set { SetProperty(ref _selectedOption, value); } } ``` 3. 创建一个命令来获取选中的值: ```csharp public DelegateCommand GetSelectedOptionCommand { get; private set; } public YourViewModel() { GetSelectedOptionCommand = new DelegateCommand(GetSelectedOption); } private void GetSelectedOption() { // 通过 SelectedOption 属性获取选中的值 // 进行相关处理 } ``` 4. 最后,在 Converter 中实现 EnumToBooleanConverter 的逻辑,该 Converter 将枚举值转换为布尔值以便在 RadioButton 上使用。 综上所述,通过上述步骤,我们就可以在 Prism 中获取到 RadioButton 的选中值,并进行相应的处理。尽管这只是其中一种实现方式,但在 Prism 中,你可以根据自己的需求进行适当的调整和修改。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值