2016-05-12 30 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