Android eclipse setText 的随机字符串数组?
如果我理解正确的话,
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
Button gm1 = (Button) findViewById(R.id.gm1);
gm1.setText();
但我不知道如何将 strRandom 项目放入 setText 部分,因为我不需要它显示我需要在此处更改的内容。
System.out.println("随机字符串为:"+strRandom);
If I understand correctly
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);
Is a way to take my string-array items and put them in random order and now I'm wanting to setText of several buttons with individual items from the strRandom. The following is for the setText of a button
Button gm1 = (Button) findViewById(R.id.gm1);
gm1.setText();
But I dont know how to put in the strRandom items into the setText part and since I dont need it displaying what do I need to alter here.
System.out.println("Random string is : "+strRandom);
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我真的不明白这个问题...
如果您只是问如何将文本设置为随机字符串,请像使用
println()
语句一样进行操作,或者
只是侧面注意:按照惯例,变量采用驼峰命名法,类名保留全部大写。 (例如,ButtonText 应该是buttonText)。您会注意到 SO 格式化程序将 ButtonText 格式化为类,而不是数组。
I really am not understanding the question...
If you're just asking how to set the text to a random string, do it just as you did with the
println()
statement,or
Just a side note: by convention, variables are done in camelCase, reserve AllCaps for class names. (e.g. ButtonText should be buttonText). You'll notice the SO formatter formats ButtonText as if it were a class, not an array.
您需要从 String 转换为 CharSequence
You need to cast from String to CharSequence