当前位置: 首页 > 知识库问答 >
问题:

尽管有edittext,但未看到Android edittext光标。isCursorVisible()返回true

淳于新
2023-03-14

在我的xml中,我已经添加了以下属性。

           <com.project.current.widget.AutoResizeEditText
                    android:id="@+id/edittext_add_text_new"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:focusable="true"
                    android:focusableInTouchMode="true"
                    android:hint="@string/text_sticker_hint"
                    android:inputType="textNoSuggestions|textVisiblePassword|textMultiLine"
                    android:isScrollContainer="false"
                    android:padding="@dimen/dimen_margin_small"
                    android:textColor="@color/black"
                    android:textColorHint="@color/blue_popover"
                    android:textSize="@dimen/text_font_edit_text_default"
                    android:layout_alignParentTop="true"
                    android:layout_centerHorizontal="true"
                    android:background="@drawable/bg_text_sticker"
                    android:layout_toRightOf="@+id/resize_icon_left"
                    android:cursorVisible="true"
                    android:textCursorDrawable="@null"
                    android:textIsSelectable="true" />

我已将Focusable和FocusableOnTouch设置为true。我还将cursorVisible设置为true。为了确保光标不是白色的(背景是白色的),我将textCursorDrawable设置为“@null”

但光标在视图中仍然不可见。我在edittext中实现了一个touchListener(因为我还需要在父布局中移动它)。因此,我将键盘设置为在ACTION\u up时弹出。当我检查的时候

editTextViewNew.isCursorVisible()

它返回true。但光标实际上不可见。如何使其可见以及edittext中的文本可选择?(长按“粘贴”复制文本的选项也不起作用。)

我正在使用以下自定义AutoResizeEditText来根据需要调整editText中的文本大小。

public class AutoResizeEditText extends android.support.v7.widget.AppCompatEditText {
private static final int NO_LINE_LIMIT = -1;
private final RectF _availableSpaceRect = new RectF();
private final SparseIntArray _textCachedSizes = new SparseIntArray();
private final SizeTester _sizeTester;
private float _maxTextSize;
private float _spacingMult = 1.0f;
private float _spacingAdd = 0.0f;
private float _minTextSize;
private int _widthLimit;
private int _maxLines;
private boolean _enableSizeCache = true;
private boolean _initiallized = false;
private TextPaint paint;

//set Boolean for disabling auto-resize when user is resizing using controls
public static boolean isUserTyping = false;

//disable autoresizing when user is not typing text
public static void checkIfUserTyping(boolean isActive){
    isUserTyping = isActive;
}

private interface SizeTester {
    /**
     * AutoResizeEditText
     *
     * @param suggestedSize
     *            Size of text to be tested
     * @param availableSpace
     *            available space in which text must fit
     * @return an integer < 0 if after applying {@code suggestedSize} to
     *         text, it takes less space than {@code availableSpace}, > 0
     *         otherwise
     */
    public int onTestSize(int suggestedSize, RectF availableSpace);
}

public AutoResizeEditText(final Context context) {
    this(context, null, 0);
}

public AutoResizeEditText(final Context context, final AttributeSet attrs) {
    this(context, attrs, 0);
}

public AutoResizeEditText(final Context context, final AttributeSet attrs,
                          final int defStyle) {
    super(context, attrs, defStyle);
    // using the minimal recommended font size

    _minTextSize = TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_SP,
            12, getResources().getDisplayMetrics());
    _maxTextSize = getTextSize();
    if (_maxLines == 0)
        // no value was assigned during construction
        _maxLines = NO_LINE_LIMIT;
    // prepare size tester:
    _sizeTester = new SizeTester() {
        final RectF textRect = new RectF();

        @TargetApi(Build.VERSION_CODES.JELLY_BEAN)
        @Override
        public int onTestSize(final int suggestedSize,
                              final RectF availableSPace) {
            paint.setTextSize(suggestedSize);
            final String text = getText().toString();
            final boolean singleline = getMaxLines() == 1;
            if (singleline) {
                textRect.bottom = paint.getFontSpacing();
                textRect.right = paint.measureText(text);
            } else {
                final StaticLayout layout = new StaticLayout(text, paint,
                        _widthLimit, Layout.Alignment.ALIGN_NORMAL, _spacingMult,
                        _spacingAdd, true);
                // return early if we have more lines
                Log.d("NLN", "Current Lines = " + Integer.toString(layout.getLineCount()));
                Log.d("NLN", "Max Lines = " + Integer.toString(getMaxLines()));
                if (getMaxLines() != NO_LINE_LIMIT
                        && layout.getLineCount() > getMaxLines())
                    return 1;
                textRect.bottom = layout.getHeight();
                int maxWidth = -1;
                for (int i = 0; i < layout.getLineCount(); i++)
                    if (maxWidth < layout.getLineWidth(i))
                        maxWidth = (int) layout.getLineWidth(i);
                textRect.right = maxWidth;
            }
            textRect.offsetTo(0, 0);
            if (availableSPace.contains(textRect))
                // may be too small, don't worry we will find the best match
                return -1;
            // else, too big
            return 1;
        }
    };

