gpt4 book ai didi

java - android中识别第二次点击事件

转载 作者:行者123 更新时间:2023-12-01 09:21:34 25 4
gpt4 key购买 nike

我正在编写我的第一个 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 编程非常陌生,非常感谢任何形式的帮助!

最佳答案

执行以下步骤即可达到预期结果

  1. 最初添加一个按钮,但不可见。

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

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

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

关于java - android中识别第二次点击事件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40137326/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com