按钮 onClick,循环 Colors/Backgrounds

Button onClick, Cycle through Colors/Backgrounds

我正在使用 Android Studio
我想在每次点击时更改按钮的颜色。
循环使用大约 10 种颜色,然后以连续循环重新开始。

例如我使用 setBackgroundResource(@drawable/oval)

oval = blue circle button  
oval2 = red circle button  
oval3 = green circle button and so on.  

到目前为止,我知道 button1 开始时是椭圆形(蓝色),onClick 变成椭圆形 2(红色)
所以我的问题是,如何添加另一个点击以将其更改为椭圆形 3(绿色),然后将其循环回到开始椭圆形(蓝色)?

MainActivity.java

package com.example.shadowz.buttononclick;

import android.graphics.Color;
import android.graphics.drawable.Drawable;
import android.support.v4.app.Fragment;
import android.support.v4.view.TintableBackgroundView;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.RelativeLayout;
import android.widget.TextView;

public class MainActivity extends AppCompatActivity
{
private Button colorChangeButton;
private TextView basicText;
private RelativeLayout background;

Button button1;
Button button2;
Button button3;
Button button4;
Button button5;
Drawable oval1;
Drawable oval2;
Drawable oval3;
Drawable oval4;
Drawable oval5;

@Override
protected void onCreate(Bundle savedInstanceState)
{
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    background = (RelativeLayout) findViewById(R.id.backgroundLayout);
    basicText = (TextView) findViewById(R.id.button1);
    colorChangeButton = (Button) findViewById(R.id.button1);

    // Code Break

    button1 = (Button) findViewById(R.id.button1);
    button2 = (Button) findViewById(R.id.button2);
    button3 = (Button) findViewById(R.id.button3);
    button4 = (Button) findViewById(R.id.button4);
    button5 = (Button) findViewById(R.id.button5);

    // Code Break

    button1.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub
            if (v == button1) {
                button1.setBackgroundResource(R.drawable.oval2);

            }
        }
    });
    // Code Break

    button2.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub
            if (v == button2) {
                button2.setBackgroundResource(R.drawable.oval3);

            }
        }
    });
    // Code Break
    button3.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub
            if (v == button3) {
                button3.setBackgroundResource(R.drawable.oval4);

            }
        }
    });
    // Code Break
    button4.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub
            if (v == button4) {
                button4.setBackgroundResource(R.drawable.oval5);

            }
        }
    });
    // Code Break
    button5.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub
            if (v == button5) {
                button5.setBackgroundResource(R.drawable.oval6);

            }
        }
    });
}
}

activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<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"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="com.example.shadowz.buttononclick.MainActivity"
android:id="@+id/backgroundLayout">

<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Hello World!" />

<Button
    android:id="@+id/button1"
    android:layout_width="140dp"
    android:layout_height="140dp"
    android:text="button1"
    android:background="@drawable/oval"
    android:padding="@dimen/abc_action_bar_content_inset_material"
    android:layout_below="@+id/button3"
    android:layout_alignParentLeft="true"
    android:layout_alignParentStart="true" />
<Button
    android:id="@+id/button2"
    android:layout_width="140dp"
    android:layout_height="140dp"
    android:text="button2"
    android:background="@drawable/oval"
    android:layout_alignTop="@+id/button1"
    android:layout_alignParentRight="true"
    android:layout_alignParentEnd="true" />

<Button
    android:id="@+id/button3"
    android:layout_width="140dp"
    android:layout_height="140dp"
    android:text="button3"
    android:background="@drawable/oval"
    android:singleLine="false"
    android:layout_centerVertical="true"
    android:layout_alignParentLeft="true"
    android:layout_alignParentStart="true" />

<Button
    android:id="@+id/button4"
    android:layout_width="140dp"
    android:layout_height="140dp"
    android:text="button4"
    android:background="@drawable/oval"
    android:padding="@dimen/abc_action_bar_content_inset_material"
    android:layout_above="@+id/button2"
    android:layout_alignLeft="@+id/button2"
    android:layout_alignStart="@+id/button2" />