    setEnabled(true);
    setFocusableInTouchMode(true);
    setFocusable(true);
    setEnableSizeCache(false);
    setMovementMethod(null);
    _initiallized = true;

}

@Override
public void setTypeface(final Typeface tf) {
    if (paint == null)
        paint = new TextPaint(getPaint());
    paint.setTypeface(tf);
    super.setTypeface(tf);
}

@Override
public void setTextSize(final float size) {
    _maxTextSize = size;
    _textCachedSizes.clear();
    adjustTextSize();
}

@Override
public void setMaxLines(final int maxlines) {
    super.setMaxLines(maxlines);
    _maxLines = maxlines;
    reAdjust();
}

@Override
public int getMaxLines() {
    return _maxLines;
}

@Override
public void setSingleLine() {
    super.setSingleLine();
    _maxLines = 1;
    reAdjust();
}

@Override
public void setSingleLine(final boolean singleLine) {
    super.setSingleLine(singleLine);
    if (singleLine)
        _maxLines = 1;
    else
        _maxLines = NO_LINE_LIMIT;
    reAdjust();
}

@Override
public void setLines(final int lines) {
    super.setLines(lines);
    _maxLines = lines;
    reAdjust();
}

@Override
public void setTextSize(final int unit, final float size) {
    final Context c = getContext();
    Resources r;
    if (c == null)
        r = Resources.getSystem();
    else
        r = c.getResources();
    _maxTextSize = TypedValue.applyDimension(unit, size,
            r.getDisplayMetrics());
    _textCachedSizes.clear();
    adjustTextSize();
}


@Override
public void setLineSpacing(final float add, final float mult) {
    super.setLineSpacing(add, mult);
    _spacingMult = mult;
    _spacingAdd = add;
}

/**
 * Set the lower text size limit and invalidate the view
 *
 * @param

 */
public void setMinTextSize(final float minTextSize) {
    _minTextSize = minTextSize;
    reAdjust();
}

private void reAdjust() {
    adjustTextSize();
}

private void adjustTextSize() {
    if (!_initiallized)
        return;

    Log.d("IsUserTyping"," "+isUserTyping);
    if(!isUserTyping)
        return;

    final int startSize = (int) _minTextSize;
    /*final int heightLimit = getMeasuredHeight()
            - getCompoundPaddingBottom() - getCompoundPaddingTop();
    _widthLimit = getMeasuredWidth() - getCompoundPaddingLeft()
            - getCompoundPaddingRight();*/

    //User maxWidth and maxHeight the textBox can attain to calculate resize values

    final int heightLimit = getMaxHeight()
            - getCompoundPaddingBottom() - getCompoundPaddingTop();
    _widthLimit = getMaxWidth() - getCompoundPaddingLeft()
            - getCompoundPaddingRight();

    if (_widthLimit <= 0)
        return;
    _availableSpaceRect.right = _widthLimit;
    _availableSpaceRect.bottom = heightLimit;
    super.setTextSize(
            TypedValue.COMPLEX_UNIT_PX,
            efficientTextSizeSearch(startSize, (int) _maxTextSize,
                    _sizeTester, _availableSpaceRect));
}

/**
 * Enables or disables size caching, enabling it will improve performance
 * where you are animating a value inside TextView. This stores the font
 * size against getText().length() Be careful though while enabling it as 0
 * takes more space than 1 on some fonts and so on.
 *
 * @param enable
 *            enable font size caching
 */
