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

Android 如何在TextView中在文本末尾显示光标?,android,Android,我想在TextView的文本末尾显示闪烁的光标 我在TextView中尝试了android:cursorVisible=“true”,但没有成功 甚至我也尝试了text.setCursorVisible(true)不起作用 <TextView android:id="@+id/text" android:layout_width="wrap_content" android:layout_height="wrap_content" android:curso

我想在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"  />


有人知道它的解决方案吗?

首先,你应该使用
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>

我想你应该选择编辑文本。您可以使用下面的代码设置其背景并使其看起来像
TextView

第一步 输出


根据@Chintan Rathod建议,最终使用编辑文本修复了此问题

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

有一个解决方案。

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

我制作了3个类变量:

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

private TextView terminal; // The TextView Object
终端文本
跟踪要显示的文本

创建了一个类方法,第一次运行
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()


你不能。。TextView不是为从用户获取输入而创建的…您应该使用EditText。这会显示光标。你们不知道自己在说什么。可以覆盖任何视图以接受输入。有趣的下一票。被否决的选民会介意说出被否决的原因吗?你的答案会把光标放在最后吗?试试看,它对我有用。如果你否决了这个答案,其他人也会给出几乎相同的答案。投票否决他们的答案。我想为TextView兄弟投票。不是为EditText投票。你不能为TextView投票。如果您想使textView可编辑,为什么不使用EditText代替?您认为您建议的答案是将光标放在末尾吗?他想要的是将
放在textView中文本的末尾。所以文本的结尾意味着不正确,我亲爱的朋友。如果我没有错的话。你在投否决票之前应该小心。你应该在那之前试试代码。很好,但你们只是改变了他的想法,那不是sol,甚至他最初提出的问题也没有解决方案。否决投票的原因是,如果像你这样的人(猜你是维基会员)开始这样回答,那么这个网站就会失去魅力。我也可以回答这个问题,但我没有回答,因为说“不”比给出一个虚假的sol要好。即使我同意@mohit。我会努力找到解决办法,希望对其他人有帮助。为你们大家干杯:)你们是个好人。不要删除,随它去吧。很高兴与您交谈:)
<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.
private boolean displayCursor;
private boolean cursorOn;
private String terminalText;

private TextView terminal; // The TextView Object
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();
}
    cursorOn = false;
    displayCursor = true;
    runCursorThread();