android.widget.ToggleButton#setBackgroundDrawable ( )源码实例Demo

下面列出了android.widget.ToggleButton#setBackgroundDrawable ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。

源代码1 项目: BigApp_Discuz_Android   文件: PasswordEditText.java
public PasswordEditText(Context context, AttributeSet attrs) {
	super(context, attrs);

	// 方式1获取属性
	TypedArray a = context.obtainStyledAttributes(attrs,
			R.styleable.PasswordEditText);

	hintString = a
			.getString(R.styleable.PasswordEditText_PasswordEditText_hint);

	passwordEditTextToggle = a
			.getDrawable(R.styleable.PasswordEditText_PasswordEditText_toggle);

	hintColor = a.getColor(R.styleable.PasswordEditText_PasswordEditText_hint_color,getContext().getResources().getColor(R.color.gray_half_5));

	passwordEditTextIcon = a
			.getDrawable(R.styleable.PasswordEditText_PasswordEditText_icon);


	passwordEditTextBackground = a
			.getDrawable(R.styleable.PasswordEditText_PasswordEditText_background);

	a.recycle();

	View view = LayoutInflater.from(context).inflate(
			R.layout.password_edittext, null);

	tbPasswordEditTextToggle = (ToggleButton) view
			.findViewById(R.id.tb_password_eidttext_toggle);

	if (passwordEditTextToggle != null)
		tbPasswordEditTextToggle
				.setBackgroundDrawable(passwordEditTextToggle);

	et = (EditText) view.findViewById(R.id.et_password_eidttext_edittext);
	if (!TextUtils.isEmpty(hintString))
		et.setHint(hintString);

	et.setHintTextColor(hintColor);


	rl = (RelativeLayout) view
			.findViewById(R.id.rl);
	if(passwordEditTextBackground !=null)
		rl.setBackgroundDrawable(passwordEditTextBackground);

	ivPasswordEditTextIcon = (ImageView) view.findViewById(R.id.iv_with_del_eidttext_icon);
	if (passwordEditTextIcon != null)
		ivPasswordEditTextIcon
				.setImageDrawable(passwordEditTextIcon);




	tbPasswordEditTextToggle.setChecked(true);

	tbPasswordEditTextToggle
			.setOnCheckedChangeListener(new OnCheckedChangeListener() {

				@Override
				public void onCheckedChanged(CompoundButton buttonView,
						boolean isChecked) {

					if (!isChecked) {
						et.setTransformationMethod(HideReturnsTransformationMethod
								.getInstance());
					} else {
						et.setTransformationMethod(PasswordTransformationMethod
								.getInstance());
					}
					et.postInvalidate();

					Editable editable = et.getText();
					et.setSelection(editable.length());
				}
			});

	// ivPasswordEditTextToggle.setOnClickListener(new OnClickListener() {
	// @Override
	// public void onClick(View v) {
	//
	// if (tbPasswordEditTextToggle.isChecked()) {
	// et.setInputType(InputType.TYPE_TEXT_VARIATION_VISIBLE_PASSWORD);
	// } else {
	// et.setInputType(InputType.TYPE_TEXT_VARIATION_NORMAL);
	// }
	// }
	// });
	// 给编辑框添加文本改变事件
	// et.addTextChangedListener(new MyTextWatcher());

	// 把获得的view加载到这个控件中
	addView(view);
}
 
@Override
public boolean updateStyle(List<PXRuleSet> ruleSets, List<PXStylerContext> contexts) {
    if (!super.updateStyle(ruleSets, contexts)) {
        return false;
    }
    // Apply the toggle Drawables wrapped in a LayerDrawable. The drawable
    // that will go into this LayerDrawable has to be a StateListDrawable,
    // so we construct it using the 'on' and 'off' states to provide the
    // toggle images in its different states.
    if (!contexts.isEmpty()) {
        // same styleable for all contexts
        Object styleable = contexts.get(0).getStyleable();
        if (styleable instanceof ToggleButton) {
            // prepare the StateListDrawable
            StateListDrawable toggleDrawable = new StateListDrawable();
            for (PXStylerContext context : contexts) {
                Drawable drawable = context.getBackgroundImage();
                if (drawable != null) {
                    if (ON.equals(context.getActiveStateName())) {
                        toggleDrawable.addState(new int[] { android.R.attr.state_checked },
                                drawable);
                    } else {
                        toggleDrawable.addState(new int[] { -android.R.attr.state_checked },
                                drawable);
                    }
                }
            }
            ToggleButton toggleButton = (ToggleButton) styleable;
            Drawable background = toggleButton.getBackground();
            if (toggleDrawable != null) {
                if (background instanceof LayerDrawable) {
                    LayerDrawable layerDrawable = (LayerDrawable) background;
                    layerDrawable.setDrawableByLayerId(android.R.id.toggle, toggleDrawable);
                } else if (background instanceof StateListDrawable) {
                    // just replace it
                    toggleButton.setBackgroundDrawable(toggleDrawable);
                }
            }
        }
    }
    return true;
}