如果用户在没有选择选项的情况下单击“下一步”按钮,则必须提供一条消息,“请选择任何一个”,否则它应该转到下一个屏幕。我已经尝试但它不会进入下一个屏幕,相反它显示吐司“请选择任何一个”和我的代码
public class Question1 extends Activity implements OnClickListener {
Button vid, next;
Typeface tp;
TextView tv;
RadioGroup rg;
RadioButton rb;
Button b;
SharedPreferences sp;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.question1);
sp = getSharedPreferences("AppFile1", 0);
tv = (TextView) findViewById(R.id.tvQuestion1);
rg = (RadioGroup) findViewById(R.id.radioGroup1);
vid = (Button) findViewById(R.id.buttonQuestion1VIdeo);
next = (Button) findViewById(R.id.buttonQuestion1Next);
vid.setOnClickListener(this);
next.setOnClickListener(this);
}
@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.buttonQuestion1VIdeo:
Intent i = new Intent(Question1.this, VideoActivity.class);
startActivity(i);
break;
case R.id.buttonQuestion1Next:
if (next.isSelected()) {
int selectedId = rg.getCheckedRadioButtonId();
rb = (RadioButton) findViewById(selectedId);
String s = rb.getText().toString();
SharedPreferences.Editor ed = sp.edit();
ed.putString("q1", s);
ed.commit();
Intent ia = new Intent(Question1.this, Question2.class);
startActivity(ia);
} else {
Toast.makeText(getApplicationContext(), "Please Select Answer", 0).show();
}
break;
default:
break;
}
}
}
答案 0 :(得分:4)
尝试:
if (rg.getCheckedRadioButtonId()!=-1) {
int selectedId = rg.getCheckedRadioButtonId();
rb = (RadioButton) findViewById(selectedId);
String s = rb.getText().toString();
SharedPreferences.Editor ed = sp.edit();
ed.putString("q1", s);
ed.commit();
Intent ia = new Intent(Question1.this, Question2.class);
startActivity(ia);
} else {
Toast.makeText(getApplicationContext(), "Please Select Answer", 0).show();
}
文档说如果没有选中任何按钮,getCheckedRadioButtonId()会返回-1
。
返回此组中所选单选按钮的标识符。选择空时,返回的值为-1。
因此,如果您从该函数获得除-1以外的任何值,则您有一个选定的按钮。
答案 1 :(得分:3)
错误的情况:if (next.isSelected())
真实条件:您想确保选项必须选择:
boolean checked = ((RadioButton) view).isChecked();
答案 2 :(得分:3)
您正在检查条件下“下一步”按钮的选择状态。 但是你想检查单选按钮选择状态。 所以你必须把它作为:
if (rg.getCheckedRadioButtonId()!=-1) {
int selectedId = rg.getCheckedRadioButtonId();
rb = (RadioButton) findViewById(selectedId);
String s = rb.getText().toString();
SharedPreferences.Editor ed = sp.edit();
ed.putString("q1", s);
ed.commit();
Intent ia = new Intent(Question1.this, Question2.class);
startActivity(ia);
} else {
Toast.makeText(getApplicationContext(), "Please Select Answer", 0).show();
}
答案 3 :(得分:0)
条件将是这样的
rb1 = (RadioButton)findViewById(R.id.rb1);
rb2 = (RadioButton)findViewById(R.id.rb2);
rb3 = (RadioButton)findViewById(R.id.rb3);
if(rb1.isChecked()==false && rb2.isChecked()==false && rb3.isChecked()==false)
{
// Toast message is here
}
else
{
// Intent code is here
}
答案 4 :(得分:0)
RadioGroup rad_grp;
Button clear,submit;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
clear = findViewById(R.id.clear);
submit = findViewById(R.id.submit);
rad_grp = findViewById(R.id.rad_grp);
rad_grp.clearCheck();
rad_grp.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
@SuppressLint("ResourceType")
@Override
public void onCheckedChanged(RadioGroup group, int checkedId) {
RadioButton rb = group.findViewById(checkedId);
if (null != rb && checkedId > -1)
{
Toast.makeText(MainActivity.this,rb.getText(), Toast.LENGTH_SHORT).show();
}
}
});
clear.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
rad_grp.clearCheck();
}
});
submit.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
RadioButton rb = rad_grp.findViewById(rad_grp.getCheckedRadioButtonId());
Toast.makeText(MainActivity.this,""+rb.getText().toString(),Toast.LENGTH_SHORT).show();
}
});
}
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".MainActivity">
<RadioGroup
android:id="@+id/rad_grp"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<RadioButton
android:id="@+id/male"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Male"/>
<RadioButton
android:id="@+id/female"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Female"/>
<RadioButton
android:id="@+id/other"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Other"/>
</RadioGroup>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="20dp">
<Button
android:id="@+id/clear"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Clear"
android:layout_marginLeft="20dp"/>
<Button
android:id="@+id/submit"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Submit"
android:layout_marginLeft="20dp"/>
</LinearLayout>
</LinearLayout>