Android获得文本框 单选框 多选框的值

这篇博客适合Android初学者,详细介绍了如何在Android应用中获取文本框、单选框和多选框的用户输入值。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

菜鸟Android学习01

如何获得文本框 单选框 多选框的值

> 程序代码分别如下所示
@获得文本框的值
private EditText emailEdit ;
    '''在onCreate方法中通过ID获得控键对象'''
    emailEdit = (EditText)findViewById(R.id.emailEdit);
    '''使用getText()方法得到控键的值'''
    str = emailEdit.getText()+"";

@获得单选框的的值
'''1-在.xml布局文件中使用RadioGroup控键生成单选框 程序代码如下所示 '''
<RadioGroup 
         android:id="@+id/sexRadioButton"
         android:layout_width="wrap_content"
         android:layout_height="wrap_content"
        android:layout_weight="4"
        android:orientation="horizontal"

        ><RadioButton
             android:id="@+id/radioButton1"
             android:layout_width="wrap_content"
             android:layout_height="wrap_content"
              **android:checked="true"**
             android:text="@string/sex01" 
             />
          <RadioButton
             android:id="@+id/radioButton2"
             android:layout_width="wrap_content"
             android:layout_height="wrap_content"

             android:text="@string/sex02" 
             />
    </RadioGroup>


'''2-在.xml布局文件中使用RadioGroup控键生成单选框 程序代码如下所示 '''


'''声明一个RadioGroup对象和一个 RadioButton对象'''
    private RadioGroup  sexRadioGroup ;
    private RadioButton  radioButton ;

 radioButton =(RadioButton)findViewById(sexRadioGroup.getCheckedRadioButtonId());
       str = radioButton.getText()+"";
@获得多选框的的值
'''1-在.xml布局文件中使用CheckBox控键生成多选框  '''
<CheckBox
            android:id="@+id/CheckBox01"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="@string/city01" />
        <CheckBox
            android:id="@+id/CheckBox02"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="@string/city02" />
        <CheckBox
            android:id="@+id/CheckBox03"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="@string/city03" />

'''在.java 文件中声明一个RadioGroup对象和一个 RadioButton对象'''
    private CheckBox  cb ;

'''获得多选框的值'''
       int [] arr = new int[] {R.id.CheckBox01,R.id.CheckBox02,R.id.CheckBox03};
       for(int i = 0 ;i<arr.length;i++){
          cb = (CheckBox) findViewById(arr[i]);
          if(cb.isChecked()){

              str+= cb.getText()+"";
          }

       }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值