使用方法获取多个随机字符串以在 XML 中显示为文本
我有一个大约 10 个字符串的列表,它们都有不同的谚语(格言)。我怎样才能随机显示它们以供用户在 XML 上阅读?
我有一个 math().random;生成 1 到 10 之间的随机 #。当达到该数字时,我使用一个简单的 switch/case 在 xml 文件中向用户显示一句话。如何在 xml 中显示预定义字符串?
即:
private void randomIdioms(){
int saying = math.random()*10;
if (saying = 3){
//THe code I am looking for that displays a string on a view in the XML file
}else if (saying = 2){
}
等等....
谢谢!
I have a list of about 10 strings that all have different sayings (adages). How can I go about randomly displaying them for the user to read on an XML?
I have a math().random; that generates a random # between 1 and 10. When that number is hit, I use a simple switch/case that displays a saying to the user in an xml file. How do I go about displaying that predefined string in the xml?
ie:
private void randomIdioms(){
int saying = math.random()*10;
if (saying = 3){
//THe code I am looking for that displays a string on a view in the XML file
}else if (saying = 2){
}
etc....
Thanks!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
不确定在 xml 中显示是什么意思
,但我建议这样做
1)将所有习语存储在列表或数组列表中
2)将其洗牌
http://www.java-examples.com/shuffle-elements-java-arraylist-example
3) 显示您想要显示的前 n 个。
编辑:
现在我重新阅读您的问题,您可能正在等待仅显示 1 个字符串。在这种情况下
1)将您的习语存储在
tab[]
或arrayList
中2)执行
index = Math.radom()*sizeOfCollection;
(对此感到抱歉)
关于显示,我仍然没有得到 xml 部分
,说你的视图中有一个 xml 布局中的 TextView,那么你想要做的是
你可以查看 this 用于布局和更改布局内容
希望它对
杰森有帮助
Not sure what you mean by display in a xml
but I would recommend this
1)store all your Idioms in a List or array list
2) shuffle it
http://www.java-examples.com/shuffle-elements-java-arraylist-example
3) display the n first ones you want to show.
Edit:
now I re read your question it occurs that your maybe waiting to display just 1 string. in that case
1)store your idioms in a
tab[]
orarrayList
2)do a
index = Math.radom()*sizeOfCollection;
(sorry about that)
regarding the display I still don't get the xml part
say you have in your view a TextView in your xml layout, then what you want to do is
you can look at this for layout and changing layout content
hope it helps
jason