在Android的编辑文本设置光标​​位置 [英] Set cursor position in android Edit text

查看:238
本文介绍了在Android的编辑文本设置光标​​位置的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我如何可以设置的android编辑文本光标位置?我有一个编辑文本和创造我想要设置光标在一定时间到底有时在文字中间,我能做些什么?

我的编辑文本如下:

 机器人:ID =@ + ID / editText11
机器人:layout_width =WRAP_CONTENT
机器人:layout_height =200SP
机器人:layout_above =@ + ID / horizo​​ntalScrollView1
机器人:layout_alignParentLeft =真
机器人:layout_alignParentRight =真
机器人:layout_alignParentTop =真
机器人:重力=顶
机器人:后台=#00000000
机器人:inputType =textMultiLine
机器人:单线=真
 

解决方案

您可以通过实现这一点:

  Edittext.setSelection(Edittext.length());
 

这将使最终的光标位置。否则,你可以得到一个字符串文本,并检查要在其中把光标文字位置,像这样

 字符串文本= edittext.gettext.tostring()

的for(int i = 0; I< text.length();我++)
{
//这里的逻辑来检查文本的位置
}
 

然后

  Edittext.setSelection(位置);
 

How can i set the cursor position in android edit text? I have an edit text and on create i want to set the cursor to some times in the end some times in the middle of the text, what can i do?

my edit text is as follows:

android:id="@+id/editText11"
android:layout_width="wrap_content"
android:layout_height="200sp"
android:layout_above="@+id/horizontalScrollView1"
android:layout_alignParentLeft="true"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true"
android:gravity="top"
android:background="#00000000"
android:inputType="textMultiLine"
android:singleLine="true"

解决方案

You can achieve this by using :

Edittext.setSelection(Edittext.length());

this will put the cursor position in the end. otherwise you can get the text in a string and check the text position where you want to put the cursor and do it like this

String text = edittext.gettext.tostring()

for(int i = 0; i <text.length();i++)
{
//your logic here to check the position of text
}

and then

Edittext.setSelection(position);

这篇关于在Android的编辑文本设置光标​​位置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