public void setEnableSizeCache(final boolean enable) {
    _enableSizeCache = enable;
    _textCachedSizes.clear();
    adjustTextSize();
}

private int efficientTextSizeSearch(final int start, final int end,
                                    final SizeTester sizeTester, final RectF availableSpace) {
    if (!_enableSizeCache)
        return binarySearch(start, end, sizeTester, availableSpace);
    final String text = getText().toString();
    final int key = text == null ? 0 : text.length();
    int size = _textCachedSizes.get(key);
    if (size != 0)
        return size;
    size = binarySearch(start, end, sizeTester, availableSpace);
    _textCachedSizes.put(key, size);
    return size;
}

private int binarySearch(final int start, final int end,
                         final SizeTester sizeTester, final RectF availableSpace) {
    int lastBest = start;
    int lo = start;
    int hi = end - 1;
    int mid = 0;
    while (lo <= hi) {
        mid = lo + hi >>> 1;
        final int midValCmp = sizeTester.onTestSize(mid, availableSpace);
        if (midValCmp < 0) {
            lastBest = lo;
            lo = mid + 1;
        } else if (midValCmp > 0) {
            hi = mid - 1;
            lastBest = hi;
        } else
            return mid;
    }
    // make sure to return last best
    // this is what should always be returned
    return lastBest;
}

@Override
protected void onTextChanged(final CharSequence text, final int start,
                             final int before, final int after) {
    super.onTextChanged(text, start, before, after);
    reAdjust();
}

@Override
protected void onSizeChanged(final int width, final int height,
                             final int oldwidth, final int oldheight) {
    _textCachedSizes.clear();
    super.onSizeChanged(width, height, oldwidth, oldheight);
    if (width != oldwidth || height != oldheight)
        reAdjust();
}

}

我发现这些细节是在这里实现的,在我这方面很容易模仿。

共有2个答案

阮健
2023-03-14

请将具有setMovementMethod的行从null中删除,这将显示光标以更改光标的可绘制性,您可以使用

齐运诚
2023-03-14
android:textIsSelectable="true"

如果要查看光标,请将其设置为false

您不必将textCursorDrawable设置为@null,因为颜色只取决于您对强调色的选择

 类似资料:
  • 我有一个名为的模板助手,我想将3个集合游标返回到我的模板,以便我可以查看所有 最好的方法是什么?谢谢

  • 我们将GCM与多个Android设备一起使用。我们能够使用GCM通知的一些设备(不是全部)最近无法接收GCM触发器(GCM服务器返回未注册)。 然而,当我们在设备上调用getRegistrationId()时,我们会在设备正常工作时获得相同的注册ID。 知道为什么服务器返回的是未注册的,而客户端返回的是实际的ID吗?

  • 问题内容: create_PaperBean.java 我正在使用struts,当执行该bean时,会在数据库中创建一个表,但是insert的值仍为零!因此,我无法进入正确的页面来进一步运行Web应用程序。我究竟做错了什么? 问题答案: 要么返回SQL数据操纵语言(DML)语句的行数的SQL语句回报 什么 。 创建表既不是INSERT也不是UPDATE,因此正常接收是因为没有行受到影响。

  • 如果没有断开连接,为什么返回零?或者我还能查什么?

  • 我正在为一个iOS应用程序构建一个Rails后端。后端需要与脸谱网接口,以获取和使用每个用户的脸谱网朋友。 但脸书的“朋友”请求却是空的。 我基本上提出了以下要求: 我看过大量关于StackOverflow的帖子(这个、这个、这个等等),但没有找到一个相同的问题或适合的解决方案。 我似乎设置了正确的权限。无论是在Rails应用上还是当用户通过iPhone应用进行身份验证时,我都将< code>us

  • 当我试图在工作区中使用文本文件创建BufferedReader时,“new BufferedReader语句”中的FileReader抛出一个filenotfoundexception。然而.exists()和.canRead()对于文件都返回true 这是代码 和输出 以及当我取消注释bufferedreader行时引发的异常 我发现了一个类似于这个的封闭线程,但在其中找不到任何明确的答案。