如果点击按钮,Android不会更新分数
我正在制作一个测验应用程序。如果用户输入了正确答案,他们的分数为+1。 然而,我想这样做,如果一个提示按钮被点击,那么他们不会得到他们的分数更新 我的代码现在看起来如下所示如果点击按钮,Android不会更新分数,android,android-fragments,if-statement,Android,Android Fragments,If Statement,我正在制作一个测验应用程序。如果用户输入了正确答案,他们的分数为+1。 然而,我想这样做,如果一个提示按钮被点击,那么他们不会得到他们的分数更新 我的代码现在看起来如下所示 public class FragmentTwo extends Fragment { private EditText userAnswer; @Override public View onCreateView(LayoutInflater inflater, ViewGroup conta
public class FragmentTwo extends Fragment {
private EditText userAnswer;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View v = inflater.inflate(R.layout.fragment_fragment_two, null);
userAnswer = (EditText)v.findViewById(R.id.editText);
final TextView tv = (TextView)v.findViewById(R.id.showCorrect);
final TextView hintv = (TextView)v.findViewById(R.id.textHint);
//final int a;
final Button submit = (Button)v.findViewById(R.id.submitBtn1);
submit.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
String theAnswer = (userAnswer.getText().toString());
if (theAnswer.equalsIgnoreCase("Greece")) {
if (hintv.isEnabled()) { //Doesn't work
((Play) getActivity()).updateScore(1);
}
tv.setText("Correct!");
} else {
tv.setText("Wrong");
}
submit.setEnabled(false);
}
});
final Button skip = (Button)v.findViewById(R.id.skipBtn);
skip.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
submit.setEnabled(false);
}
});
final Button hint = (Button)v.findViewById(R.id.hintBtn);
hint.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
hintv.setText("The capital is Athens \n The country is in Europe \n It starts with G... ");
hint.setEnabled(false);
}
});
我应该改写if语句吗?只需添加一个布尔变量,单击提示按钮后该变量将变为true
private EditText userAnswer;
private boolean isHintButtonClicked = false;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View v = inflater.inflate(R.layout.fragment_fragment_two, null);
userAnswer = (EditText)v.findViewById(R.id.editText);
final TextView tv = (TextView)v.findViewById(R.id.showCorrect);
final TextView hintv = (TextView)v.findViewById(R.id.textHint);
//final int a;
final Button submit = (Button)v.findViewById(R.id.submitBtn1);
submit.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
String theAnswer = (userAnswer.getText().toString());
if (theAnswer.equalsIgnoreCase("Greece")) {
if (!isHintButtonClicked) {
((Play) getActivity()).updateScore(1);
}
tv.setText("Correct!");
} else {
tv.setText("Wrong");
}
isHintButtonClicked = false;
submit.setEnabled(false);
}
});
final Button skip = (Button)v.findViewById(R.id.skipBtn);
skip.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
submit.setEnabled(false);
}
});
final Button hint = (Button)v.findViewById(R.id.hintBtn);
hint.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
isHintButtonClicked = true;
hintv.setText("The capital is Athens \n The country is in Europe \n It starts with G... ");
hint.setEnabled(false);
}
});
当然,别忘了加上
isHintButtonClicked = false;
因此,对于下一个问题,它将再次为false。只需添加一个布尔变量,在单击提示按钮后该变量将变为true
private EditText userAnswer;
private boolean isHintButtonClicked = false;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View v = inflater.inflate(R.layout.fragment_fragment_two, null);
userAnswer = (EditText)v.findViewById(R.id.editText);
final TextView tv = (TextView)v.findViewById(R.id.showCorrect);
final TextView hintv = (TextView)v.findViewById(R.id.textHint);
//final int a;
final Button submit = (Button)v.findViewById(R.id.submitBtn1);
submit.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
String theAnswer = (userAnswer.getText().toString());
if (theAnswer.equalsIgnoreCase("Greece")) {
if (!isHintButtonClicked) {
((Play) getActivity()).updateScore(1);
}
tv.setText("Correct!");
} else {
tv.setText("Wrong");
}
isHintButtonClicked = false;
submit.setEnabled(false);
}
});
final Button skip = (Button)v.findViewById(R.id.skipBtn);
skip.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
submit.setEnabled(false);
}
});
final Button hint = (Button)v.findViewById(R.id.hintBtn);
hint.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
isHintButtonClicked = true;
hintv.setText("The capital is Athens \n The country is in Europe \n It starts with G... ");
hint.setEnabled(false);
}
});
当然,别忘了加上
isHintButtonClicked = false;
因此,对于下一个问题,它将再次为false。使用
布尔标记,例如:
private boolean hintUsed;
单击提示按钮后,将其设置为true:
hint.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
hintv.setText("The capital is Athens \n The country is in Europe \n It starts with G... ");
hint.setEnabled(false);
hintUsed = true;
}
});
然后,在检查答案时,仅当hintUsed
仍然为false时,才给予分数:
if (!hintUsed) {
((Play) getActivity()).updateScore(1);
}
使用布尔
标志,例如:
private boolean hintUsed;
单击提示按钮后,将其设置为true:
hint.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
hintv.setText("The capital is Athens \n The country is in Europe \n It starts with G... ");
hint.setEnabled(false);
hintUsed = true;
}
});
然后,在检查答案时,仅当hintUsed
仍然为false时,才给予分数:
if (!hintUsed) {
((Play) getActivity()).updateScore(1);
}