2016-05-12 27 views
2

我正在使用Android Studio
我想要改变我的按钮的颜色,每次点击。
要循环使用10种颜色,并在连续循环中重新开始。按钮onClick,通过颜色/背景循环

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

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

到目前为止,我有这是Button1的开始椭圆形(蓝色)和的onClick变成oval2(红色)
所以我的问题是,如何添加其他点击将其更改为椭圆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> 

回答

0

您可以通过以下做到这一点:

创建整数数组中的资源:

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

然后创建spe官方OnClickListener类类代码

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(); 
     } 
    } 
} 
+0

我有一个实施你所建议的事情有点麻烦。 我已经添加了整数数组,但至于其他地方,我放置它的任何地方都会破坏我的代码。我不完全确定我需要编辑代码来完成您所建议的工作。我现在只需要编码一周。但我决心让这个工作。 我已经添加了完整的MainActivity.java和activity_main.xml(如果有帮助的话) – Slayerazazel

+0

我已将代码添加到MainActivity中,请尝试。如果您将发现一些错误,请将它们发布到您的问题主体中。 –

+0

代码工作正常,但它只是让我所有的按钮看透白色。它不会在点击时添加额外的颜色。所以这似乎是一个向后的步骤,因为我可以将按钮从蓝色变为红色,按钮2从蓝色变为绿色。有没有像“如果这个按钮是红色的,改变颜色为绿色,然后如果这个按钮是绿色的,改变颜色为蓝色,那么如果这个按钮是蓝色的,改变颜色为紫色,那么如果这个颜色是紫色的,返回到开始/红色,漂洗和重复? – Slayerazazel