android input鼠标坐标,android - 如何在EditText中设置光标位置?

android - 如何在EditText中设置光标位置?

有两个EditText,在加载页面时,第一个EditText中设置了一个文本,所以现在光标将在EditText的起始位置,我想在第二个EditText中设置光标位置,其中不包含任何数据。 这该怎么做?

19个解决方案

406 votes

position是int的位置:

editText1.setSelection(position)

NotACleverMan answered 2019-03-11T21:01:57Z

91 votes

我已经这样做了以编程方式更新EditText的文本后将光标位置设置为文本的结尾这里,etmsg是EditText

etmsg.setText("Updated Text From another Activity");

int position = etmsg.length();

Editable etext = etmsg.getText();

Selection.setSelection(etext, position);

MKJParekh answered 2019-03-11T21:02:21Z

28 votes

如何在Android中设置EditText光标位置

Code下面是将光标设置为EditText:

EditText editText = (EditText)findViewById(R.id.edittext_id);

editText.setSelection(0);

Code下面是将光标设置为EditText的结尾:

EditText editText = (EditText)findViewById(R.id.edittext_id);

editText.setSelection(editText.getText().length());

下面的代码是在第2个字符位置后设置光标:

EditText editText = (EditText)findViewById(R.id.edittext_id);

editText.setSelection(2);

IntelliJ Amiya answered 2019-03-11T21:03:16Z

14 votes

我想在edittext中设置不包含数据的光标位置

在一个空的EditText中只有一个位置,它是setSeletion(0)。

或者你的意思是当你的活动开始时你想要关注你的requestFocus()? 在那种情况下,它的requestFocus()

Reno answered 2019-03-11T21:03:53Z

10 votes

使用以下行

e2.setSelection(e2.length());

e2是编辑文本对象名称

Syed Danish Haider answered 2019-03-11T21:04:25Z

9 votes

让editText2是你的第二个EditText视图。然后在onResume()中放入以下代码片段

editText2.setFocusableInTouchMode(true);

editText2.requestFocus();

或者说

在第二个EditText视图的xml布局中。

monish george answered 2019-03-11T21:05:02Z

6 votes

一段时间编辑文本光标donot来自特定位置,如果我们直接使用editText.setSelection(position);。在这种情况下,你可以尝试

editText.post(new Runnable() {

@Override

public void run() {

editText.setSelection(string.length());

}

});

dev.sourabh answered 2019-03-11T21:05:27Z

5 votes

setSelection(int index) setSelection(int index)中的方法应该允许你这样做。

Avinash answered 2019-03-11T21:05:52Z

4 votes

提醒:如果您使用edittext.setSelection()来设置光标,并且在设置alertdialog时它不起作用,请确保在创建对话框后设置selection()

例:

AlertDialog dialog = builder.show();

input.setSelection(x,y);

calav3ra answered 2019-03-11T21:06:20Z

2 votes

记得在编辑文本setSelection之前致电requestFocus()。

Dong Thang answered 2019-03-11T21:06:47Z

2 votes

此代码将帮助您将光标显示在编辑文本的最后位置。

editText.requestFocus();

editText.setSelection(editText.length());

Kailas Bhakade answered 2019-03-11T21:07:13Z

1 votes

我不会直接得到setSelection()方法,所以我完成了如下所示   工作就像魅力

EditText editText = (EditText)findViewById(R.id.edittext_id);

editText.setText("Updated New Text");

int position = editText.getText().length();

Editable editObj= editText.getText();

Selection.setSelection(editObj, position);

sujith s answered 2019-03-11T21:07:40Z

1 votes

如果要在n之后设置光标从右到左然后你必须这样做。

edittext.setSelection(edittext.length()-n);

如果是edittext的文字就好

version

并且您想要将光标从右侧移动到第6个位置

然后它会移动光标 -

version

^

Abu Mohammad Rasel answered 2019-03-11T21:08:26Z

