如何在TextView中将光标显示在文本末尾?

8
我想在TextView中的文本末尾显示闪烁的光标。
我尝试在TextView中使用android:cursorVisible="true",但不起作用。
即使我尝试了text.setCursorVisible(true);也不行。
<TextView
    android:id="@+id/text"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:cursorVisible="true"
    android:textCursorDrawable="@null"  />

有没有人知道这个问题的解决方案?

3
TextView 不适合用来接收用户的输入,您无法这样做。 - Pankaj Kumar
你应该使用EditText,它可以显示光标。 - Sayed Jalil Hassan
你们不知道你们在说什么。任何视图都可以被重写以接受输入。 - user9599745
4个回答

6

首先,为了输入文本,应该使用EditText而不是TextView。如果光标仍然不闪烁,请在xml文件中设置android:cursorVisible="true"属性,这应该使光标闪烁。如果您的光标在编辑框中不可见,那也是无法看到光标闪烁的原因之一。请设置android:textCursorDrawable="@null"。这应该解决您的问题。

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

</EditText>

在你的Activity类中,也要添加以下代码。
EditText input = (EditText)findViewById(R.id.edittext1);
input.setSelection(input.getText().length());

有趣的负评。能否告诉下投票者为什么要给负评呢? - Umer Farooq
你的回答会把光标放在末尾吗? - Mohit
1
试一下,这对我有效。如果您对此答案进行了反对投票,其他人也给出了几乎相同的答案。请对他们的答案进行反对投票。 - Umer Farooq
1
我需要的是TextView,而不是EditText。 - Uday
2
你不能对TextView这样做。如果要使TextView可编辑,为什么不使用EditText呢? - Umer Farooq
文本视图是用于查看文本的。想要使用文本视图来编辑文本就像想要在盘子上喝汤一样。顺便说一句,这对我有用,但是否有一种简单的方法可以使光标仅在编辑文本时显示。我的光标即使我没有编辑它也会出现。 - Andrew Wilson

2

这个有解决方案。

我在制作终端应用程序时必须这样做,我使用了一个简单的runnable放置光标并使其闪烁。

我创建了3个类变量:

private boolean displayCursor;
private boolean cursorOn;
private String terminalText;

private TextView terminal; // The TextView Object

terminalText 用于跟踪要显示的文本。

创建了一个类方法,第一次运行 runnable

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();
}

onCreate()中初始化变量并调用runCursorThread()

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

1
我建议你使用EditText。你可以使用以下代码设置它的背景并使其看起来像TextView

步骤1

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

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

输出

enter image description here


你认为你提出的答案会将光标放在末尾吗? - Mohit
我想在TextView中显示闪烁的光标,而不是在TextEditor中。 - Uday
1
很好,但你们只是改变了他的想法,这不是解决方案,甚至对于他最初提出的问题也没有解决方案。投票反对的原因是,如果像你们这样的人(我猜你是维基成员)开始这样回答,那么这个网站就会失去它的魅力。我也可以回答这个问题,但我没有,因为说“不”比给出错误的解决方案更好。 - Mohit
1
即使我同意 @mohit 的观点,我也会尝试找到解决方案,希望对其他人有所帮助。祝大家好运.. :) - Uday
1
你是个不错的人。别删除,让它存在着。很高兴和你交谈 :) - Mohit
显示剩余6条评论

1

最终按照@Chintan Rathod的建议,使用EditText解决了这个问题。

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

代码

EditText text=(EditText) findViewById(R.id.text);
text.setText("hello");
text.setSelection(text.getText().length()); // reference to @Umer Farooq code.

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接