in this program I use radiobuttons. Currently I've some problems with the button number 2 (button2
), in fact when it is pressed nothing happens!
在这个程序中我使用radiobuttons。目前我在按钮编号2(按钮2)上遇到了一些问题,实际上按下时没有任何反应!
When I press button2
it should show the name of each radio button in "TextView" (while button1
is used to clear choosing radio buttons). Can anyone please help me or give me any kind of tips? Thanks.
当我按下button2时,它应该在“TextView”中显示每个单选按钮的名称(而button1用于清除选择单选按钮)。有谁可以帮助我或给我任何提示?谢谢。
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.TextView;
public class RadiobuttonActivity extends Activity {
Button button1;
Button button2;
TextView textView1;
RadioButton r1;
RadioButton r2;
RadioButton r3;
RadioGroup radioGroup1;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
button1 = (Button) findViewById(R.id.button1);
button2 = (Button) findViewById(R.id.button2);
textView1 = (TextView) findViewById(R.id.textView1);
radioGroup1 = (RadioGroup) findViewById(R.id.radioGroup1);
r1 = (RadioButton) findViewById(R.id.r1);
r2 = (RadioButton) findViewById(R.id.r2);
r3 = (RadioButton) findViewById(R.id.r3);
button1.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
radioGroup1.clearCheck();
textView1.setText("AllUnChecked");
}
});
button2.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
int selectedid = radioGroup1.getCheckedRadioButtonId();
switch (selectedid) {
case 0:
textView1.setText("Red");
break;
case 1:
textView1.setText("Green");
break;
case 2:
textView1.setText("Black");
break;
}
}
});
}
}
this is the main.xml :
这是main.xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<RadioGroup
android:id="@+id/radioGroup1"
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
<RadioButton
android:id="@+id/r1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:checked="true"
android:text="Red" />
<RadioButton
android:id="@+id/r2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Green" />
<RadioButton
android:id="@+id/r3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Black" />
</RadioGroup>
<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="AllUnchecked" />
<Button
android:id="@+id/button2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Witch radio button is ckecked?" />
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Text View"
android:textAppearance="?android:attr/textAppearanceLarge" />
</LinearLayout>
and this is main.xml file:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<RadioGroup
android:id="@+id/radioGroup1"
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
<RadioButton
android:id="@+id/r1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:checked="true"
android:text="Red" />
<RadioButton
android:id="@+id/r2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Green" />
<RadioButton
android:id="@+id/r3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Black" />
</RadioGroup>
<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="AllUnchecked" />
<Button
android:id="@+id/button2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Witch radio button is ckecked?" />
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Text View"
android:textAppearance="?android:attr/textAppearanceLarge" />
1 个解决方案
#1
1
As explained in this SO post
正如本文所述
Instead of use:
而不是使用:
int selectedid = radioGroup1.getCheckedRadioButtonId();
Use this:
int selectedid = radioGroup1.indexOfChild(findViewById(radioGroup1.getCheckedRadioButtonId()));
#1
1
As explained in this SO post
正如本文所述
Instead of use:
而不是使用:
int selectedid = radioGroup1.getCheckedRadioButtonId();
Use this:
int selectedid = radioGroup1.indexOfChild(findViewById(radioGroup1.getCheckedRadioButtonId()));