2016-05-12 16 views
2

私はAndroidスタジオを使用しています
ボタンをクリックするたびに、自分のボタンの色を変更したいと思います。
約10色まで循環させ、連続ループでやり直します。ボタンonClick、サイクルスルーカラー/背景

例えば、私はこれまでのところ、私は(赤)ボタン1が(青)として楕円形の始まりとのonClickはoval2に変わるということ、それを持っているsetBackgroundResource(@drawable/oval)

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

を使用してい
だから私の質問は別のものを追加する方法であり、クリックして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

資源で整数の配列を作成します。

<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あなたのクラスコード

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

内部OnClickListenerクラスをCIALそして、各ボタンにそのリスナーを設定します。

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

私がいますあなたが提案したことを実装するのに少し問題があります。 私はinterger配列を追加しましたが、残りの部分はどこに置いても、私のコードは壊れます。私はあなたが仕事を提案したものを作るためにコードを編集する必要があるかどうかは正確にはわかりません。私はちょうど今週のちょうど1週間のためにコーディングしています。しかし、私はこれを働かせることに決めました。 MainActivity.javaとactivity_main.xmlをすべて追加しました。 – Slayerazazel

+0

MainActivityにコードを追加しました。試してみてください。いくつかのエラーが発生した場合は、質問の本文に投稿してください。 –

+0

コードは正常に動作しますが、すべてのボタンが白く見えるだけです。クリックで余分な色を追加することはありません。だから、私はどこにいたのか、私は青から赤、ボタン2は青から緑にボタンを得ることができるので、後ろのステップと思われる。 「このボタンが赤い場合は緑色に変更し、緑色の場合は青色に変更し、青色の場合は紫色に変更し、紫色の場合は元に戻ってください開始/赤、すすぎ、リピートする – Slayerazazel