6

ボタンがあります

Button button = (Button)findViewById(R.id.button);
button.setBakgroundResource(R.drawable.icon);

どのバックグラウンド リソース ボタンがあるかを確認したい場合、それは可能ですか? どうやって。

例えば ​​:

if (button.getResourceId()==R.drawable.icon)

何かをする...

更新: 条件は FALSE です 本当であってほしい 画像が一致しません

vi.findViewById(R.id.button1).setOnClickListener(新しいOnClickListener(){

        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub

            v.findViewById(R.id.button1).setBackgroundResource(R.drawable.boutton_off);

            Drawable aImg = (Drawable)v.findViewById(R.id.button1).getBackground();
            Drawable bImg = v.getResources().getDrawable(R.drawable.boutton_off);

            if(aImg==bImg){

                System.out.println("On");

            }else 
                System.out.println("off");



            //main.popMessage(position);


        }

    }); 
4

8 に答える 8

4

できないようです。リソースは Drawable に解決され、標準機能に戻すことができるのはそれだけです。別の方法でドローアブルを id に戻す方法があるかもしれませんが、この機能はボタンクラスには実装されていません。

その resourceId に簡単にアクセスする必要があり、コードからリソースを設定している場合は、Android ボタンを実装する ButtonClass を記述し、setBackgroundResource をオーバーロード/作成して、アクセス可能な追加フィールドに id を保存できます。もちろん、これは機能しません。ボタンが BackgroundRessource を取得したのは、あなたの側からの関数呼び出しによるものではありません。

ここにいくつかのコードがあります。

import android.content.Context;
import android.util.AttributeSet;
import android.widget.Button;

public class MyButton extends Button {
    private int bgId;

    public MyButton(Context context) {
        super(context);
    }
    public MyButton(Context context, AttributeSet attrs) {
        super(context, attrs);
    }
    public MyButton(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
    }
    @Override
    public void setBackgroundResource(int resId) {
        super.setBackgroundResource(resId);
        bgId = resId;   
    }

    public int getBackgroundId() {
        return bgId;
    }
}
于 2013-04-24T12:17:31.233 に答える
-1
Drawable aImg = (Drawable)done.getBackground();
Drawable bImg = getResources().getDrawable(R.drawable.ic_launcher);
if(aImg==bImg){

}
于 2013-04-24T12:22:41.827 に答える
-1

このようなことを行うことができます.. 1.ボタンに設定した背景は、この背景をボタンのIDとして設定します.手段button.setId(R.drawable.image);

2.状態を確認します。

int temp = button.getId();
    if(temp == R.drawable.image){
    //Do something...
    }

お役に立てば幸いです...

于 2013-04-24T12:16:18.577 に答える