首页 文章

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

提问于
浏览
196

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

20 回答

  • 3

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

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

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

  • 1

    使用以下行

    e2.setSelection(e2.length());
    

    e2 是编辑文本对象名称

  • -2

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

    editText2.setFocusableInTouchMode(true);
    editText2.requestFocus();
    

    或者说

    <requestFocus />
    

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

  • 10

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

    editText.post(new Runnable() {
                    @Override
                    public void run() {
                        editText.setSelection(string.length());
                    }
                });
    
  • 29

    Edittext 中的 setSelection(int index) 方法应该允许您这样做 .

  • 407

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

    例:

    AlertDialog dialog = builder.show();
    input.setSelection(x,y);
    
  • -1

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

    editText.requestFocus();
     editText.setSelection(editText.length());
    
  • 9

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

    以下代码是 Set cursor to StartingEditText

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

    以下代码是 EditTextSet cursor to end

    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);
    
  • 1

    请记住在 setSelection 之前为edittext调用 requestFocus() .

  • 4

    其中position是int:

    editText1.setSelection(position)
    
  • 2

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

    etmsg.setText("Updated Text From another Activity");
    int position = etmsg.length();
    Editable etext = etmsg.getText();
    Selection.setSelection(etext, position);
    
  • 5

    我不会直接得到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);
    
  • 14

    如果你想从右到左设置 n 字符之后的光标,那么你必须这样做 .

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

    如果是edittext的文字就好

    version<sub></sub>
    

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

    然后它会移动光标 -

    version<sub> </sub>
                    ^
    
  • 0

    将光标设置为行和列

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

    • getIndexFromPos(x, y) 转到第x行的第y列

    • getIndexFromPos(x, -1) 转到第x行的最后一列

    • getIndexFromPos(-1, y) 转到最后一行的y列

    • getIndexFromPos(-1, -1) 转到最后一行的最后一列

    处理所有行和列边界;输入大于行长度的列将返回该行最后一列的位置 . 输入大于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() 不同 .

  • 0
    EditText editText = findViewById(R.id.editText);
    editText.setSelection(editText.getText().length());
    
  • 6

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

    EditText rename;
     String title = "title_goes_here";
     int counts = (int) title.length();
     rename.setSelection(counts);
     rename.setText(title);
    
  • 0

    如果要将光标放在EditText上的某个位置,可以使用:

    yourEditText.setSelection(position);
    

    此外,还可以设置初始位置和最终位置,以便以编程方式选择一些文本,这样:

    yourEditText.setSelection(startPosition, endPosition);
    

    请注意,设置选择可能会很棘手,因为您可以将光标放在字符之前或之后,下图说明了在这种情况下如何编制索引:

    enter image description here

    因此,如果您希望光标位于文本末尾,只需将其设置为 yourEditText.length() 即可 .

  • 0

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

    fun EditText.placeCursorAtLast() {
        val string = this.text.toString()
        this.setSelection(string.length)
    }
    

    然后简单地调用 myEditText.placeCursorAtLast()

  • 91

    在Xml文件中android:paddingLeft:

    <EditText
        android:id="@+id/txt_amount"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:paddingLeft="10dp"/>
    

    我希望这能帮助你

  • -1

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

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

    同样的方法,您可以使用android:paddingRight =“100dp”这将从右端移动光标100dp的结束位置 .

    有关更多详细信息,请在我的博客上查看此文章:Android: Setting Cursor Starting and Ending Position in EditText Widget

相关问题