android input鼠标坐标,android - 如何在EditText中设置光标位置?
android - 如何在EditText中設(shè)置光標(biāo)位置?
有兩個(gè)EditText,在加載頁(yè)面時(shí),第一個(gè)EditText中設(shè)置了一個(gè)文本,所以現(xiàn)在光標(biāo)將在EditText的起始位置,我想在第二個(gè)EditText中設(shè)置光標(biāo)位置,其中不包含任何數(shù)據(jù)。 這該怎么做?
19個(gè)解決方案
406 votes
position是int的位置:
editText1.setSelection(position)
NotACleverMan answered 2019-03-11T21:01:57Z
91 votes
我已經(jīng)這樣做了以編程方式更新EditText的文本后將光標(biāo)位置設(shè)置為文本的結(jié)尾這里,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中設(shè)置EditText光標(biāo)位置
Code下面是將光標(biāo)設(shè)置為EditText:
EditText editText = (EditText)findViewById(R.id.edittext_id);
editText.setSelection(0);
Code下面是將光標(biāo)設(shè)置為EditText的結(jié)尾:
EditText editText = (EditText)findViewById(R.id.edittext_id);
editText.setSelection(editText.getText().length());
下面的代碼是在第2個(gè)字符位置后設(shè)置光標(biāo):
EditText editText = (EditText)findViewById(R.id.edittext_id);
editText.setSelection(2);
IntelliJ Amiya answered 2019-03-11T21:03:16Z
14 votes
我想在edittext中設(shè)置不包含數(shù)據(jù)的光標(biāo)位置
在一個(gè)空的EditText中只有一個(gè)位置,它是setSeletion(0)。
或者你的意思是當(dāng)你的活動(dòng)開(kāi)始時(shí)你想要關(guān)注你的requestFocus()? 在那種情況下,它的requestFocus()
Reno answered 2019-03-11T21:03:53Z
10 votes
使用以下行
e2.setSelection(e2.length());
e2是編輯文本對(duì)象名稱
Syed Danish Haider answered 2019-03-11T21:04:25Z
9 votes
讓editText2是你的第二個(gè)EditText視圖。然后在onResume()中放入以下代碼片段
editText2.setFocusableInTouchMode(true);
editText2.requestFocus();
或者說(shuō)
在第二個(gè)EditText視圖的xml布局中。
monish george answered 2019-03-11T21:05:02Z
6 votes
一段時(shí)間編輯文本光標(biāo)donot來(lái)自特定位置,如果我們直接使用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)中的方法應(yīng)該允許你這樣做。
Avinash answered 2019-03-11T21:05:52Z
4 votes
提醒:如果您使用edittext.setSelection()來(lái)設(shè)置光標(biāo),并且在設(shè)置alertdialog時(shí)它不起作用,請(qǐng)確保在創(chuàng)建對(duì)話框后設(shè)置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
此代碼將幫助您將光標(biāo)顯示在編輯文本的最后位置。
editText.requestFocus();
editText.setSelection(editText.length());
Kailas Bhakade answered 2019-03-11T21:07:13Z
1 votes
我不會(huì)直接得到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之后設(shè)置光標(biāo)從右到左然后你必須這樣做。
edittext.setSelection(edittext.length()-n);
如果是edittext的文字就好
version
并且您想要將光標(biāo)從右側(cè)移動(dòng)到第6個(gè)位置
然后它會(huì)移動(dòng)光標(biāo) -
version
^
Abu Mohammad Rasel answered 2019-03-11T21:08:26Z
0 votes
將光標(biāo)設(shè)置為行和列
您可以使用以下代碼獲取EditText中與某個(gè)行和列對(duì)應(yīng)的位置。 然后,您可以使用getTrueLineCount()設(shè)置光標(biāo)位置。可以對(duì)以下方法進(jìn)行調(diào)用:
getTrueLineCount()轉(zhuǎn)到第x行的第y列
getTrueLineCount()轉(zhuǎn)到第x行的最后一列
getTrueLineCount()轉(zhuǎn)到最后一行的y列
getTrueLineCount()轉(zhuǎn)到最后一行的最后一列
處理所有行和列邊界; 輸入大于行長(zhǎng)度的列將返回該行最后一列的位置。 輸入大于EditText行數(shù)的行將轉(zhuǎn)到最后一行。 它經(jīng)過(guò)嚴(yán)格測(cè)試后應(yīng)該足夠可靠。
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;
}
這個(gè)問(wèn)題已經(jīng)得到了回答,但我認(rèn)為有人可能想要這樣做。
它通過(guò)循環(huán)遍歷每個(gè)字符來(lái)工作,每次找到行分隔符時(shí)遞增行數(shù)。 當(dāng)行計(jì)數(shù)等于所需行時(shí),它將返回當(dāng)前索引+列,如果列超出范圍,則返回行結(jié)束索引。 您還可以重用getTrueLineCount()方法,它返回一個(gè)忽略文本換行的行數(shù),與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中設(shè)置光標(biāo)位置? 嘗試以下代碼
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中,您可以創(chuàng)建一個(gè)這樣的擴(kuò)展函數(shù):
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
我相信最簡(jiǎn)單的方法就是使用填充。
在你的xml的edittext部分中說(shuō),添加????機(jī)器人:paddingLeft=“100dp”這將從右端開(kāi)始移動(dòng)光標(biāo)100dp的起始位置。
同樣的方式,你可以使用????機(jī)器人:paddingRight=“100dp”這將從右端向左移動(dòng)光標(biāo)100dp的結(jié)束位置。
有關(guān)更多詳細(xì)信息,請(qǐng)查看我的博客上的這篇文章:Android:在EditText小部件中設(shè)置光標(biāo)開(kāi)始和結(jié)束位置
Arthur Wang answered 2019-03-11T21:12:43Z
創(chuàng)作挑戰(zhàn)賽新人創(chuàng)作獎(jiǎng)勵(lì)來(lái)咯,堅(jiān)持創(chuàng)作打卡瓜分現(xiàn)金大獎(jiǎng)總結(jié)
以上是生活随笔為你收集整理的android input鼠标坐标,android - 如何在EditText中设置光标位置?的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: 1个高速收费站1年能收多少通行费?博主:
- 下一篇: gear s3刷android wear