【问题标题】:Change background color on button click?单击按钮时更改背景颜色?
【发布时间】:2014-10-09 13:24:15
【问题描述】:

我希望我的第二个活动的背景颜色在按下按钮后改变。这是 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));
    }
});

}

【问题讨论】:

    标签: android


    【解决方案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) {
    

    【讨论】:

    • 完美运行,但还有一件事。我怎样才能让它不随机?这样就可以了吗?
    • @user3456590 使用int = 0 在索引中使用并在单击按钮时增加它。
    • +1'ed,我该怎么做?我几天前才开始
    • @user3456590 见上面的编辑
    • 它变为数组中的第一个颜色,但随后不再改变
    【解决方案2】:

    试试这个

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

    希望对你有帮助:)

    【讨论】:

    • 设置另一个指向 R.array.colorcode_array 的字符串数组,并按照 Spurdow 的回答告诉你。或者,将 id 设置为您的根 RelativeLayout 并在您的代码上对该 RelativeLayout 使用 setBackgroundColor()。
    • 我是否只是将其添加到我现有的 onclick 方法中?