android - True and False button update questions and answer
问题描述
Hi doing a simple test for concussion. Has a textView and then a true and false button, the more true answers results in a not diagnosed result. I want each click on either the true or false button to update text view to next question. The false answer seems to do nothing! Is this a good way to do it or should I be trying something else, Thanks MainActivity
public class MainActivity extends AppCompatActivity implements View.OnClickListener {
int[] stringIdList = {R.string.Q1, R.string.Q2, R.string.Q3, R.string.Q4, R.string.Q5};
int stringListCounter = 0;
TextView text1;
int concussued = 0;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button button = findViewById(R.id.button);
text1 = findViewById(R.id.text1);
button.setOnClickListener(this);
Button negative=findViewById(R.id.button3);
negative.setOnClickListener(this);
}
@SuppressLint("SetTextI18n")
public void onClick(@NonNull View v) {
int id = v.getId();
if (id == R.id.button && stringListCounter < stringIdList.length - 1) {
stringListCounter++;
text1.setText(stringIdList[stringListCounter]);
}
concussued = concussued - 1;
}
public void onClick2(@NonNull View v) {
int id = v.getId();
if (id == R.id.button3 && stringListCounter < stringIdList.length - 1) {
stringListCounter++;
text1.setText(stringIdList[stringListCounter]);
}
concussued = concussued + 1;
}
public void onClick3(View v) {
Button diagnose = findViewById(R.id.button2);
diagnose.setOnClickListener(this);
findViewById(R.id.text1);
if (concussued >= 2) {
text1.setText("Concussed!");
} else {
text1.setText("Not concussed");
}
}
}
MainActivityXML
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<TextView
android:id="@+id/text1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="onClick"
android:text=""
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.498"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.176" />
<Button
android:id="@+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="308dp"
android:onClick="onClick"
android:text="True"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<Button
android:id="@+id/button2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="167dp"
android:layout_marginLeft="167dp"
android:layout_marginTop="196dp"
android:onClick="onClick3"
android:text="Diagnose"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/button" />
<Button
android:id="@+id/button3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="33dp"
android:onClick="onClick2"
android:text="False"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/button" />
</androidx.constraintlayout.widget.ConstraintLayout>
解决方案
您实现的接口View.OnClickListener
只有一种方法可以实现onClick
:
/**
* Interface definition for a callback to be invoked when a view is clicked.
*/
public interface OnClickListener {
/**
* Called when a view has been clicked.
*
* @param v The view that was clicked.
*/
void onClick(View v);
}
您的方法onClick2
并且onClick3
不是此接口的一部分,因此不会调用它们。False
单击按钮时调用该onClick
方法。您可以在此处放置一个断点并对其进行调试。
要解决此问题,您可以使用switch
语句:
@SuppressLint("SetTextI18n")
public void onClick(@NonNull View v) {
switch(v.getId()) {
case R.id.button:
trueButtonClicked();
break;
case R.id.button2:
diagnose();
break;
case R.id.button3:
falseButtonClicked();
break;
}
}
public void trueButtonClicked() {
if (stringListCounter < stringIdList.length - 1) {
stringListCounter++;
text1.setText(stringIdList[stringListCounter]);
}
concussued = concussued - 1;
}
public void falseButtonClicked() {
if (stringListCounter < stringIdList.length - 1) {
stringListCounter++;
text1.setText(stringIdList[stringListCounter]);
}
concussued = concussued + 1;
}
public void diagnose() {
if (concussued >= 2) {
text1.setText("Concussed!");
} else {
text1.setText("Not concussed");
}
}
推荐阅读
- python - 使用python根据前后字符提取部分文本(html)文件
- java - Camel 应用程序重新处理 Kinesis 记录
- python - 使用 mmap 和空文件的 Python 共享内存
- java - Hibernate Validation 6.0 ListValueExtractor.extractValues 似乎对大型列表的性能很差
- python - PyWin32 不包括从 PST 读入的所有电子邮件上的一个特定标记实例
- php - 在 php 文件中找不到用于获取 api 的 Swagger 类
- css - 如何并排放置两个输入>
- windows-10 - 正则表达式查找文件名中 2 个连字符之间的数据
- javascript - 反向对象解构的术语是什么?
- splunk - 在 splunk 查询中结合 2 个索引的结果