使用随机数字与OnClickListener Android

时间:2017-09-30 20:15:37

标签: java android

我正在尝试开发的程序有两个按钮,上面有随机数字,允许用户选择最大的按钮。它应该在每次猜测后重置数字,并保持正确猜测的分数,但我遇到的问题是OnClickListener我必须使我的两个int值最终从OnClickListener访问它们但是这意味着随机值不会改变。反正有这个让它起作用吗?

这是我的代码:

public class MainActivity extends AppCompatActivity {

    Button btn1;
    Button btn2;
    TextView textView1;
    int correctCount = 0;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        //retrive the Button and TetView objects
        btn1 = (Button) findViewById(R.id.btn1);
        btn2 = (Button) findViewById(R.id.btn2);
        textView1 = (TextView) findViewById(R.id.textView1);;

        Random randd = new Random();
         final int value = randd.nextInt(100)+1;
         final int value2 = randd.nextInt(100)+1;

        btn1.setText("" + value);
        btn2.setText("" + value2);
        textView1.setText("Correct = ");

        //register a listener for button clicks to be an anonymous
        //OnClickListener object with the onClick event handler
        btn1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if(value > value2){
                    btn1.setText("" + value);
                    btn2.setText("" + value2);
                    correctCount++;
                    textView1.setText("Correct = " + correctCount);
                }


            }
        });

        btn2.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if(value2 > value){
                    btn1.setText("" + value);
                    btn2.setText("" + value2);
                    correctCount++;
                    textView1.setText("Correct = " + correctCount);
                }


            }
        });

    }
}

3 个答案:

答案 0 :(得分:1)

您可以创建一个函数,例如:

   public void resetButtonNumbers(){
    Random randd = new Random();
    value = randd.nextInt(100)+1;
    value2 = randd.nextInt(100)+1;

    btn1.setText("" + value);
    btn2.setText("" + value2);
}

然后只需在每次点击时调用它。

btn2.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {
        if(value2 > value){
            correctCount++;
            textView1.setText("Correct = " + correctCount);
        }
          resetButtonNumbers();

    }
});

此外,您需要将值变量作为类成员:

public class MainActivity extends AppCompatActivity {
private int value;
private int value2;
}

答案 1 :(得分:-1)

您需要将变量创建为类范围。像这样:

public class MainActivity extends AppCompatActivity {
  private int value;
  private int value2;
}

现在您可以从OnClickListener访问它们了。请始终使您的变量更具可读性。因此,将变量命名为value,您可以使用mFirstRandom。之前的变量可以更改为

public class MainActivity extends AppCompatActivity {
  private int mFirstRandom;
  private int mSecondRandom;
}

答案 2 :(得分:-1)

您可以为两个按钮创建一个功能,此功能将获取每个按钮的当前值,并比较它是否大于另一个按钮。如果是,它将添加分数,如果不是,它将什么都不做。单击任何按钮后,每个按钮上的数字将重置。见functionDoCount

public class MainActivity extends AppCompatActivity {

    Button btn1;
    Button btn2;
    TextView textView1;
    int correctCount = 0;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        //retrive the Button and TetView objects
        btn1 = (Button) findViewById(R.id.btn1);
        btn2 = (Button) findViewById(R.id.btn2);
        textView1 = (TextView) findViewById(R.id.textView1);;

        Random randd = new Random();
        int value = randd.nextInt(100)+1;
        int value2 = randd.nextInt(100)+1;

        btn1.setText("" + value);
        btn2.setText("" + value2);
        textView1.setText("Correct = ");

        //register a listener for button clicks to be an anonymous
        //OnClickListener object with the onClick event handler
        btn1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                functionDoCount(1);
            }
        });

        btn2.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                functionDoCount(2);
            }
        });

    }

    void functionDoCount(int buttonIndex) {
        int currentValue = Integer.parseInt(btn1.getText().toString());
        int currentValue2 = Integer.parseInt(btn2.getText().toString());

        if(buttonIndex == 1) { // if first button is clicked
            if(currentValue > currentValue2){
                correctCount++;
                textView1.setText("Correct = " + correctCount);
            }
        } else { // if the other button is clicked
            if(currentValue2 > currentValue){
                correctCount++;
                textView1.setText("Correct = " + correctCount);
            }
        }

        Random randd = new Random();
        int value = randd.nextInt(100)+1;
        int value2 = randd.nextInt(100)+1;
        btn1.setText("" + value);
        btn2.setText("" + value2);
    }
}