我想生成一个随机字符串(带有一些规则),而不是将其用作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();更多推荐
发布评论