使用清晰的edittext更好的方法

时间:2017-07-10 14:29:15

标签: java android android-edittext

我只是想知道在Android Java中清除EditText会更好:

editText.getText().clear();

editText.setText("");

效率或其他方面是否存在显着差异?我看到一些关于清算的帖子,但没有人解释利弊。

2 个答案:

答案 0 :(得分:4)

clear()Editable界面的一种方法,当您致电getText()及其在SpannableStringBuilder中的实施时,您获得的是:

public void clear() {
    replace(0, length(), "", 0, 0);
    mSpanInsertCount = 0;
}

没有详细介绍,方法调用replace(0, length(), "", 0, 0)只是将索引0中的任何字符串替换为length(),其长度为零。

所以我猜它与setText("")相同。

答案 1 :(得分:2)

看到区别:

  

clear()vs setText()

public void clear() {
    replace(0, length(), "", 0, 0);
    mSpanInsertCount = 0;
}

setText()方法重载:

public final void setText(CharSequence text) {
    setText(text, mBufferType);
}

以上setText(CharSequence text)方法调用setText(CharSequence text, BufferType type),它带有2个参数:

public void setText(CharSequence text, BufferType type) {
    setText(text, type, true, 0);

    if (mCharWrapper != null) {
        mCharWrapper.mChars = null;
    }
}

以上setText(CharSequence text, BufferType type)方法调用setText(CharSequence text, BufferType type, boolean notifyBefore, int oldlen),它带有4个参数:

private void setText(CharSequence text, BufferType type,
                     boolean notifyBefore, int oldlen) {
    if (text == null) {
        text = "";
    }

    // If suggestions are not enabled, remove the suggestion spans from the text
    if (!isSuggestionsEnabled()) {
        text = removeSuggestionSpans(text);
    }

    if (!mUserSetTextScaleX) mTextPaint.setTextScaleX(1.0f);

    if (text instanceof Spanned &&
        ((Spanned) text).getSpanStart(TextUtils.TruncateAt.MARQUEE) >= 0) {
        if (ViewConfiguration.get(mContext).isFadingMarqueeEnabled()) {
            setHorizontalFadingEdgeEnabled(true);
            mMarqueeFadeMode = MARQUEE_FADE_NORMAL;
        } else {
            setHorizontalFadingEdgeEnabled(false);
            mMarqueeFadeMode = MARQUEE_FADE_SWITCH_SHOW_ELLIPSIS;
        }
        setEllipsize(TextUtils.TruncateAt.MARQUEE);
    }

    int n = mFilters.length;
    for (int i = 0; i < n; i++) {
        CharSequence out = mFilters[i].filter(text, 0, text.length(), EMPTY_SPANNED, 0, 0);
        if (out != null) {
            text = out;
        }
    }

    if (notifyBefore) {
        if (mText != null) {
            oldlen = mText.length();
            sendBeforeTextChanged(mText, 0, oldlen, text.length());
        } else {
            sendBeforeTextChanged("", 0, 0, text.length());
        }
    }

    boolean needEditableForNotification = false;

    if (mListeners != null && mListeners.size() != 0) {
        needEditableForNotification = true;
    }

    if (type == BufferType.EDITABLE || getKeyListener() != null ||
            needEditableForNotification) {
        createEditorIfNeeded();
        mEditor.forgetUndoRedo();
        Editable t = mEditableFactory.newEditable(text);
        text = t;
        setFilters(t, mFilters);
        InputMethodManager imm = InputMethodManager.peekInstance();
        if (imm != null) imm.restartInput(this);
    } else if (type == BufferType.SPANNABLE || mMovement != null) {
        text = mSpannableFactory.newSpannable(text);
    } else if (!(text instanceof CharWrapper)) {
        text = TextUtils.stringOrSpannedString(text);
    }

    if (mAutoLinkMask != 0) {
        Spannable s2;

        if (type == BufferType.EDITABLE || text instanceof Spannable) {
            s2 = (Spannable) text;
        } else {
            s2 = mSpannableFactory.newSpannable(text);
        }

        if (Linkify.addLinks(s2, mAutoLinkMask)) {
            text = s2;
            type = (type == BufferType.EDITABLE) ? BufferType.EDITABLE : BufferType.SPANNABLE;

            /*
             * We must go ahead and set the text before changing the
             * movement method, because setMovementMethod() may call
             * setText() again to try to upgrade the buffer type.
             */
            mText = text;

            // Do not change the movement method for text that support text selection as it
            // would prevent an arbitrary cursor displacement.
            if (mLinksClickable && !textCanBeSelected()) {
                setMovementMethod(LinkMovementMethod.getInstance());
            }
        }
    }

    mBufferType = type;
    mText = text;

    if (mTransformation == null) {
        mTransformed = text;
    } else {
        mTransformed = mTransformation.getTransformation(text, this);
    }

    final int textLength = text.length();

    if (text instanceof Spannable && !mAllowTransformationLengthChange) {
        Spannable sp = (Spannable) text;

        // Remove any ChangeWatchers that might have come from other TextViews.
        final ChangeWatcher[] watchers = sp.getSpans(0, sp.length(), ChangeWatcher.class);
        final int count = watchers.length;
        for (int i = 0; i < count; i++) {
            sp.removeSpan(watchers[i]);
        }

        if (mChangeWatcher == null) mChangeWatcher = new ChangeWatcher();

        sp.setSpan(mChangeWatcher, 0, textLength, Spanned.SPAN_INCLUSIVE_INCLUSIVE |
                   (CHANGE_WATCHER_PRIORITY << Spanned.SPAN_PRIORITY_SHIFT));

        if (mEditor != null) mEditor.addSpanWatchers(sp);

        if (mTransformation != null) {
            sp.setSpan(mTransformation, 0, textLength, Spanned.SPAN_INCLUSIVE_INCLUSIVE);
        }

        if (mMovement != null) {
            mMovement.initialize(this, (Spannable) text);

            /*
             * Initializing the movement method will have set the
             * selection, so reset mSelectionMoved to keep that from
             * interfering with the normal on-focus selection-setting.
             */
            if (mEditor != null) mEditor.mSelectionMoved = false;
        }
    }

    if (mLayout != null) {
        checkForRelayout();
    }

    sendOnTextChanged(text, 0, oldlen, textLength);
    onTextChanged(text, 0, oldlen, textLength);

    notifyViewAccessibilityStateChangedIfNeeded(AccessibilityEvent.CONTENT_CHANGE_TYPE_TEXT);

    if (needEditableForNotification) {
        sendAfterTextChanged((Editable) text);
    }

    // SelectionModifierCursorController depends on textCanBeSelected, which depends on text
    if (mEditor != null) mEditor.prepareCursorControllers();
}