Android EditText多行无法正常工作 [英] Android EditText Multiline does not work as it should

查看:207
本文介绍了Android EditText多行无法正常工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我非常渴望这个功能. 为了使这些EditTexts多行启用,我几乎尝试了所有事情, 但他们只是继续沿一行滚动整个EditText.

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.

要停在EditText边框的尽头并移至下一行有多难?

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

我有一个带有EditText和2个按钮的活动.这些按钮之一将预定的文本行添加到EditText.另一个将EditText的文本放到某种形式的对象中,稍后我将在应用程序中使用它.

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.

但是,我无法使用此多行功能..我尝试限制大小.设置多行标志.禁用单行.给行和minLines一个随机数(10). 在EditText上禁用水平滚动.但是什么都行不通...

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....

有人可以告诉我我到底在做什么错吗?以及如何解决EditText的这种令人讨厌的厌恶.

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

这就是我现在的噩梦.

    <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...

>隧道尽头的光.

当我专注于xml时.一个新的干净项目向我指出,EditText textMessage = (EditText)findViewById(R.id.callofedittext); textMessage.setInputType(InputType.TYPE_TEXT_FLAG_MULTI_LINE | InputType.TYPE_TEXT_FLAG_CAP_SENTENCES);引起了我所有的问题.并非是xml中的属性.

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.

推荐答案

来自此

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);

这实际上是不正确的,因为TYPE_TEXT_FLAG_MULTI_LINETYPE_TEXT_FLAG_CAP_SENTENCES只是标志,并且不包含实际的输入类型.为了使它们起作用,必须将它们分层放置为InputType.TYPE_CLASS_TEXT上方的标志.如果没有此类型类标志,则编辑文本不具有可将您的标志应用到的基本输入类型类,并且默认为没有指定的输入类型.

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);

有关这些标志如何工作的官方详细信息,请参见 TextView-android:inputType

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

我不确定为什么要这样设计.就我个人而言,我认为他们应该隐藏它们如何表示其标志(如ints/bit标志),而应该为它们的公共接口使用Enum枚举和/或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.

这篇关于Android EditText多行无法正常工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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