2017-06-29 30 views
-2

質問を明確にしましょう。私は4つのイメージボタンを持っています。私は押されたアイコンのアイコンを変更したいと思います。次のボタンを押して次のボタンを変更し、最後のボタンを前のアイコンに戻す必要があります。クリックしたImageButtonのアイコンをアンドロイドで動的に変更する方法は?

注:例はInstagramです。それぞれのアイコンをクリックするといくつかのグレーアイコンが表示され、黒に変わります。もう1つクリックすると最後のものが灰色に変わり、新しいものが黒に変わります。

+2

ボタンの異なる状態ごとに異なるドローアブルを使用してください。 – SripadRaj

+0

@SripadRaj私はAndroidプログラミングの初心者です。 :) – Amin

+0

このソリューションをチェックできますか?https://stackoverflow.com/questions/32534076/what-is-the-best-way-to-do-a-button-group-that-c​​an-be-selected- and-activate-inde – ViramP

答えて

1

あなたが描画可能なフォルダ内のxmlファイルを作成するための以下のコードを使用して、描画イメージとしてそれを使用することができます。

<?xml version="1.0" encoding="utf-8"?> 
    <selector xmlns:android="http://schemas.android.com/apk/res/android"> 
     <item android:drawable="@drawable/button_pressed_yellow" 
       android:state_pressed="true" /> 
     <item android:drawable="@drawable/button_focused_orange" 
       android:state_focused="true" /> 
     <item android:drawable="@drawable/button_normal_green" /> 
     <item android:drawable="@drawable/selected_icon" android:state_selected="true"/> 

    </selector> 

押し状態に応じて、異なる画像を使用しています。

+0

このコードは、半分の秒のようにアイコンの色を変更するだけです。私は他のアイコンを押すまでどのように押された色を保つのですか? Instagramのように。 <項目アンドロイド:描画可能= "描画可能/ button_focused_orange @" アンドロイド:state_selected = "TRUE" /> – Amin

+0

は、上記のコード内の1つの以上の状態追加 およびiv.setSelected(真) を呼び出します。選択されたボタンの場合は 、 の場合iv.setSelected(false);選択されていないボタンの場合は です。 それは動作します。 –

+0

選択した状態を追加しましたが、まだ動作していません。 – Amin

1
public class CamTestActivity extends Activity implements View.OnClickListener{ 

    Button b1,b2,b3,b4; 

    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.main); 

     b1 = (Button)findViewById(R.id.btn1); 
     b2 = (Button)findViewById(R.id.btn2); 
     b3 = (Button)findViewById(R.id.btn3); 
     b4 = (Button)findViewById(R.id.btn4); 

     b1.setOnClickListener(this); 
     b2.setOnClickListener(this); 
     b3.setOnClickListener(this); 
     b4.setOnClickListener(this); 
    } 

    @Override 
    public void onClick(View v) { 

     int id = v.getId(); 
     switch (id){ 

      case R.id.btn1: 
       pressbtn1(); 
       break; 

      case R.id.btn2: 
       pressbtn2(); 
       break; 

      case R.id.btn3: 
       pressbtn3(); 
       break; 

      case R.id.btn4: 
       pressbtn4(); 
       break; 
     } 

    } 

    private void pressbtn4() { 
     b1.setBackgroundColor(getResources().getColor(R.color.color_gray)); 
     b2.setBackgroundColor(getResources().getColor(R.color.color_gray)); 
     b3.setBackgroundColor(getResources().getColor(R.color.color_gray)); 
     b4.setBackgroundColor(getResources().getColor(R.color.color_black)); 
    } 

    private void pressbtn3() { 
     b1.setBackgroundColor(getResources().getColor(R.color.color_gray)); 
     b2.setBackgroundColor(getResources().getColor(R.color.color_gray)); 
     b3.setBackgroundColor(getResources().getColor(R.color.color_black)); 
     b4.setBackgroundColor(getResources().getColor(R.color.color_gray)); 
    } 

    private void pressbtn2() { 
     b1.setBackgroundColor(getResources().getColor(R.color.color_gray)); 
     b2.setBackgroundColor(getResources().getColor(R.color.color_black)); 
     b3.setBackgroundColor(getResources().getColor(R.color.color_gray)); 
     b4.setBackgroundColor(getResources().getColor(R.color.color_gray)); 
    } 

    private void pressbtn1() { 
     b1.setBackgroundColor(getResources().getColor(R.color.color_black)); 
     b2.setBackgroundColor(getResources().getColor(R.color.color_gray)); 
     b3.setBackgroundColor(getResources().getColor(R.color.color_gray)); 
     b4.setBackgroundColor(getResources().getColor(R.color.color_gray)); 
    } 
} 
+0

私は背景色を変更したくありません。アイコンを完全に変更したいです。ボタン1のアイコン2つはグレー、1は黒ですクリックしたアイコン(残りのグレー)とアイコン2を黒(残りのグレー)に変えたいと考えています – Amin

+0

私は色を変えました..アイコンを変更することもできます。 b1.setBackgroundColor(getResources()。getColor(R.color.color_gray)); –

1

Javaコード:

public void onAnyButtonCLick(View view) 
    { 

    for (int i = 0; i < parent_layout.getChildCount(); i++) { 
      View v = parent_layout.getChildAt(i); 
    // Check v is button 
    if(v instanceof Button) 
    { 

    // set all button with grey color background 
     v.setBackgroundColor(getResources().getColor(R.color.color_gray)); 

    } 

    } // loop ends 

    // color the current click button with black. 
    view.setBackgroundColor(getResources().getColor(R.color.color_black)); 


    } // end of click 

XML:

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:id="@+id/parent_layout" 
    android:orientation="vertical" android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:weightSum="2"> 



    <Button 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:text="First Button" 

     android:onClick="button_click"/> 
    <Button 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:text="Second Button" 
     android:onClick="button_click"/> 
    <Button 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:text="Third Button" 
     android:onClick="button_click"/> 
    <Button 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:text="Forth Button" 
     android:onClick="button_click"/> 

</LinearLayout> 


// Hope it Helps !! 
関連する問題