我正在尝试在textView对象中显示一个字符串。
目前,我只得到字符串数组中的第一个字符串。

   public void onClick(View v) {
        Resources res = getResources();
        String[] hintsStr = res.getStringArray(R.array.hints);
        TextView hintShow = (TextView)findViewById(R.id.hintShow);
        int rndInd = (int) Math.random()*5;
        //hintShow.setText(rndInd);
        hintShow.setText(hintsStr[rndInd]);
        //System.out.print(rndInd);
    }

最佳答案

尝试生成随机数。-

Random rand = new Random();
int rndInd = rand.nextInt(5); // Will get a rand number between 0 and 4


另外,您应该实例化rand对象一次,作为onClick方法外部的实例变量。

07-27 19:21