Android EditText Multiline does not work as it should

你说的曾经没有我的故事 提交于 2019-12-01 02:54:20

问题


I'm pretty desperate about this feature. I tried pretty much everything there is to find to made these EditTexts multiline enabled, but they just keep going on a single line scrolling the entire EditText with it.

How hard can it be to stop at the end of the border of the EditText and move to the next line?

I have this activity with an EditText and 2 buttons. One of these buttons adds a predetermined line of text to the EditText. The other puts the EditText's text into some form of object that I use later in the app.

However I can't get this multiline feature to work.. I've tried limiting the size. Setting the multiline flag. Disabling singleline. Giving lines, and minLines a random number (10). Disabling horizontalscroll on the EditText. But nothing works....

Can anyone tell me what the hell I'm doing wrong? And how I can fix this horrid abomination of an EditText.

This is how my nightmare looks like now.

    <EditText
        android:id="@+id/callofedittext"
        android:layout_width="wrap_content"
        android:inputType="textMultiLine"
        android:width="300dp"
        android:minLines="10"
        android:layout_height="wrap_content"
        android:gravity="top|left"
        android:textColor="@color/textWhite"
        android:background="@color/textBlack"
        android:paddingLeft="3dp"
        android:singleLine="false"
        android:scrollHorizontally="false"

        >

        <requestFocus />
    </EditText>

It haunts my dreams...

EDIT: > Light at the end of the tunnel.

While I was focussing on the xml.. A new clean project pointed out to me that EditText textMessage = (EditText)findViewById(R.id.callofedittext); textMessage.setInputType(InputType.TYPE_TEXT_FLAG_MULTI_LINE | InputType.TYPE_TEXT_FLAG_CAP_SENTENCES); is causing all of my problems. Not specifically the properties inside the xml.


回答1:


From this comment, the inputType was set in the code as well with:

textMessage.setInputType(InputType.TYPE_TEXT_FLAG_MULTI_LINE |
                         InputType.TYPE_TEXT_FLAG_CAP_SENTENCES);

This is actually not correct, because TYPE_TEXT_FLAG_MULTI_LINE and TYPE_TEXT_FLAG_CAP_SENTENCES are only flags, and do not contain the actual input type. In order for them to work, they must be layered as flags on top of InputType.TYPE_CLASS_TEXT. Without this type class flag, the edit text does not have a base input type class to apply your flags to, and defaults to no specified input type.

So, the correct way to set the input type with both of these flags is:

textMessage.setInputType(InputType.TYPE_CLASS_TEXT |
                         InputType.TYPE_TEXT_FLAG_MULTI_LINE |
                         InputType.TYPE_TEXT_FLAG_CAP_SENTENCES);

For official details on how these flags work, see the Android Developer Docs on InputType and TextView - android:inputType

I'm not sure why the design decision is this. Personally, I think they should have hidden how they are representing their flags (as ints/bit flags), and instead had enums and/or subclasses of InputType for their public interface.




回答2:


hey you have to add the following code in xml file ..

android:gravity="top"
android:maxLines="4"
android:inputType="textMultiLine"
android:scrollbars="vertical"
android:textSize="16sp"
android:padding="10dp"

and you have to put activity file ...

edtComment = (EditText) findViewById(R.id.edtComment);
edtComment.setMovementMethod(new ScrollingMovementMethod());

this is works for me and hope it will works for you .....




回答3:


Have you tried using

android:layout_height="wrap content"

instead of

android:layout_height="match_parent"


来源:https://stackoverflow.com/questions/25294498/android-edittext-multiline-does-not-work-as-it-should

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