我正在尝试在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
方法外部的实例变量。
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句