【问题标题】:How to display cursor at the end of text in TextView?如何在 TextView 的文本末尾显示光标?
【发布时间】:2013-08-27 06:07:27
【问题描述】:

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

有人知道解决办法吗?

【问题讨论】:

  • 你不能...... TextView 不是为了从用户那里获取输入......
  • 您应该使用 EditText。显示光标。
  • 你们不知道你们在说什么。任何视图都可以被覆盖以接受输入。

标签: android


【解决方案1】:

首先,您应该使用EditText 代替TextView 来获取输入。如果光标仍然不闪烁,请在xml file 中设置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>

在您的活动类中,也添加此代码。

EditText input = (EditText)findViewById(R.id.edittext1);
input.setSelection(input.getText().length());

【讨论】:

  • 有趣的反对票。投反对票的人会介意说出投反对票的原因吗?
  • 你的答案是否将光标放在末尾?
  • 试试对我有用。如果您对这个答案投了反对票,那么其他人也会给出几乎相同的答案。也对他们的答案投反对票。
  • 我想要它用于 TextView 兄弟..而不是用于 EditText。
  • 你不能为 textView 这样做。如果你想让 textView 可编辑,为什么不使用 EditText 来代替
【解决方案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();

【讨论】:

    【解决方案3】:

    我认为你应该选择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());
    

    输出

    【讨论】:

    • 您认为您建议的答案会将光标放在末尾吗?
    • 他想要的是at the end of the text in TextView。所以文本的结尾意味着不在我亲爱的朋友的右边。如果我没记错的话。在投票之前你应该小心。您应该在此之前尝试代码。
    • 很好,但是你们只是改变了主意,这不是溶胶,即使他最初提出的问题也没有解决方案。投反对票的原因是,如果像你这样的人(猜你是 wiki 成员)会开始这样回答,那么这个网站就会失去魅力。我也可以回答这个问题,但我没有比给出错误的 sol 更好的说法。
    • 即使我同意@mohit。我将尝试找到解决方案,希望对其他人有帮助。向你们所有人干杯..:)
    • 你是个好人。不删,顺其自然。很高兴与您交谈:)
    【解决方案4】:

    最后按照 @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.
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-04-26
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多