菜鸟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()+"";
}
}