2015-11-27 75 views
1
I need to set Selector dynamically 

我把帮助从这个How to set an image as a Background to a Button using StateListDrawable (dynamically or programmatically or through code)冻结申请

当我尝试设置默认的图像它不工作

state.addState(new int[] { },selectedthumb); 

请帮我

我已尝试

StateListDrawable state = new StateListDrawable(); 
state.addState(new int[] { android.R.attr.state_selected }, 
         selectedthumb); 
state.addState(new int[] { android.R.attr.state_focused }, thumb); 

state.addState(new int[] { },thumb); 
+0

你有什么selectedthumb? –

+0

@ChiragSavsani可绘制 – isha

回答

1
public void selector(Button b,int selectedthumb,int thumb) 
    { 
     StateListDrawable states = new StateListDrawable(); 
     states.addState(new int[] {- android.R.attr.state_pressed }, getResources().getDrawable(selectedthumb)); 
     states.addState(new int[] {}, getResources().getDrawable(thumb)); 
     b.setBackgroundDrawable(states); 
    } 
+0

感谢它的工作........ – isha

0

Check this answer您需要使用所需状态的负值。例如: -

states.addState(new int[] {-android.R.attr.state_enabled},R.drawable.btn_disabled);