开发者

Switch between a TextView and EditText

开发者 https://www.devze.com 2023-01-14 14:35 出处:网络
Is this possible to interchange a TextView and an Edit开发者_Go百科Text. Like display the text when needed, but allow editing when needed. Is there a method (as in NON-XML) way of editing a TextView o

Is this possible to interchange a TextView and an Edit开发者_Go百科Text. Like display the text when needed, but allow editing when needed. Is there a method (as in NON-XML) way of editing a TextView or non-editing a EditText?


Is this possible to interchange a TextView and an EditText.

Put both in your layout in the same spot. Make one invisible. Toggle visibility as needed.

Or, put both as children of a ViewSwitcher or ViewFlipper, and use that to switch between them.

Is there a method (as in NON-XML) way of editing a TextView or non-editing a EditText?

No, AFAIK.


Yes, in fact, according to the EditText code:

EditText is a thin veneer over TextView that configures itself to be editable.

And it is very thin! You could basically say a TextView is an EditText that is configured to be non-editable. The code for EditText is so short I put it at the bottom of this question for reference.

The only real difference is in the styles, which are set in XML:

<style name="Widget.EditText">
    <item name="android:focusable">true</item>
    <item name="android:focusableInTouchMode">true</item>
    <item name="android:clickable">true</item>
    <item name="android:background">?android:attr/editTextBackground</item>
    <item name="android:textAppearance">?android:attr/textAppearanceMediumInverse</item>
    <item name="android:textColor">?android:attr/editTextColor</item>
    <item name="android:gravity">center_vertical</item>
</style>

You can reset those to the TextView values in java using something like this:

        mEditView.setFocusable(false);
    mEditView.setFocusableInTouchMode(false);
    mEditView.setClickable(false);
    mEditView.setBackground(null); // Or setBackgroundDrawable() on early APIs.
    mEditView.setTextAppearance(mEditView.getContext(), android.R.attr.textAppearanceSmall);
    mEditView.setTextColor(Color.BLACK); // I'm not sure how to get the default here.
    mEditView.setGravity(Gravity.TOP | Gravity.START);

You probably won't want to change all of those, e.g. maybe leave gravity and text colour/appearance, so things don't suddenly change weirdly.

EditText code

public class EditText extends TextView {
    public EditText(Context context) {
        this(context, null);
    }

    public EditText(Context context, AttributeSet attrs) {
        this(context, attrs, com.android.internal.R.attr.editTextStyle);
    }

    public EditText(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
    }

    @Override
    protected boolean getDefaultEditable() {
        return true;
    }

    @Override
    protected MovementMethod getDefaultMovementMethod() {
        return ArrowKeyMovementMethod.getInstance();
    }

    @Override
    public Editable getText() {
        return (Editable) super.getText();
    }

    @Override
    public void setText(CharSequence text, BufferType type) {
        super.setText(text, BufferType.EDITABLE);
    }

    /**
     * Convenience for {@link Selection#setSelection(Spannable, int, int)}.
     */
    public void setSelection(int start, int stop) {
        Selection.setSelection(getText(), start, stop);
    }

    /**
     * Convenience for {@link Selection#setSelection(Spannable, int)}.
     */
    public void setSelection(int index) {
        Selection.setSelection(getText(), index);
    }

    /**
     * Convenience for {@link Selection#selectAll}.
     */
    public void selectAll() {
        Selection.selectAll(getText());
    }

    /**
     * Convenience for {@link Selection#extendSelection}.
     */
    public void extendSelection(int index) {
        Selection.extendSelection(getText(), index);
    }

    @Override
    public void setEllipsize(TextUtils.TruncateAt ellipsis) {
        if (ellipsis == TextUtils.TruncateAt.MARQUEE) {
            throw new IllegalArgumentException("EditText cannot use the ellipsize mode "
                    + "TextUtils.TruncateAt.MARQUEE");
        }
        super.setEllipsize(ellipsis);
    }

    @Override
    public void onInitializeAccessibilityEvent(AccessibilityEvent event) {
        super.onInitializeAccessibilityEvent(event);
        event.setClassName(EditText.class.getName());
    }

    @Override
    public void onInitializeAccessibilityNodeInfo(AccessibilityNodeInfo info) {
        super.onInitializeAccessibilityNodeInfo(info);
        info.setClassName(EditText.class.getName());
    }
}


EditText is a subclass of TextView. If you want to disable the editing function of an EditText you can simply set the inputType to "none" and it will behave as a TextView.


I have created a few methods which you can leverage to do the appropriate shift whenever needed. Hope This will Help everyone.

    //Code for reference
public static void setViewingMode(final AppCompatEditText mEditView) {
    mEditView.setFocusable(false);
    mEditView.setFocusableInTouchMode(false);
    mEditView.setClickable(false);
    int sdk = android.os.Build.VERSION.SDK_INT;
    if (sdk < android.os.Build.VERSION_CODES.JELLY_BEAN) {
        mEditView.setBackgroundDrawable(null);
    } else {
        mEditView.setBackground(null);
    }
}

public static void setEditingMode(final AppCompatEditText mEditView, final Context context) {
    mEditView.setFocusable(false);
    mEditView.setFocusableInTouchMode(false);
    mEditView.setClickable(false);
    mEditView.setBackground(null);

    AppCompatEditText et = new AppCompatEditText(context);

    int sdk = android.os.Build.VERSION.SDK_INT;
    if (sdk < android.os.Build.VERSION_CODES.JELLY_BEAN) {
        mEditView.setBackgroundDrawable(et.getBackground());
    } else {
        mEditView.setBackground(et.getBackground());
    }
}

You can leverage these methods to Switch Between Viewing and Editing Mode For EditText as well As AppCompatEditText (Androidx).

0

精彩评论

暂无评论...
验证码 换一张
取 消