如果用户单击“下一步”按钮而未选择选项,则必须烘烤一条消息,“请选择任何一个”,否则应转到下一个屏幕。我已经尝试过了,但是它不会转到下一个屏幕,而是显示吐司“请选择任何一个”和我的代码
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;
}
}
}
尝试使用:
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以外的任何值,您都将选择一个按钮。
错误条件:if (next.isSelected())
真实条件:如要确保必须选择选项:
boolean checked = ((RadioButton) view).isChecked();
您正在检查下一步按钮本身的选择状态。但是您要检查单选按钮的选择状态。因此,您必须将其指定为:
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();
}
条件将是这样
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
}
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();
}
});
}