android - 单击按钮时随机更改背景颜色?

标签 android

这是我的代码:

 @Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_my_activity2);
    Button n = (Button) findViewById(R.id.button);
    Typeface typeface = Typeface.createFromAsset(getAssets(), "BebasNeue Bold.ttf");
    n.setTypeface(typeface);
    final TextView tv = (TextView) findViewById(R.id.textView);
    Typeface face = Typeface.createFromAsset(getAssets(),
            "OSP-DIN.ttf");
    tv.setTypeface(face);


    final String[] values = getResources().getStringArray(R.array.things_array);
    n.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Random RAND=new Random();
            String nextValue = values[RAND.nextInt(values.length)];
            tv.setText(nextValue);
        }
    });
}

我也有这个:

<string-array name="colorcode_array">
<item>3498db</item>
<item>2ecc71</item>
<item>9b59b6</item>
<item>f1c40f</item>
<item>1abc9c</item>
<item>2980b9</item>
<item>8e44ad</item>
<item>e41c1c</item>
<item>2ecca9</item>
<item>752ecc</item>
<item>4f2ecc</item>
<item>2eccc3</item>
<item>2ecc53</item>
<item>2ecc2e</item>
<item>5bcc2e</item>
<item>9ecc2e</item>
<item>cca12e</item>
<item>cc712e</item>
<item>f1c209</item>
<item>86f109</item>
<item>f11616</item>
<item>9c1818</item>
</string-array>

现在我需要的是单击按钮的那一刻,您可以看到按钮已经从 things_array 随机加载文本到 TextView 中,但同时我希望背景颜色通过使用颜色随机更改colorcode_array 中的代码。

我该怎么做?

最佳答案

view.setBackgroundColor(Color.parseColor("#"+array[index])); 应该为你做:)

(您的情况:view = tvarray[index] = nextValue

关于android - 单击按钮时随机更改背景颜色?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25295303/

相关文章:

android - Android 上的低功耗蓝牙发现

android - setText 没有更新

android - Android 模拟器上没有剩余空间

Android 有时我得到 android.app.RemoteServiceException : Bad notification posted from package. 为什么?

java - 使用 Spring api 将数据发送到服务器

android - 在 Android 上执行文件 I/O - 它属于哪个线程

android - Android 中的停止/恢复 Activity 和 onCreate

android - HTTP Post OK,如何显示响应?

android - 获取事件和指针位置

android - 从上下文菜单启动一个新的 Activity 并在 onCreate 中自动化另一个 Activity 重新启动同一个 Activity