【问题标题】:Android eclipse random string array for setText?用于setText的Android eclipse随机字符串数组?
【发布时间】:2011-04-20 20:04:09
【问题描述】:

如果我理解正确

    Random ran = new Random();
    String[] ButtonText = null;
    Resources res = getResources();
    ButtonText = res.getStringArray(R.array.ButtonText_array);
    String strRandom = ButtonText[ran.nextInt(ButtonText.length)];
    System.out.println("Random string is : "+strRandom);

是一种获取我的字符串数组项并将它们按随机顺序排列的方法,现在我想用 strRandom 中的单个项设置几个按钮的 setText。下面是一个按钮的setText

    Button gm1 = (Button) findViewById(R.id.gm1);
    gm1.setText();

但我不知道如何将 strRandom 项放入 setText 部分,因为我不需要它在这里显示我需要更改的内容。

System.out.println("Random string is : "+strRandom);

【问题讨论】:

    标签: android arrays button random settext


    【解决方案1】:

    我真的不明白这个问题......

    如果您只是询问如何将文本设置为随机字符串,请像使用 println() 语句一样进行操作,

    gm1.setText(strRandom);
    

    gm1.setText(ButtonText[ran.nextInt(ButtonText.length)]);
    

    附注:按照惯例,变量以驼峰命名法完成,为类名保留 AllCaps。 (例如 ButtonText 应该是 buttonText)。您会注意到 SO 格式化程序将 ButtonText 格式化为一个类,而不是一个数组。

    【讨论】:

      【解决方案2】:
      gm1.setText((CharSequence)("Random string is : " + strRandom));
      

      你需要从 String 转换为 CharSequence

      【讨论】:

      • 不需要从字符串强制转换为 CharSequence。
      猜你喜欢
      • 1970-01-01
      • 2016-09-27
      • 2020-01-12
      • 2016-01-04
      • 2014-02-09
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多