1

我正在尝试编写一个表格,并且有一次有 2 个单选按钮询问此人的性别。我想要求至少按下一个按钮,否则他们会收到祝酒词。它们出现在 activity_main 中,如下所示:

<RadioGroup 
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal">

        <RadioButton
            android:id="@+id/radioButton1"
            android:layout_width="wrap_content"
            android:layout_height="53dp"
            android:layout_weight="1.00"
            android:onClick="onRadioButtonClicked"
            android:text="@string/female" />

        <RadioButton
            android:id="@+id/radioButton2"
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:layout_weight="1.00"
            android:onClick="onRadioButtonClicked"
            android:text="@string/male" />
</RadioGroup>

以及MainActivity中的相关代码:

    RadioButton rb1, rb2;
    rb1 = (RadioButton) findViewById(R.id.radioButton1);
    rb2 = (RadioButton) findViewById(R.id.radioButton2);
    if(!rb1.isChecked() && !rb2.isChecked()){
        Toast.makeText(this, "Please select a gender.", Toast.LENGTH_SHORT).show();
        return;
    }

可能值得注意的是,我尝试只使用一个 & 来执行 if 语句,但单击单选按钮后我的应用程序仍然崩溃。

这是LogCat:

09-11 01:21:26.599: E/AndroidRuntime(821): FATAL EXCEPTION: main
09-11 01:21:26.599: E/AndroidRuntime(821): java.lang.IllegalStateException: Could not find a method onRadioButtonClicked(View) in the activity class com.example.myapplication.MainActivity for onClick handler on view class android.widget.RadioButton with id 'radioButton1'
09-11 01:21:26.599: E/AndroidRuntime(821):  at android.view.View$1.onClick(View.java:3620)
09-11 01:21:26.599: E/AndroidRuntime(821):  at android.view.View.performClick(View.java:4240)
09-11 01:21:26.599: E/AndroidRuntime(821):  at android.widget.CompoundButton.performClick(CompoundButton.java:100)
09-11 01:21:26.599: E/AndroidRuntime(821):  at android.view.View$PerformClick.run(View.java:17721)
09-11 01:21:26.599: E/AndroidRuntime(821):  at android.os.Handler.handleCallback(Handler.java:730)
09-11 01:21:26.599: E/AndroidRuntime(821):  at android.os.Handler.dispatchMessage(Handler.java:92)
09-11 01:21:26.599: E/AndroidRuntime(821):  at android.os.Looper.loop(Looper.java:137)
09-11 01:21:26.599: E/AndroidRuntime(821):  at android.app.ActivityThread.main(ActivityThread.java:5103)
09-11 01:21:26.599: E/AndroidRuntime(821):  at java.lang.reflect.Method.invokeNative(Native Method)
09-11 01:21:26.599: E/AndroidRuntime(821):  at java.lang.reflect.Method.invoke(Method.java:525)
09-11 01:21:26.599: E/AndroidRuntime(821):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:737)
09-11 01:21:26.599: E/AndroidRuntime(821):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
09-11 01:21:26.599: E/AndroidRuntime(821):  at dalvik.system.NativeStart.main(Native Method)
09-11 01:21:26.599: E/AndroidRuntime(821): Caused by: java.lang.NoSuchMethodException: onRadioButtonClicked [class android.view.View]
09-11 01:21:26.599: E/AndroidRuntime(821):  at java.lang.Class.getConstructorOrMethod(Class.java:423)
09-11 01:21:26.599: E/AndroidRuntime(821):  at java.lang.Class.getMethod(Class.java:787)
09-11 01:21:26.599: E/AndroidRuntime(821):  at android.view.View$1.onClick(View.java:3613)
09-11 01:21:26.599: E/AndroidRuntime(821):  ... 12 more

谢谢。

4

4 回答 4

6

创建一个方法onRadioButtonClicked,因为您已经设置onClickxml. 所以你必须有一个Activity可以处理点击的名称的方法。

方法

public void onRadioButtonClicked(View v){
    // Your code on click
}
于 2013-09-11T05:35:28.750 回答
1

试试下面的代码:

public class MainActivity extends Activity {

  private RadioGroup radioSexGroup;
  private RadioButton radioSexButton;
  private Button btnDisplay;

  @Override
  public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    addListenerOnButton();

  }

  public void addListenerOnButton() {

    radioSexGroup = (RadioGroup) findViewById(R.id.radioSex);
    btnDisplay = (Button) findViewById(R.id.btnDisplay);

    btnDisplay.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {

                // get selected radio button from radioGroup
            int selectedId = radioSexGroup.getCheckedRadioButtonId();

                        if(selected==-1)
                             Toast.makeText(MyAndroidAppActivity.this,
                "Please Select at least one Gender", Toast.LENGTH_SHORT).show();
            // find the radiobutton by returned id
                radioSexButton = (RadioButton) findViewById(selectedId);

            Toast.makeText(MyAndroidAppActivity.this,
                radioSexButton.getText(), Toast.LENGTH_SHORT).show();

        }

    });

  }
}
于 2013-09-11T05:37:08.610 回答
0

你定义了吗

public void onRadioButtonClicked(View v)

作为你的线

android:onClick="onRadioButtonClicked"

暗示?

于 2013-09-11T05:36:29.747 回答
0

在 xml 文件 onRadioButtonClicked 中定义属性 onClick 并在 java 文件中访问此方法,

public void onRadioButtonClicked(View view) {
    switch (view.getId()) {
    case R.id.YOUR_ID_RADIO_BUTTON:
                    // write here some one code
        break;

    default:
        break;
    }
}
于 2013-09-11T05:38:52.337 回答