0

現在の画像ファイルを確認して、ボタンの背景画像を変更する方法。現在どの画像がオンになっているかを確認して、サウンドのオン/オフの画像を試したいとします。

main.xml

<Button
        android:id="@+id/button3"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="30dp"
        android:onClick="onClickSound"
        android:background="@drawable/sound11"
        android:onClick="onClickSound" />

main.java

public void onClickSound(View view) {
    final Button btn1 = (Button) findViewById(R.id.button3);

    Drawable i = btn1.getBackground();

            /*How to check which image resource is set*/

    btn1.setBackgroundResource(R.drawable.sound00);
}

plzがそれをチェックするのを助けることができますか?試してみましたが、R.drawable.sound00 と "i" を比較できません。

4

1 に答える 1

2

画像の最後の状態を示すフラグを設定し、それに応じてコンテンツを更新することができます。イメージ ファイル自体以外の側面で状態を判断することをお勧めします。非常に基本的に、これを試すことができます:

        boolean isPlayingSound = false; 
        public void onClickSound(View view) {
             //update this variable accordingly
             isPlayingSound = !isPlayingSound
             view.setBackgroundResource(isPlayingSound ? R.drawable.sound11 : R.drawable.sound00);          
        }
于 2013-05-20T10:46:57.223 に答える