これは、状態に応じて、アプリのボタン画像を変更するために使用する xml コードです。
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_pressed="false"
android:drawable="@drawable/button_n" />
<item android:state_pressed="true"
android:drawable="@drawable/button_p" />
</selector>
コードでこれを行うにはどうすればよいですか? 私はこれを試しました:
StateListDrawable sl = new StateListDrawable();
sl.addState(new int[]{ android.R.attr.state_pressed}, R.drawable.gridcard_button_p);
ただし、addState は最初の引数として int 配列を取り、ドローアブル オブジェクトを sedon として取ります (私の例のように int ではありません)。
この方法を正しい方法で使用するにはどうすればよいですか?