How to set the state_pressed of a button

自闭症网瘾萝莉.ら 提交于 2019-12-07 19:27:25

Try this.

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="@drawable/btn_pressed"
      android:state_pressed="true"/>
<item android:drawable="@drawable/btn_normal" />
</selector>

use StateListDrawable for setting selector by code as:

StateListDrawable states = new StateListDrawable();
states.addState(new int[] {android.R.attr.state_pressed},getResources().getDrawable(R.drawable.pressed));
states.addState(new int[] {android.R.attr.state_focused},getResources().getDrawable(R.drawable.focused));
states.addState(new int[] { },getResources().getDrawable(R.drawable.normal));

 button.setBackgroundDrawable(states);//FOR BUTTON
babith

Use this:

StateListDrawable states = new StateListDrawable(){
    @Override
    protected boolean onStateChange(int[] stateSet) {
        //
    }
};
itemView.setBackground(states);
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!