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!
相关问题
- How can I create this custom Bottom Navigation on
- Bottom Navigation View gets Shrink Down
- How to make that the snackbar action button be sho
- Listening to outgoing sms not working android
- How to create Circular view on android wear?
相关文章
- android开发 怎么把图片放入drawable的文件夹下
- android上如何获取/storage/emulated/下的文件列表
- androidStudio有个箭头不认识
- SQLite不能创建表
- Windows - Android SDK manager not listing any plat
- Animate Recycler View grid when number of columns
- Why is the app closing suddenly without showing an
- Android OverlayItem.setMarker(): Change the marker
I can't find a simple way to get this information either, so your approach seems about right. Don't forget to check for the case where
getSelectionStart()
returns 0. You can make the code reusable by putting it in a static utility method, like this:The
countOccurrences()
method is from this question, but you should use one of the better answers to that question (e.g.StringUtils.countMatches()
from commons lang) if feasible.I have a full working example that demonstrates this method, so let me know if you need more help.
Hope this helps!
Just to let people know:
There is a better way to do this then Doug Paul has suggested by using the
getLineForOffset(selection)
:find the last index of "\n"using method lastindex=String.lastindexof("\n") then get a substring using method String.substring(lstindex,string.length).and you will get the last line in two lines of code.