按钮onClick,循环通过颜色/背景

时间:2016-05-12 08:39:49

标签: android android-studio android-button

我正在使用Android Studio
我希望每次点击都能改变按钮的颜色 将其循环通过大约10种颜色,然后以连续循环重新开始。

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

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

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

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>

0 个答案:

没有答案