我有Radiogroup并且里面有两个单选按钮。但是我无法获得两个单选按钮的字符串值。
这是我的代码: -
public void checkFieldsForEmptyValue(){
phonenumber=phone_number.getText().toString();
String password=Password.getText().toString();
RadioGroup radioGroup=(RadioGroup) findViewById(R.id.rg_lgoinscreen);
RadioButton radioButton_student=(RadioButton)findViewById(R.id.Radio_button_student);
RadioButton radioButton_teacher=(RadioButton)findViewById(R.id.Radio_button_teacher);
String radio_button_student=Integer.toString(radioButton_student.getId());
if(phonenumber.length() > 0 && password.length() > 0 && radio_button_student.length()>0)
{
Toast.makeText(this, "Else working", Toast.LENGTH_LONG).show();
login.setEnabled(true);
}
这是XML文件
<RadioGroup
android:id="@+id/rg_lgoinscreen"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="10dp"
android:layout_margin="1dp"
android:layout_below="@id/login_password"
android:orientation="horizontal"
>
<RadioButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:buttonTint="@color/white"
android:text="Student"
android:textColor="@color/white"
android:id="@+id/Radio_button_student"
android:layout_marginLeft="19dp"/>
<RadioButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="50dp"
android:buttonTint="@color/white"
android:text="Teacher"
android:textColor="@color/white"
android:id="@+id/Radio_button_teacher"/>
</RadioGroup>
答案 0 :(得分:0)
我不知道你为什么要用
Integer.toString(radioButton_student.getId())
你只需要单选按钮然后使用它:
radioButton_student.getText()
如果您需要检查radioButton是否已选中,请使用:
radioButton_student.isChecked()
答案 1 :(得分:0)
使用此代码
public void checkFieldsForEmptyValue(){
phonenumber=phone_number.getText().toString();
String password=Password.getText().toString();
RadioGroup radioGroup=(RadioGroup) findViewById(R.id.rg_lgoinscreen);
RadioButton radioButton_student=(RadioButton)findViewById(R.id.Radio_button_student);
RadioButton radioButton_teacher=(RadioButton)findViewById(R.id.Radio_button_teacher);
` int selectedId = radioGroup.getCheckedRadioButtonId();
radioButton = (RadioButton) findViewById(selectedId);
String radio_button_student=Integer.toString(radioButton.getText());
if(phonenumber.length() > 0 && password.length() > 0 && radio_button_student.length()>0)
{
Toast.makeText(this, "Else working", Toast.LENGTH_LONG).show();
login.setEnabled(true);
}