【问题标题】:Displaying a random string on a TextView in a layout在布局中的 TextView 上显示随机字符串
【发布时间】:2011-05-13 22:59:41
【问题描述】:

大家好 - 在 Android 中,我在我的 string.xml 中写了一些字符串,我想根据随机数在 TextView 上显示...这是我所拥有的:

  int randCropPercentage = (int) Math.ceil(Math.random() * 100);  
  Random randPhrase50 = new Random();
        int[] array50 = new int[] { R.string.ss2, R.string.ss4, R.string.ss5,
                R.string.st4, R.string.st5, R.string.tt2, R.string.tt3,
                R.string.tt5, R.string.to2, R.string.to3, R.string.to4,
                R.string.os5 };
        int randPhrase = randPhrase50.nextInt(array50.length - 1);

在 if 语句中,我有这个:

 if (randomCropPercentage < 50){
                mTheMessage.setText(array50(randPhrase));
                            //etc

但我知道我做的不对,因为我得到了错误:

The method array50(int) is undefined for the type MAIN

有什么想法吗?

【问题讨论】:

    标签: android arrays string random textview


    【解决方案1】:

    事实上你应该这样写:

    array50[randPhrase]
    

    数组不是通过() 而是通过[] 提供对元素的访问

    【讨论】:

      【解决方案2】:

      使用[]访问数组

      试试这个: mTheMessage.setText(array50[randPhrase]);

      【讨论】:

        猜你喜欢
        • 2020-10-26
        • 2011-05-13
        • 1970-01-01
        • 2014-10-17
        • 2023-03-15
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2023-03-18
        相关资源
        最近更新 更多