在 TextView 和 EditText 之间切换

发布于 2024-09-18 04:04:02 字数 102 浏览 6 评论 0原文

是否可以互换 TextView 和 EditText。就像在需要时显示文本,但在需要时允许编辑。是否有一种方法(如非 XML 中)编辑 TextView 或不编辑 EditText 的方法?

Is this possible to interchange a TextView and an EditText. 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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

绻影浮沉 2024-09-25 04:04:02

是否可以互换 TextView 和 EditText。

将两者放在布局中的同一位置。使一个不可见。根据需要切换可见性。

或者,将两者作为 ViewSwitcherViewFlipper 的子级,并使用它们在它们之间进行切换。

是否有一种方法(如非 XML)编辑 TextView 或不编辑 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.

紫竹語嫣☆ 2024-09-25 04:04:02

是的,事实上,根据EditText代码:

EditText 是 TextView 上的一个薄饰面,将自身配置为
可编辑。

而且它非常很薄!您基本上可以说 TextView 是一个配置为不可编辑的 EditTextEditText 的代码非常短,我将其放在这个问题的底部以供参考。

唯一真正的区别在于样式,它们是在 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>

您可以使用以下内容将它们重置为 java 中的 TextView 值:

        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);

您可能不想更改所有这些,例如也许留下重力和文本颜色/外观,所以事情不会突然发生奇怪的变化。

EditText代码

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());
    }
}

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());
    }
}
何其悲哀 2024-09-25 04:04:02

EditTextTextView 的子类。如果您想禁用 EditText 的编辑功能,只需将 inputType 设置为 "none" ,它将表现为 >文本视图

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.

心房敞 2024-09-25 04:04:02

我创建了一些方法,您可以在需要时利用它们进行适当的转变。希望这对大家有帮助。

    //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());
    }
}

您可以利用这些方法在 EditText 以及 AppCompatEditText (Androidx) 的查看和编辑模式之间切换。

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).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文