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

前端 未结 4 1177
一整个雨季
一整个雨季 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 18:58

    First of all you should use EditText in place of TextView for taking input. If still the cursor doesn't blink, set the android:cursorVisible="true"attribute in xml file, it should make the cursor blink. If your cursor is not visible in edit text, that's also a reason one can't see the cursor blinking. Set android:textCursorDrawable="@null". This should solve your problem

    <EditText
       android:id="@+id/editext1"
       android:layout_width="fill_parent"
       android:layout_height="wrap_content"
       android:textCursorDrawable="@null"
       android:cursorVisible="true">
    
    </EditText>
    

    In your activity class, add this code as well.

    EditText input = (EditText)findViewById(R.id.edittext1);
    input.setSelection(input.getText().length());
    
    0 讨论(0)
  • 2021-02-09 19:01

    I think you should go for EditText. You can set its background and make it appears like TextView with below code.

    Step 1

    <EditText
        android:id="@+id/edtText"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:background="@android:color/transparent" >
    </EditText>
    

    Step 2

    EditText edt = (EditText) findViewById(R.id.edtText);
    edt.setSelection(edt.getText().length());
    

    Output

    enter image description here

    0 讨论(0)
  • 2021-02-09 19:03

    Finally Fixed this Using the EditText as per @Chintan Rathod advice.

    <EditText
    android:id="@+id/text"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:background="@android:color/transparent"/> //reference to @Chintan Rathod.
    

    Code

    EditText text=(EditText) findViewById(R.id.text);
    text.setText("hello");
    text.setSelection(text.getText().length()); // reference to @Umer Farooq code.
    
    0 讨论(0)
  • 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();
    
    0 讨论(0)
提交回复
热议问题