java - 如何在文本中间聚焦
问题描述
我有一个EditText
多行由 Google 地址 API 生成的 Place + newline + Address 组成。
显示屏幕时,我想让文本光标位于地名末尾(第一行末尾)而不是EditText
.
String text = placeName + "\n" + addressName;
TextView tv = findViewById(R.id.placeAddress);
tv.setText(text);
解决方案
你说“光标”,但你有一个TextView
,而不是一个EditText
。如果您想要一个EditText
以便用户可以与之交互,您可以使用setSelection方法并传递您希望光标旁边的字符的索引
String text = placeName + "\n" + addressName;
EditText et = findViewById(R.id.placeAddress);
et.setText(text);
et.setSelection(placeName.length());
推荐阅读
- php - 覆盖 PHP OOP 中的方法
- amazon-web-services - 如何在 MWAA 中设置电子邮件警报?
- amazon - 需要从附加的 EBS (150GB) 扩展 lvm (centos-home)
- ios - 使用 mbuild 签署 Xamarin iOS 服务扩展
- asp.net-mvc - 检测 QueryString 并在任何请求 URL 上创建会话
- python - Python If 语句:根据 Pandas 数据框列中的文本运行不同的公式
- r - 将日期列转换为从整数 Rstudio 开始的天数
- c# - 使用临时表作为目标的 SQLBulkCopy - 随机抛出“无法访问目标表”
- php - 如何在编辑刀片 Laravel 中获取选定的复选框值
- visual-studio-code - VSCode:keybindings.json 不断被覆盖