0 votes

将光标设置为行和列

您可以使用以下代码获取EditText中与某个行和列对应的位置。 然后,您可以使用getTrueLineCount()设置光标位置。可以对以下方法进行调用:

getTrueLineCount()转到第x行的第y列

getTrueLineCount()转到第x行的最后一列

getTrueLineCount()转到最后一行的y列

getTrueLineCount()转到最后一行的最后一列

处理所有行和列边界; 输入大于行长度的列将返回该行最后一列的位置。 输入大于EditText行数的行将转到最后一行。 它经过严格测试后应该足够可靠。

static final String LINE_SEPARATOR = System.getProperty("line.separator");

int getIndexFromPos(int line, int column) {

int lineCount = getTrueLineCount();

if (line < 0) line = getLayout().getLineForOffset(getSelectionStart()); // No line, take current line

if (line >= lineCount) line = lineCount - 1; // Line out of bounds, take last line

String content = getText().toString() + LINE_SEPARATOR;

int currentLine = 0;

for (int i = 0; i < content.length(); i++) {

if (currentLine == line) {

int lineLength = content.substring(i, content.length()).indexOf(LINE_SEPARATOR);

if (column < 0 || column > lineLength) return i + lineLength; // No column or column out of bounds, take last column

else return i + column;

}

if (String.valueOf(content.charAt(i)).equals(LINE_SEPARATOR)) currentLine++;

}

return -1; // Should not happen

}

// Fast alternative to StringUtils.countMatches(getText().toString(), LINE_SEPARATOR) + 1

public int getTrueLineCount() {

int count;

String text = getText().toString();

StringReader sr = new StringReader(text);

LineNumberReader lnr = new LineNumberReader(sr);

try {

lnr.skip(Long.MAX_VALUE);

count = lnr.getLineNumber() + 1;

} catch (IOException e) {

count = 0; // Should not happen

}

sr.close();

return count;

}

这个问题已经得到了回答,但我认为有人可能想要这样做。

它通过循环遍历每个字符来工作,每次找到行分隔符时递增行数。 当行计数等于所需行时,它将返回当前索引+列,如果列超出范围,则返回行结束索引。 您还可以重用getTrueLineCount()方法,它返回一个忽略文本换行的行数,与TextView.getLineCount()不同。

Nicolas answered 2019-03-11T21:09:46Z

0 votes

EditText editText = findViewById(R.id.editText);

editText.setSelection(editText.getText().length());

Satendra Behre answered 2019-03-11T21:10:07Z

0 votes

如果要在EditText中设置光标位置? 尝试以下代码

EditText rename;

String title = "title_goes_here";

int counts = (int) title.length();

rename.setSelection(counts);

rename.setText(title);

Mujahid khan answered 2019-03-11T21:10:34Z

-1 votes

在kotlin中,您可以创建一个这样的扩展函数:

fun EditText.placeCursorAtLast() {

val string = this.text.toString()

this.setSelection(string.length)

}

然后只需致电myEditText.placeCursorAtLast()

notdrone answered 2019-03-11T21:11:22Z

-1 votes

在Xml文件中android:paddingLeft:

android:id="@+id/txt_amount"

android:layout_width="match_parent"

android:layout_height="wrap_content"

android:paddingLeft="10dp"/>

我希望这能帮助你

Mohamed Ben Romdhane answered 2019-03-11T21:11:56Z

-2 votes

我相信最简单的方法就是使用填充。

在你的xml的edittext部分中说,添加    机器人:paddingLeft=“100dp”这将从右端开始移动光标100dp的起始位置。

同样的方式,你可以使用    机器人:paddingRight=“100dp”这将从右端向左移动光标100dp的结束位置。

有关更多详细信息,请查看我的博客上的这篇文章:Android:在EditText小部件中设置光标开始和结束位置

Arthur Wang answered 2019-03-11T21:12:43Z

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值