这是我的代码:
@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>
现在我需要的是,单击按钮的那一刻,你可以看到按钮已经randomley将文本从things_array加载到textview中,但同时我希望使用颜色随机改变背景颜色colorcode_array中的代码。
我该怎么做?
答案 0 :(得分:3)
view.setBackgroundColor(Color.parseColor("#"+array[index]));
应该为你做:)
(您的案例:view = tv
和array[index] = nextValue