我尝试使用单选按钮组窗口小部件在我的应用程序中选择性别。现在就是这个。用户将选择性别,并且在单击按钮时,将引导用户进行相应的活动。我写了代码。第二个问题是我的程序什么也没做,虽然我在if部分启动了新的活动。我调试了我的程序,看看if条件是否满足在我看来是这样的。这是xml和.java文件
public class Gender extends Activity{
private RadioGroup radioSexGroup;
private RadioButton radioSexButton;
private Button btnDisplay;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.gender_asking);
addListenerOnButton();
}
public void addListenerOnButton() {
radioSexGroup = (RadioGroup) findViewById(R.id.radioSex);
btnDisplay = (Button) findViewById(R.id.gender_button);
btnDisplay.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
//****************************
int selectedId = radioSexGroup.getCheckedRadioButtonId();
// find the radiobutton by returned id
radioSexButton = (RadioButton) findViewById(selectedId);
if("Iam a Guy!".equals(radioSexButton.getText()))
{
Intent male_activity = new Intent(Gender.this, Meter.class);
startActivity(male_activity);
}
if("Iam a Girl!".equals(radioSexButton.getText()))
{
Intent male_activity = new Intent(Gender.this, For_Girl.class);
startActivity(male_activity);
}
//********************************
}
});
}
}
和关联的xml是:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@drawable/hyu_bg"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".KissingMeter" >
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="34dp"
android:text="Tell Us Your Gender!"
android:textSize="30dp" />
<RadioGroup
android:id="@+id/radioSex"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/gender_button"
android:layout_below="@+id/textView1"
android:layout_marginTop="30dp" >
<RadioButton
android:id="@+id/radioMale"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="@+id/radioFemale"
android:layout_alignLeft="@+id/radioFemale"
android:layout_marginBottom="34dp"
android:checked="true"
android:text="Iam a Guy!" />
<RadioButton
android:id="@+id/radioFemale"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="@+id/gender_button"
android:layout_centerHorizontal="true"
android:layout_marginBottom="44dp"
android:text="Iam a Girl!" />
</RadioGroup>
<Button
android:id="@+id/gender_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/radioSex"
android:layout_centerHorizontal="true"
android:layout_marginTop="18dp"
android:text="Lol" />
</RelativeLayout>
答案 0 :(得分:0)
首先,在这种情况下,不要依赖View
的文本。如果您决定稍后更改它,那么它将产生问题。最有可能的是,您不会更改它的id
,所以只需使用它。把它改成这样的东西
btnDisplay.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v)
{
int selectedId = radioSexGroup.getCheckedRadioButtonId(); / get the id
switch (selectedId) // switch on the button selected
{
case R.id.radioMale:
Intent male_activity = new Intent(Gender.this, Meter.class);
startActivity(male_activity);
break;
case R.id.radioFemale:
Intent male_activity = new Intent(Gender.this, For_Girl.class);
startActivity(male_activity);
break;
default:
Toast.makeText(v.getContext(), "No gender selected",
Toast.LENGTH_SHORT).show();
break;
}
//********************************
}
});