android 编程中怎样从单选按钮获取数据

2025-05-08 01:32:32
推荐回答(4个)
回答1:


对于如图所示的单选按钮 xml文件表示为

          android:id="@+id/sex"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content">
                    android:id="@+id/male"
            android:text="男"/>
                    android:id="@+id/female"
            android:text="女"/>
    

获取数据内容示例:

this.sex=(RadioGroup) super.findViewById(R.id.sex);
this.male=(RadioButton) super.findViewById(R.id.male);
this.female=(RadioButton) super.findViewById(R.id.female);
this.sex.setOnCheckedChangeListener(new OnCheckedChangeListenerImp());

private class OnCheckedChangeListenerImp implements OnCheckedChangeListener{

public void onCheckedChanged(RadioGroup group, int checkedId) {
String temp=null;
if(MainActivity.this.male.getId()==checkedId){
temp="男";
}
else if(MainActivity.this.female.getId()==checkedId){
temp="女";
}

RadioButton是android开发中常见的一种控件,而使用简单,通常与RadioGroup一起使用。RadioButton表示单个圆形单选框,而RadioGroup是可以容纳多个RadioButton的容器。

回答2:

首先获取单选按钮id,然后根据单选按钮id获取按钮值,然后通过相关的业务逻辑获取想要的数据值

回答3:

判断是否被选中:
//性别
if(rdo_m.isChecked())
xb=rdo_m.getText().toString().trim();
else
xb=rdo_n.getText().toString().trim();

回答4:

事件呗
上CSDN下载几本android教程看看就会了
基础的东东在网上的教程很容易就找到了