如果用户单击“下一步”按钮而不选择选项,则必须显示一条消息,“请选择任何一个”,否则它应该转到下一个屏幕。我已经尝试过,但它不会进入下一个屏幕,而是显示吐司“请选择任何一个”和我的代码
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() http://developer.android.com/reference/android/widget/RadioGroup.html#getCheckedRadioButtonId%28%29回报-1
.
返回该组中所选单选按钮的标识符。选择空时,返回值为-1。
因此,如果该函数中有除 -1 之外的任何值,则您有一个选定的按钮。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)