【问题标题】:Recognize second click event in android识别android中的第二次点击事件
【发布时间】:2016-10-19 16:52:16
【问题描述】:

我正在编写我的第一个 Android 应用程序。这是一个测验应用程序,其中显示一个带有 4 个答案选项的问题。

我想在单击其中一个按钮后立即显示正确答案,然后在再次单击该按钮时显示下一个问题。


此时正确答案和下一题同时显示。

public class ACEInhibitors extends Fragment implements View.OnClickListener {
List<Question> quesList;
int score = 0;
int qid = 0;
Question currentQ;
TextView txtQuestion;
RadioButton rda, rdb, rdc, rdd;
Button checkBtn;

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    /** Inflating the layout for this fragment **/
    View view = inflater.inflate(R.layout.question, null);

    DatabaseHelper db = new DatabaseHelper(getActivity());

    quesList = db.getAllACEQuestions();
    currentQ = quesList.get(qid);
    txtQuestion = (TextView) view.findViewById(R.id.textView);
    rda = (RadioButton) view.findViewById(R.id.radio0);
    rdb = (RadioButton) view.findViewById(R.id.radio1);
    rdc = (RadioButton) view.findViewById(R.id.radio2);
    rdd = (RadioButton) view.findViewById(R.id.radio3);

    rda.setOnClickListener(this);
    rdb.setOnClickListener(this);
    rdc.setOnClickListener(this);
    rdd.setOnClickListener(this);

    setQuestionView();

    return view;
}

@Override
public void onClick(View view) {
    Fragment fragment = null;
    switch (view.getId()) {
        case R.id.radio0:
            // Happening for the first click
            // Set colors according to correct answer
            rda.setBackgroundColor(Color.RED);
            rdb.setBackgroundColor(Color.RED);
            rdc.setBackgroundColor(Color.RED);
            rdd.setBackgroundColor(Color.RED);

            if (currentQ.getANSWER().equals(currentQ.getOPTA())) {
                rda.setBackgroundColor(Color.GREEN);
            } else if (currentQ.getANSWER().equals(currentQ.getOPTB())) {
                rdb.setBackgroundColor(Color.GREEN);
            } else if (currentQ.getANSWER().equals(currentQ.getOPTC())) {
                rdc.setBackgroundColor(Color.GREEN);
            } else if (currentQ.getANSWER().equals(currentQ.getOPTD())) {
                rdd.setBackgroundColor(Color.GREEN);
            }

            // Supposed to happen for the second click
            // Display the next question
            currentQ = quesList.get(qid);
            setQuestionView();
            break;
    }
}

private void setQuestionView() {
    txtQuestion.setText(currentQ.getQUESTION());
    rda.setText(currentQ.getOPTA());
    rdb.setText(currentQ.getOPTB());
    rdc.setText(currentQ.getOPTC());
    rdd.setText(currentQ.getOPTD());
    qid++;
}

public void replaceFragment(Fragment newFragment) {
    FragmentTransaction transaction = getFragmentManager().beginTransaction();
    transaction.replace(R.id.fragment_container, newFragment);
    transaction.commit();
}

}


我对android和java编程真的很陌生,非常感谢任何帮助!

【问题讨论】:

    标签: java android sqlite android-fragments


    【解决方案1】:

    执行以下步骤以获得所需的结果

    1. 最初添加一个按钮,可见性消失。

    2. 现在将来自 onClick 的 setQuestionView() 调用替换为 button.setVisibility(View.VISIBLE);

    3. 为按钮添加点击监听器。

    4. 点击按钮再次调用setQuestionView()

    【讨论】:

    • 这是一个绝妙的主意!谢谢!我会尽快接受你的回答!
    猜你喜欢
    • 2017-07-03
    • 1970-01-01
    • 2022-01-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-10-24
    • 1970-01-01
    相关资源
    最近更新 更多