安卓:Edittext-获取当前行 [英] Android: Edittext- get current line

查看:460
本文介绍了安卓:Edittext-获取当前行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在一个EditText有获取光标的当前行的方法?如果没有,我会写我自己的方法,但只是想检查。如果我写我自己的方法是最好的方法,是通过每一个字符的EditText上,直到selectionstart和计数\ñ的使用For循环的数量,还是有更好的办法?谢谢!

In an edittext is there a method for getting the current line of the cursor? If not I will write my own method, but just wanted to check. If I do write my own method would the best method be to go through every character in the edittext until selectionstart and count the number of \n's using a For loop, or is there a better way? Thanks!

推荐答案

只是为了让人们知道:

有一个更好的办法做到这一点,然后道格·保罗曾建议使用 getLineForOffset(选择)

There is a better way to do this then Doug Paul has suggested by using the getLineForOffset(selection):

public int getCurrentCursorLine(EditText editText)
{    
    int selectionStart = Selection.getSelectionStart(editText.getText());
    Layout layout = editText.getLayout();

    if (!(selectionStart == -1)) {
        return layout.getLineForOffset(selectionStart);
    }

    return -1;
}

这篇关于安卓:Edittext-获取当前行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