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

菜鸟Android学习01

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

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

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

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

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


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


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

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

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

       }

你可能感兴趣的:(Android)