Android EditText - 停止光标闪烁,需要实心光标

Android EditText - Stop cursor blinking, want a solid 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>

但这似乎不太可能。

这是编辑文本

 <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"/>

meta-mind 有什么建议吗?我所有的谷歌都在return搜索如何隐藏光标。

我会继续调查并 return 我的结果。

好的,看来 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);

希望这对某人有所帮助。