在这里,我正在尝试制作一个应用程序,其中我的 MainActivity 屏幕上有 4 个选项,用户应该只能在选择其中一个单选按钮时单击提交。(否则),这有助于我在提交时避免空指针异常选项。
============================= MainActivity.java ================== ==
package com.Xapp.radiobuttondemo;
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.widget.Button;
import android.widget.RadioGroup;
import android.widget.RadioGroup.OnCheckedChangeListener;
import android.widget.TextView;
public class MainActivity extends Activity {
RadioGroup radioGroup;
Button submit;
TextView tv;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
submit=(Button) findViewById(R.id.submit);
radioGroup=(RadioGroup) findViewById(R.id.radioOptions);
radioGroup.setOnCheckedChangeListener(new OnCheckedChangeListener()
{
public void onCheckedChanged(RadioGroup group, int checkedId) {
boolean is=group.isActivated(); //This must be something else,Please suggest
if(is==true)
{
submit.setEnabled(true);
}
else
{
submit.setEnabled(false);
}
}
});
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
}
=============================== activity_main.xml ================= ===========
<LinearLayout 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: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=".MainActivity" >
<RadioGroup
android:id="@+id/radioOptions"
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
<RadioButton
android:id="@+id/radioButton1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/option1" />
<RadioButton
android:id="@+id/radioButton2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/option2" />
<RadioButton
android:id="@+id/radioButton3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/option3" />
<RadioButton
android:id="@+id/radioButton4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/option4" />
</RadioGroup>
<Button
android:id="@+id/submit"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:enabled="false"
android:text="Submit" />
</LinearLayout>
======================================== strings.xml ======== ==========================
<string name="app_name">RadioButtonDemo</string>
<string name="action_settings">Settings</string>
<string name="hello_world">Select One</string>
<string name="option1">Red</string>
<string name="option2">Blue</string>
<string name="option3">Green</string>
<string name="option4">Pink</string>