Set limitation for EditText height and maximum lines according to the screen size

半城伤御伤魂 提交于 2019-12-10 21:05:35

问题


I have a notebook application and I want to limit my editText maximum lines according to the screen height. for example 40 lines in nexus-6p and 35 lines in nexus5 and etc.

So my question is how can I limit the editText lines? I don't have any idea for this.

UPDATE

I want to create new note page when cursor reached the last line.


回答1:


I Found the solution, thank everyone for the response.

My solution is:

int lineHeight = getLineHeight();
int height = getHeight();
int maximumLines = height / lineHeight();



回答2:


Check your screen size and put lines acording the height.

Display display = getWindowManager().getDefaultDisplay();
int height = display.getHeight();

//Calculate the lines with the height proportion

edittext.setMaxLines(lines);



回答3:


I think you should create different layout files based on the screen dimensions. In those layout files you can change the maxLine parameter for the EditText.

Check https://developer.android.com/guide/practices/screens_support.html




回答4:


You can get screen size in pixel by writing following code.

Display display = getWindowManager().getDefaultDisplay();
Point size = new Point();
display.getSize(size);
int width = size.x;
int height = size.y;

After that you can put the condition and then assign the EditText in following way

editText.setMaxLines();

Your problem will be solved

For more details of Screen size refer the link: Supporting Multiple Screens




回答5:


If your EditText just contains chars, use screenSize/editText.getLineHeight(). However, if it also contains some mark-ups(like Spannable), this method will be useless and there may not be a solution.



来源:https://stackoverflow.com/questions/42294307/set-limitation-for-edittext-height-and-maximum-lines-according-to-the-screen-siz

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