点击按钮更改背景颜色?

时间:2014-08-15 19:08:49

标签: android

我想要在按下按钮后更改第二个活动的背景颜色。这是my_activity2.xml:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MyActivity2"
android:background="#ffdb4b5e">

<Button
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Next"
    android:id="@+id/button"
    android:layout_alignParentBottom="true"
    android:layout_alignParentLeft="true"
    android:layout_alignParentStart="true"
    android:layout_alignParentRight="true"
    android:layout_alignParentEnd="true"
    android:textColor="#ffffffff"
    android:textSize="72sp"
    android:background="#00000000"
    />

<TextView
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:textAppearance="?android:attr/textAppearanceLarge"
    android:text="@string/thing1"
    android:id="@+id/textView"
    android:textColor="#ffffffff"
    android:textSize="36sp"
    android:gravity="center"
    android:layout_above="@+id/button"
    />

这是我的MyActivity2.java:

@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);

我将这些背景颜色存储在strings.xml中,如下所示:

<?xml version="1.0" encoding="utf-8"?>

<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>

现在,当单击“下一步”按钮时,如何随机更改背景颜色?马修

编辑:

    @Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_my_activity2);
    final RelativeLayout layout = (RelativeLayout) findViewById(R.id.my_relative_layout);
    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);
    final String[] value = getResources().getStringArray(R.array.colorcode_array);
n.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {
        Random RAND=new Random();
        String nextValue = values[RAND.nextInt(values.length)];
        String newValue = value[index++];
        tv.setText(nextValue);
        layout.setBackgroundColor(Color.parseColor(newValue));
    }
});

}

2 个答案:

答案 0 :(得分:1)

首先,您的数组格式错误,因为您使用的是hex,它应该在十六进制颜色之前有标签,因此您可以稍后在代码中解析它。

<强>样品:

将所有颜色更改为此格式

<string-array name="colorcode_array">
<item>#3498db</item>
<item>#2ecc71</item>
<item>#9b59b6</item>
<item>#f1c40f</item>
<item>#1abc9c</item>
   .
   .

在您的布局中,您需要在RelativeLayout中添加参考ID,因此您需要在其上添加 ID

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/my_relative_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MyActivity2"
android:background="#ffdb4b5e">

然后在您的onCreate中引用它,就像您在Button中所做的那样。

RelativeLayout layout = (RelativeLayout) findViewById(R.id.my_relative_layout);

现在要更改RelativeLayout背景的颜色,现在需要使用Color.parseColor对其进行解析,并将参考背景设置为您创建的RelativeLayout上方。

<强>样品:

@Override
public void onClick(View v) {
    Random RAND=new Random();
    String nextValue = values[RAND.nextInt(values.length)];
    layout.setBackgroundColor(Color.parseColor(nextValue));
}

编辑:

创建int的全局实例

int index = 0;

在你的onClick中增加它

@Override
public void onClick(View v) {
    String nextValue = values[index++];
    layout.setBackgroundColor(Color.parseColor(nextValue));
}

编辑#2:

int index = 0;
    @Override
protected void onCreate(Bundle savedInstanceState) {

答案 1 :(得分:0)

试试这个

@Override
public void onClick(View v) {
    Random RAND=new Random();
    String nextValue = values[RAND.nextInt(values.length)];

    View view = getWindow().getDecorView();
    view.setBackgroundColor(Color.parseColor(nextValue));
}

希望它有所帮助:)