字符串用法为TextView ID(String usage as TextView ID)

系统教程 行业动态 更新时间:2024-06-14 17:02:18
字符串用法为TextView ID(String usage as TextView ID)

我想生成一个随机字符串(带有一些规则),而不是将其用作textview id。 例如,我想使用带有此字符串的settext。 目的:我应该随机选择一个textview,而不是将其文本设置为另一个。

I would like to generate a random string(with some rules), than use it as a textview id. For example I would like to use settext with this string. Purpose: I should select a textview randomly, than set its text to another.

最满意答案

实际上,有不同种类的方法来实现这一目的。 例如,您可以拥有一组可以随机选择的文本。

String[] strArr = { "text1", "text2", "text3" }; Random rand = new Random(); int selected = rand.nextInt(3); textView.setText(strArr[selected]);

如果你必须从其他textview获取字符串,那么你可以创建一个ID数组而不是一个文本数组。 然后使用Random对象获取ID,然后使用以下内容:

TextView textToGetString = (TextView) findViewById(idArray[selected]); String newText = textToGetString.getText();

Actually, there are different kinds of way to achieve this purpose. For instance you could have an array of texts that can be selected randomly.

String[] strArr = { "text1", "text2", "text3" }; Random rand = new Random(); int selected = rand.nextInt(3); textView.setText(strArr[selected]);

If you MUST get the string from other textviews then you can create an array of IDs instead of an array of text. Then use the Random object to get an ID and then something like:

TextView textToGetString = (TextView) findViewById(idArray[selected]); String newText = textToGetString.getText();

更多推荐

本文发布于:2023-04-21 18:52:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/dzcp/02d6f6ae7de43a4a0d56cceae354b07f.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:字符串   TextView   ID   String   usage

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!