按下每个按钮后显示随机字符串

时间:2015-12-04 22:13:41

标签: android

学习如何从数组中生成随机数。代码将在onCreate方法中选择一个随机字符串,但是当单击该按钮时,字符串没有变化。我是否以某种方式弄乱了变量的范围?我真的不知道这是错的。

public class MainActivity extends AppCompatActivity {
String[] qArray; //initialize qArray
private static final Random rgen = new Random(); //sets the new Random method to the keyword rgen

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

    qArray = getResources().getStringArray(R.array.qArray); //grabs string array in the XML file

    String q = qArray[rgen.nextInt(qArray.length)];//generates a random index of qArray to set to q

    TextView question = (TextView) findViewById(R.id.question); //sets the TextView to "question"

    question.setText(q); //sets the text in "question" to q



}

public void onClick(View view){
    Button button = (Button) view.findViewById(R.id.button_click);
    button.setOnClickListener(new View.OnClickListener(){
        @Override
    public void onClick(View view){

                 /*This should do the same thing as onCreate but each time                          
                   the button is pressed. I think the error is here.*/

            String q = qArray[rgen.nextInt(qArray.length)];
            TextView question = (TextView) findViewById(R.id.question);
            question.setText(q);

           }
        });
      }
    }

3 个答案:

答案 0 :(得分:2)

首先,你没有正确设置点击监听器,你做了onClick方法,这个方法从未被调用过。你也可以多次获得视图的引用,你应该这样做。

public class MainActivity extends AppCompatActivity {
    String[] qArray; //initialize qArray
    private static final Random rgen = new Random(); //sets the new Random     method to the keyword rgen

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

        qArray = getResources().getStringArray(R.array.qArray); //grabs string array in the XML file

        String q = qArray[rgen.nextInt(qArray.length)];//generates a random index of qArray to set to q

        TextView question = (TextView) findViewById(R.id.question); //sets the TextView to "question"

        question.setText(q); //sets the text in "question" to q
        Button button = (Button) view.findViewById(R.id.button_click);
        button.setOnClickListener(new View.OnClickListener(){
            @Override
            public void onClick(View view){
                 /*This should do the same thing as onCreate but each time                          
                   the button is pressed. I think the error is here.*/

                String q = qArray[rgen.nextInt(qArray.length)];
                question.setText(q);
           }
        });
    }
}

答案 1 :(得分:0)

您需要在xml中设置onClickListener(在这种情况下,您不需要在代码中设置侦听器),或者您需要在onCreate中设置侦听器。现在它被设置在一个名为onClick的函数中,它永远不会被调用。

答案 2 :(得分:0)

首先,你永远不会设置onClickListeren。你有一个方法可以添加一个,但你永远不会调用它。将onClick方法中的代码移至onCreate方法。这可能会解决您的问题。

如果没有,请不要进行随机决赛并将其添加到@Override onClick()

rgen = new Random(System.currentTimeMillis());

每次都会使用不同的种子