日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

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

發布時間:2023/12/15 编程问答 28 豆豆
生活随笔 收集整理的這篇文章主要介紹了 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

創作挑戰賽新人創作獎勵來咯,堅持創作打卡瓜分現金大獎

總結

以上是生活随笔為你收集整理的android input鼠标坐标,android - 如何在EditText中设置光标位置?的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。