0

如果我理解正确

    Random ran = new Random();
    String[] ButtonText = null;
    Resources res = getResources();
    ButtonText = res.getStringArray(R.array.ButtonText_array);
    String strRandom = ButtonText[ran.nextInt(ButtonText.length)];
    System.out.println("Random string is : "+strRandom);

是一种获取我的字符串数组项并将它们按随机顺序排列的方法,现在我想用 strRandom 中的单个项设置几个按钮的 setText。下面是一个按钮的setText

    Button gm1 = (Button) findViewById(R.id.gm1);
    gm1.setText();

但我不知道如何将 strRandom 项目放入 setText 部分,因为我不需要它显示我需要在此处更改的内容。

System.out.println("Random string is : "+strRandom);

4

2 回答 2

2

我真的不明白这个问题......

如果您只是询问如何将文本设置为随机字符串,请像使用println()语句一样进行操作,

gm1.setText(strRandom);

或者

gm1.setText(ButtonText[ran.nextInt(ButtonText.length)]);

顺便说一句:按照惯例,变量是在驼峰式中完成的,为类名保留 AllCaps。(例如 ButtonText 应该是 buttonText)。您会注意到 SO 格式化程序将 ButtonText 格式化为一个类,而不是一个数组。

于 2011-04-20T20:11:07.873 回答
0
gm1.setText((CharSequence)("Random string is : " + strRandom));

您需要从 String 转换为 CharSequence

于 2011-04-20T21:05:30.433 回答