Android如果点击了按钮,则不更新分数

时间:2017-01-08 12:36:02

标签: android android-fragments if-statement

我正在制作一个测验应用。如果用户输入了正确答案,则他们的分数为+1。 但是,我想这样做,如果点击了一个提示按钮,那么他们就不会更新他们的分数。

我的代码现在看起来如下

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);
            }
        });

如果我应该写下什么声明呢?

3 个答案:

答案 0 :(得分:1)

只需在单击提示按钮后添加一个更改为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;

所以下一个问题再次出现错误。

答案 1 :(得分:1)

使用boolean标志,例如:

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仍为假的情况下才会奖励该点:

if (!hintUsed) { 
    ((Play) getActivity()).updateScore(1);
}

答案 2 :(得分:0)

if (hintv.getVisibility() == View.INVISIBLE || hintv.getVisibility() == View.GONE)

这表明仅当TextView不可见时才会出现。