首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Android问答消息提示

Android问答消息提示
EN

Stack Overflow用户
提问于 2016-01-19 15:11:14
回答 1查看 53关注 0票数 1

嗨,我正在创建一个测试应用程序。我想在我的提交按钮中添加一个函数,如果用户没有在无线电按钮中选择任何答案,就会有一条消息:“请选择一个答案”,这是代码

代码语言:javascript
复制
  public void onClickNext(View view) {
    String level = getIntent().getExtras().getString("level");
    DbHelper db = new DbHelper(this);
    db.getQuestByLevel(level, qnum);

    RadioGroup grp = (RadioGroup) findViewById(R.id.questionAndAnswers);
    RadioButton answer = (RadioButton) findViewById(grp.getCheckedRadioButtonId());
    if (corAnswer != null && corAnswer.equalsIgnoreCase((String) answer.getText())) {
        score++;
        Log.d("answer", "Your score" + score);
    }
    if (qnum <= 5) {

    }
    else {
        Intent intent = new Intent(QuizActivity.this, ResultActivity.class);
        Bundle b = new Bundle();
        b.putInt("score", score);
        intent.putExtras(b);
        startActivity(intent);
        finish();
    }

    txtQuestion.setText(db.question);
    rda.setText(db.optionA);
    rdb.setText(db.optionB);
    rdc.setText(db.optionC);
    rdd.setText(db.optionD);
    corAnswer = db.answer;
    qnum++;
    rdgrp.clearCheck();
}

}

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2016-01-19 15:36:09

在下一行

代码语言:javascript
复制
RadioButton answer = (RadioButton) findViewById(grp.getCheckedRadioButtonId());

如果没有选中RadioButtongetCheckedRadioButtonId()将返回"-1“,并且由于没有使用该id的视图,”应答“将是null

这样您就可以执行以下操作

代码语言:javascript
复制
if (answer == null)
{
    Toast.makeText(MyActivity.this, "select an answer please", Toast.LENGTH_SHORT).show();

    // nothing more to do here so 
    return;
}
if (corAnswer!= null && corAnswer.equalsIgnoreCase((String) answer.getText())
{
    // continue as before
}
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/34880290

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档