Android ImageButton - determine what resource is currently set

前端 未结 4 2089
Happy的楠姐
Happy的楠姐 2020-12-19 02:07

Is there a way I can find what resource a particular ImageButton is set to, at any given time?

For eg: I have an ImageButton that I set to R.drawable.btn_on

4条回答
  •  别那么骄傲
    2020-12-19 02:47

    I don't know how to access the resource directly, but for what you try to achieve, wouldn't it suffice to just get the state?

        ImageButton btn = (ImageButton) findViewById(R.id.btn);
    
        int [] states = btn.getDrawableState();
        for (int i : states) {
            if (i == android.R.attr.state_pressed) {
                Log.v("btn", "Button in pressed state");
            }
        }
    

    http://developer.android.com/reference/android/R.attr.html#state_pressed

提交回复
热议问题