How to set cursor position in EditText?

前端 未结 23 1489
广开言路
广开言路 2020-11-28 02:51

There are two EditText,while loading the page a text is set in the first EditText, So now cursor will be in the starting place of EditText, I want

相关标签:
23条回答
  • 2020-11-28 03:09
    EditText editText = findViewById(R.id.editText);
    editText.setSelection(editText.getText().length());
    
    0 讨论(0)
  • 2020-11-28 03:11

    If you want to set cursor position in EditText? try these below code

    EditText rename;
     String title = "title_goes_here";
     int counts = (int) title.length();
     rename.setSelection(counts);
     rename.setText(title);
    
    0 讨论(0)
  • 2020-11-28 03:12

    Set cursor to a row and column

    You can use the following code to get the position in your EditText that corresponds to a certain row and column. You can then use editText.setSelection(getIndexFromPos(row, column)) to set the cursor position. The following calls to the method can be made:

    • getIndexFromPos(x, y) Go to the column y of line x
    • getIndexFromPos(x, -1) Go to the last column of line x
    • getIndexFromPos(-1, y) Go to the column y of last line
    • getIndexFromPos(-1, -1) Go to the last column of the last line

    All line and column bounds are handled; Entering a column greater than the line's length will return position at the last column of the line. Entering a line greater than the EditText's line count will go to the last line. It should be reliable enough as it was heavily tested.

    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;
    }
    

    The question was already answered but I thought someone could want to do that instead.

    It works by looping through each character, incrementing the line count every time it finds a line separator. When the line count equals the desired line, it returns the current index + the column, or the line end index if column is out of bounds. You can also reuse the getTrueLineCount() method, it returns a line count ignoring text wrapping, unlike TextView.getLineCount().

    0 讨论(0)
  • 2020-11-28 03:14

    use the below line

    e2.setSelection(e2.length());
    

    e2 is edit text Object Name

    0 讨论(0)
  • 2020-11-28 03:14

    as a reminder: if you are using edittext.setSelection() to set the cursor, and it is NOT working while setting up an alertdialog for example, make sure to set the selection() AFTER the dialog has been created

    example:

    AlertDialog dialog = builder.show();
    input.setSelection(x,y);
    
    0 讨论(0)
  • 2020-11-28 03:14

    I believe the most simple way to do this is just use padding.

    Say in your xml's edittext section, add android:paddingLeft="100dp" This will move your start position of cursor 100dp right from left end.

    Same way, you can use android:paddingRight="100dp" This will move your end position of cursor 100dp left from right end.

    For more detail, check this article on my blog: Android: Setting Cursor Starting and Ending Position in EditText Widget

    0 讨论(0)
提交回复
热议问题