根据Random Textview显示文本

时间:2015-12-04 10:20:40

标签: android random textview

我在按钮点击的两个textview中显示随机文本。现在我必须在textview2中的textview1中显示文本的解释。但是我无法得到它。将不胜感激。

我把文字放在string.xml

<string name="one">1</string>
<string name="two">2</string>
<string name="three">3</string>
<string name="four">4</string>
<string name="five">5</string>
<string name="six">6</string>
<string name="seven">7</string>
<string name="eight">8</string>
<string name="nine">9</string>
<string name="ten">10</string>

<string name="one_explaination">This is number one</string>
<string name="two_explaination">This is number two</string>
<string name="three_explaination">This is number three</string>
<string name="four_explaination">This is number four</string>
<string name="five_explaination">This is number five</string>
<string name="six_explaination">This is number six</string>
<string name="seven_explaination">This is number seven</string>
<string name="eight_explaination">This is number eight</string>
<string name="nine_explaination">This is number nine</string>
<string name="ten_explaination">This is number ten</string>

我在MainActivity中的代码:

    Random number,number_explaination;
    int [] array_number,array_number_explaination;
    int textview_number,textview_number_explaination;
    TextView textView1,textView2;

    number = new Random();
    array_number = new int[] {R.string.one,R.string.two,R.string.three,R.string.four,R.string.five,R.string.six,R.string.seven,
            R.string.eight,R.string.nine,R.string.ten};
    textview_number = number.nextInt(array_number.length - 1);
    textView1.setText(array_number[textview_number]);

            number_explaination = new Random();
    array_number_explaination = new int[] {R.string.one_explaination,R.string.two_explaination,R.string.three_explaination,
            R.string.four_explaination,R.string.five_explaination,R.string.six_explaination,R.string.seven_explaination,
            R.string.eight_explaination,R.string.nine_explaination,R.string.ten_explaination};
    textview_number_explaination = number_explaination.nextInt(array_number_explaination.length - 1);
    textView2.setText(array_number_explaination[textview_number_explaination]);

如果我在textview1中得到Random R.string.two然后我会得到R.string.two_explaination等我想要的就是这里。我怎么能得到这个。抱歉我的英语不好。

3 个答案:

答案 0 :(得分:1)

试试这个

    Random number,number_explaination;
    int [] array_number,array_number_explaination;
    int textview_number,textview_number_explaination;
    TextView textView1,textView2;

    number = new Random();
    array_number = new int[] {R.string.one,R.string.two,R.string.three,R.string.four,R.string.five,R.string.six,R.string.seven,
            R.string.eight,R.string.nine,R.string.ten};

    array_number_explaination = new int[] {R.string.one_explaination,R.string.two_explaination,R.string.three_explaination,
            R.string.four_explaination,R.string.five_explaination,R.string.six_explaination,R.string.seven_explaination,
            R.string.eight_explaination,R.string.nine_explaination,R.string.ten_explaination};  

    textview_number = number.nextInt(array_number.length - 1);
    textView1.setText(getResources().getString(array_number[textview_number]));
    textView2.setText(getResources().getString(array_number_explaination[textview_number]));

您无需为解释文本生成另一个随机数

答案 1 :(得分:0)

试试这个:

textView2.setText(getString(array_number_explaination[textview_number_explaination]));
应该使用

getString(int)来获取字符串资源文件中的字符串。

答案 2 :(得分:0)

你需要在字符串文件中使用字符串数组:

  <string-array
        name="string_array_name">
        <item
            >text_string</item>
    </string-array>

然后在代码中创建一个数组,参考:

R.array.string_array_name

请参阅http://developer.android.com/guide/topics/resources/string-resource.html以供参考。

在代码中获得两个数组之后,您可以运行循环并执行所需的工作