Android EditText - 停止光标闪烁,想要一个实心光标

时间:2015-07-13 16:37:51

标签: android android-edittext cursor

我希望能够一直看到光标。没有眨眼,也没有隐藏。

我可以扩展editText并进入渲染图形并在文本写入时进行设置,但这只是一个痛苦,需要多余的工作来识别用户点击/光标移动。

要清楚

editText.setCursorVisible(false);

不是我要找的答案。

可能可以在XMl可绘制文件中设置吗?

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="rectangle" >
    <size android:width="1dp" />
    <stroke android:color="@android:color/black"/>
    <solid android:color="@android:color/black"/>
</shape>

但这似乎不太可能。

这是editText

 <AutoCompleteTextView
        android:id="@+id/input_text"
        android:layout_width="fill_parent"
        android:layout_height="0dp"
        android:textSize="7mm"
        android:drawableRight="@drawable/clear_tran"
        android:drawableEnd="@drawable/clear_tran"
        android:background="@android:color/transparent"

        android:cursorVisible="true"
        android:textCursorDrawable="@drawable/cursor"

        android:text=""
        android:autoLink="none"
       android:textAppearance="@android:style/TextAppearance.Holo.Medium"
        android:clickable="true"
        android:inputType="text|textNoSuggestions"
        android:layout_weight="1"
        android:textStyle="normal"
        android:singleLine="true"
        android:textIsSelectable="false"
        android:layout_marginLeft="2mm"
        android:layout_marginRight="2mm"
        android:layout_marginBottom="1mm"/>

来自元脑的任何建议?我的所有谷歌都只是返回如何隐藏光标。

我将继续调查并返回我的结果。

1 个答案:

答案 0 :(得分:0)

好的,似乎API不允许静态游标。我创建了自己的AutoCompleteTextView扩展,它呈现了一个永不闪烁的游标。 (您可以使用相同的代码来扩展noemal EditText或其他衍生物)

如果你想要一个多行编辑文本,那么高度参数将需要一些额外的工作。我假设一行并将文本框的高度居中。

    public class AutoCompleteEditTextStaticCursor extends AutoCompleteTextView {

            private int mCursorColor = Color.BLACK; //Cursor defaults to black
            private 

    int mStroke = 5; //Default stroke is 5

        public AutoCompleteEditTextStaticCursor(Context context) {
            super(context);
            setCursorVisible(false);
        }

        public AutoCompleteEditTextStaticCursor(Context context, AttributeSet attrs){
            super(context, attrs);
            setCursorVisible(false);
        }
        public AutoCompleteEditTextStaticCursor(Context context, AttributeSet attrs, int defStyle){
            super(context, attrs, defStyle);
            setCursorVisible(false);
        }

        /**
         * Set the cursor color
         * Must have a static int reference.
         * If you wish to use a resource then use the following method
         * int color = getResources().getColor(R.color.yourcolor);
         *
         * Default value Color.BLACK
         * @param color
         */
        public void setCursorColor(int color){ //Cursor defaults to black
            mCursorColor = color;
        }

        /**
         * Set the cursor stroke width
         *
         * Default value is 5
         * @param stroke
         */
        public void setCursorStroke(int stroke){
            mStroke = stroke;
        }

        @Override
        protected void onDraw(Canvas canvas) {

            //Take this opportunity to draw our cursor
            canvas = drawCursor(canvas);

            super.onDraw(canvas);
        }

        /**
         * Draw a cursor as a simple line, 
         * It would be possible to render a drawable if you wanted rounded corners 
         * or additional control over cursor 
         * 
         * @param canvas
         * @return
         */
        private Canvas drawCursor(Canvas canvas) {

            int pos = getSelectionStart();
            Layout layout = getLayout();

            if (layout == null){
                return canvas;


             }

            //Get where the cursor should be
            float x = layout.getPrimaryHorizontal(pos);


    //When there is no text, just off set it so that the whole cursor is drawn
            if (x< mStroke/2){
                x = mStroke/2

;
        }

            //Get the height of the edit text we will half this to center
            //TODO this will only work for 1 line!!!!!!! Multi line edit text will need further adjustment
            float height = canvas.getHeight();

            //Get the text Height
            float textHeight = getTextSize();

            Paint p = new Paint();
            p.setColor(mCursorColor);
            p.setStrokeWidth(mStroke);

            canvas.drawLine(x, height/2 - textHeight/2, x, height/2 +textHeight/2, p);

            return canvas;
        }
    }

用法就像这样

 feedbackText = (AutoCompleteEditTextStaticCursor) findViewById(R.id.input_text);
    feedbackText.setCursorColor(getResources().getColor(R.color.cursor_color));
    feedbackText.setCursorStroke(9);

希望这有助于某人。