代码实现Selector按钮点击效果
2012-09-28 10:11
525 查看
用途:动态设置Button、ImageView等组件在不同状态下的背景/前景显示效果。
扩展下的话可以前景/背景的显示效果可以使用网络图片。
优点:灵活,减少xml的编写。
参考:
[AndroidOpenSource]frameworksasecorejavaandroidviewview.xml
[AndroidOpenSource]frameworksasecore
es
esvaluespublic.xml
代码如下:
/** 设置Selector。 */
public static StateListDrawable newSelector(Context context, int idNormal, int idPressed, int idFocused,
int idUnable) {
StateListDrawable bg = new StateListDrawable();
Drawable normal = idNormal == -1 ? null : context.getResources().getDrawable(idNormal);
Drawable pressed = idPressed == -1 ? null : context.getResources().getDrawable(idPressed);
Drawable focused = idFocused == -1 ? null : context.getResources().getDrawable(idFocused);
Drawable unable = idUnable == -1 ? null : context.getResources().getDrawable(idUnable);
// View.PRESSED_ENABLED_STATE_SET
bg.addState(new int[] { android.R.attr.state_pressed, android.R.attr.state_enabled }, pressed);
// View.ENABLED_FOCUSED_STATE_SET
bg.addState(new int[] { android.R.attr.state_enabled, android.R.attr.state_focused }, focused);
// View.ENABLED_STATE_SET
bg.addState(new int[] { android.R.attr.state_enabled }, normal);
// View.FOCUSED_STATE_SET
bg.addState(new int[] { android.R.attr.state_focused }, focused);
// View.WINDOW_FOCUSED_STATE_SET
bg.addState(new int[] { android.R.attr.state_window_focused }, unable);
// View.EMPTY_STATE_SET
bg.addState(new int[] {}, normal);
return bg;
}
复制代码
示例代码:
Button btnNormal = (Button) findViewById(R.id.btnSampleNormal);
btnNormal.setBackgroundDrawable(newSelector(this, R.drawable.btn_normal, R.drawable.btn_selected,
R.drawable.btn_selected, R.drawable.btn_unable));
Button btnUnable = (Button) findViewById(R.id.btnSampleUnable);
btnUnable.setBackgroundDrawable(newSelector(this, R.drawable.btn_normal, R.drawable.btn_selected,
R.drawable.btn_selected, R.drawable.btn_unable));
btnUnable.setEnabled(false);
btnUnable.setOnClickListener(this);
复制代码
/** 对TextView设置不同状态时其文字颜色。 */
private ColorStateList createColorStateList(int normal, int pressed, int focused, int unable) {
int[] colors = new int[] { pressed, focused, normal, focused, unable, normal };
int[][] states = new int[6][];
states[0] = new int[] { android.R.attr.state_pressed, android.R.attr.state_enabled };
states[1] = new int[] { android.R.attr.state_enabled, android.R.attr.state_focused };
states[2] = new int[] { android.R.attr.state_enabled };
states[3] = new int[] { android.R.attr.state_focused };
states[4] = new int[] { android.R.attr.state_window_focused };
states[5] = new int[] {};
ColorStateList colorList = new ColorStateList(states, colors);
return colorList;
}
扩展下的话可以前景/背景的显示效果可以使用网络图片。
优点:灵活,减少xml的编写。
参考:
[AndroidOpenSource]frameworksasecorejavaandroidviewview.xml
[AndroidOpenSource]frameworksasecore
es
esvaluespublic.xml
代码如下:
/** 设置Selector。 */
public static StateListDrawable newSelector(Context context, int idNormal, int idPressed, int idFocused,
int idUnable) {
StateListDrawable bg = new StateListDrawable();
Drawable normal = idNormal == -1 ? null : context.getResources().getDrawable(idNormal);
Drawable pressed = idPressed == -1 ? null : context.getResources().getDrawable(idPressed);
Drawable focused = idFocused == -1 ? null : context.getResources().getDrawable(idFocused);
Drawable unable = idUnable == -1 ? null : context.getResources().getDrawable(idUnable);
// View.PRESSED_ENABLED_STATE_SET
bg.addState(new int[] { android.R.attr.state_pressed, android.R.attr.state_enabled }, pressed);
// View.ENABLED_FOCUSED_STATE_SET
bg.addState(new int[] { android.R.attr.state_enabled, android.R.attr.state_focused }, focused);
// View.ENABLED_STATE_SET
bg.addState(new int[] { android.R.attr.state_enabled }, normal);
// View.FOCUSED_STATE_SET
bg.addState(new int[] { android.R.attr.state_focused }, focused);
// View.WINDOW_FOCUSED_STATE_SET
bg.addState(new int[] { android.R.attr.state_window_focused }, unable);
// View.EMPTY_STATE_SET
bg.addState(new int[] {}, normal);
return bg;
}
复制代码
示例代码:
Button btnNormal = (Button) findViewById(R.id.btnSampleNormal);
btnNormal.setBackgroundDrawable(newSelector(this, R.drawable.btn_normal, R.drawable.btn_selected,
R.drawable.btn_selected, R.drawable.btn_unable));
Button btnUnable = (Button) findViewById(R.id.btnSampleUnable);
btnUnable.setBackgroundDrawable(newSelector(this, R.drawable.btn_normal, R.drawable.btn_selected,
R.drawable.btn_selected, R.drawable.btn_unable));
btnUnable.setEnabled(false);
btnUnable.setOnClickListener(this);
复制代码
/** 对TextView设置不同状态时其文字颜色。 */
private ColorStateList createColorStateList(int normal, int pressed, int focused, int unable) {
int[] colors = new int[] { pressed, focused, normal, focused, unable, normal };
int[][] states = new int[6][];
states[0] = new int[] { android.R.attr.state_pressed, android.R.attr.state_enabled };
states[1] = new int[] { android.R.attr.state_enabled, android.R.attr.state_focused };
states[2] = new int[] { android.R.attr.state_enabled };
states[3] = new int[] { android.R.attr.state_focused };
states[4] = new int[] { android.R.attr.state_window_focused };
states[5] = new int[] {};
ColorStateList colorList = new ColorStateList(states, colors);
return colorList;
}
相关文章推荐
- 代码实现Selector按钮点击效果
- js点击按钮实现水波纹效果代码(CSS3和Canves)
- selector实现按钮点击效果
- 图片轮换效果实现代码(点击按钮停止执行)
- Android按钮点击效果的实现(selector)
- Android按钮点击效果的实现(selector)
- 代码实现按钮点击高亮效果
- 图片轮换效果实现代码(点击按钮停止执行)
- android Button按钮实现点击效果代码附带demo下载
- Android中ImageButton自定义按钮的按下效果的代码实现方法
- selector实现按钮选中高亮效果原理
- Android_UI_点击按钮切换背景效果实现
- asp.net下按钮点击后禁用的实现代码
- AngularJS实现按钮提示与点击变色效果
- Android中图片实现按钮点击效果
- 安卓的LinearLayout实现按钮的点击效果
- 添加按钮图标并且当点击或者悬浮上面出现不同效果的代码
- 点击按钮实现倒计时效果
- Android中ImageButton自定义按钮的按下效果的代码实现方法,附网上2种经典解决方法。
- android selector 背景选择器的使用, button (未点击,点击,选中保持状态)效果实现