<Button
    android:id="@+id/button5"
    android:layout_width="140dp"
    android:layout_height="140dp"
    android:text="button5"
    android:background="@drawable/oval"
    android:padding="@dimen/abc_action_bar_content_inset_material"
    android:layout_alignBottom="@+id/button1"
    android:layout_centerHorizontal="true"
    android:layout_marginBottom="78dp" />


</RelativeLayout>

您可以通过以下方式完成此操作:

在资源中创建整数数组:

<integer-array name="backgrounds">
    <item>@drawable/oval1</item>
    <item>@drawable/oval2</item>
    <item>@drawable/oval3</item>
    <item>@drawable/oval4</item>
    <item>@drawable/oval5</item>
</integer-array>

然后在你的 class 代码中创建特殊的 OnClickListener class

private static class MyClickListener implements View.OnClickListener {

    private int mBackgroundIndex = 0;
    private final TypedArray mBackgrounds;

    public MyClickListener(Context context) {
        mBackgrounds = context.getResources().obtainTypedArray(R.array.backgrounds);
    }

    @Override
    public void onClick(View v) {
        mBackgroundIndex++;
        if (mBackgroundIndex >= mBackgrounds.length()) {
            mBackgroundIndex = 0;
        }
        v.setBackgroundResource(mBackgrounds.getResourceId(mBackgroundIndex, 0));
    }

    @Override
    protected void finalize() throws Throwable {
        mBackgrounds.recycle();
        super.finalize();
    }
}

然后为每个按钮设置监听器:

button1.setOnClickListener(new MyClickListener(this));
button2.setOnClickListener(new MyClickListener(this));
button3.setOnClickListener(new MyClickListener(this));
button4.setOnClickListener(new MyClickListener(this));
button5.setOnClickListener(new MyClickListener(this));

这应该会产生以下 MainActivity 代码:

package com.example.shadowz.buttononclick;

import android.content.Context;
import android.graphics.Color;
import android.graphics.drawable.Drawable;
import android.support.v4.app.Fragment;
import android.support.v4.view.TintableBackgroundView;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.RelativeLayout;
import android.widget.TextView;

public class MainActivity extends AppCompatActivity {
    private Button colorChangeButton;
    private TextView basicText;
    private RelativeLayout background;

    Button button1;
    Button button2;
    Button button3;
    Button button4;
    Button button5;
    Drawable oval1;
    Drawable oval2;
    Drawable oval3;
    Drawable oval4;
    Drawable oval5;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        background = (RelativeLayout) findViewById(R.id.backgroundLayout);
        basicText = (TextView) findViewById(R.id.button1);
        colorChangeButton = (Button) findViewById(R.id.button1);

        // Code Break

        button1 = (Button) findViewById(R.id.button1);
        button2 = (Button) findViewById(R.id.button2);
        button3 = (Button) findViewById(R.id.button3);
        button4 = (Button) findViewById(R.id.button4);
        button5 = (Button) findViewById(R.id.button5);

        button1.setOnClickListener(new MyClickListener(this));
        button2.setOnClickListener(new MyClickListener(this));
        button3.setOnClickListener(new MyClickListener(this));
        button4.setOnClickListener(new MyClickListener(this));
        button5.setOnClickListener(new MyClickListener(this));
    }

    private static class MyClickListener implements View.OnClickListener {

        private int mBackgroundIndex = 0;
        private final TypedArray mBackgrounds;

        public MyClickListener(Context context) {
            mBackgrounds = context.getResources().obtainTypedArray(R.array.backgrounds);
        }

        @Override
        public void onClick(View v) {
            mBackgroundIndex++;
            if (mBackgroundIndex >= mBackgrounds.length()) {
                mBackgroundIndex = 0;
            }
            v.setBackgroundResource(mBackgrounds.getResourceId(mBackgroundIndex, 0));
        }

        @Override
        protected void finalize() throws Throwable {
            mBackgrounds.recycle();
            super.finalize();
        }
    }
}