How to Check the number of lines in a String in Android?

て烟熏妆下的殇ゞ 提交于 2020-01-15 03:51:09

问题


I want to check the no of lines in my text view from the string I am going to pass to the test view. I have added android:maxLines = "2".... If it exceeds two i need to show/hide a view....How can I achieve this?


回答1:


Use a thread to count the number of lines,

 textView.setText("Text Here");
    textView.post(new Runnable() {

        @Override
        public void run() {

            Log.v("Line count: ", textView.getLineCount()+"");
        }
    });

If you want to limit the number of lines in TextView from xml then use android:maxLines




回答2:


I believe the TextView has a method getLineCount which you can use to get the number of lines. You can compare that function's return value with maxLines and show/hide if the limit is reached.

See also: TextView.getLine()

And: TextView - maxLines attribute



来源:https://stackoverflow.com/questions/22854554/how-to-check-the-number-of-lines-in-a-string-in-android

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!