How to display cursor at the end of text in TextView?

前端 未结 4 1176
一整个雨季
一整个雨季 2021-02-09 18:51

I wanted to display blinking cursor at the end of the text in TextView .

I tried by android:cursorVisible=\"true\" in TextView But no go .

Even i t

4条回答
  •  醉话见心
    2021-02-09 19:11

    There is a solution for this.

    I had to do this when I was making a terminal app and I used a simple runnable put a cursor at the end and make it blink.

    I made 3 class variables:

    private boolean displayCursor;
    private boolean cursorOn;
    private String terminalText;
    
    private TextView terminal; // The TextView Object
    

    terminalText keeps track of the text to be displayed.

    Created a class method that runs the runnable the first time

    private void runCursorThread() {
        Runnable runnable = new Runnable() {
            public void run() {
                if (displayCursor) {
                    if (cursorOn) {
                        terminal.setText(terminalText);
                    } else {
                        terminal.setText(terminalText + '_');
                    }
                    cursorOn = !cursorOn;
                }
                terminal.postDelayed(this, 400);
            }
        };
        runnable.run();
    }
    

    And initiated the variables and called runCursorThread() in onCreate()

        cursorOn = false;
        displayCursor = true;
        runCursorThread();
    

提交回复
热议问题