Java Code Examples for android.widget.ToggleButton#setBackgroundDrawable()

The following examples show how to use android.widget.ToggleButton#setBackgroundDrawable() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: PasswordEditText.java    From BigApp_Discuz_Android with Apache License 2.0 4 votes vote down vote up
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);
}
 
Example 2
Source File: PXVirtualToggleAdapter.java    From pixate-freestyle-android with Apache License 2.0 4 votes vote down vote up
@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;
}