Prevent ToggleButton Toggle

I have a ToggleButton, when you click it, I do not want the state to change. I myself will handle state changes when, after receiving feedback from any button, it switches. How can I prevent a state change by clicking a button?

+4
source share
3 answers

Although I think that you can simply mark it as disabled, I don’t think it is a good idea, as users are used to certain semantics of such a button.

If you want to show some state, why don't you use ImageView and show different images depending on the state?

0
source

You can implement your own ToggleButton using the overriden toggle() method with an empty body.

+4
source

Instead, you can simply use CheckedTextView.

Of course, you need to set the background image and text based on state, but not the ones (which you may have already used), this is a good alternative solution.

here's a sample code if you skip the textOn and textOff attributes:

CheckableTextView.java:

 public class CheckableTextView extends CheckedTextView { private CharSequence mTextOn, mTextOff; public CheckableTextView (final Context context, final AttributeSet attrs, final int defStyle) { super(context, attrs, defStyle); final TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.CheckableTextView, defStyle, 0); mTextOn = a.getString(R.styleable.CheckableTextView_textOn); mTextOff = a.getString(R.styleable.CheckableTextView_textOff); a.recycle(); } public CheckableTextView(final Context context, final AttributeSet attrs) { this(context, attrs, 0); } public CheckableTextView(final Context context) { this(context, null, 0); } @Override public void setChecked(final boolean checked) { super.setChecked(checked); if (mTextOn == null && mTextOff == null) return; if (checked) super.setText(mTextOn); else super.setText(mTextOff); } public void setTextOff(final CharSequence textOff) { this.mTextOff = textOff; } public void setTextOn(final CharSequence textOn) { this.mTextOn = textOn; } public CharSequence getTextOff() { return this.mTextOff; } public CharSequence getTextOn() { return this.mTextOn; } } 

in res / values ​​/attr.xml:

 <declare-styleable name="SyncMeCheckableTextView"> <attr name="textOn" format="reference|string" /> <attr name="textOff" format="reference|string" /> </declare-styleable> 

another possible solution would be to use setClickable (false) in a ToggleButton and handle the onTouchListener when the movement action is ACTION_UP.

+1
source

All Articles