EditText上不会换行它的文本 [英] EditText won't wrap its text

查看:138
本文介绍了EditText上不会换行它的文本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道有很多的相关的#1的问题,但我想这样做是写在其中任何一个,到目前为止,我无法得到这个工作。一切

I know there are lots of related questions in Stackoverflow, but I tried to do everything that's written in any of them and so far I couldn't get this working.

我的问题很简单。我有一个Android应用程序,我有一个框,用户将输入文本。不过,我不希望这样的文字为多(它不应该有它的\\ n),但我想它来包装,如果行的大小大于视域更大。所以基本上,我要的是相同的行为在Gmail主题字段文本框了。

My problem is simple. I have an android application, and I have a box where the user will input text. However, I don't want this text to be multiline (it shouldn't have "\n" in it), but I want it to wrap if the size of the line is greater than the viewport. So basically, what I want is the same behaviour that the textBox on Gmail Subject field has.

这是我的整个观点:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
          android:layout_width="fill_parent"
          android:layout_height="match_parent"
          android:orientation="vertical">

<TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/question"
        android:textAppearance="@style/titleFont"/>

<EditText
    android:id="@+id/edit_question_value"
    android:layout_width="match_parent"
    android:layout_height="0dp"
    android:layout_weight="0.3"
    android:gravity="top"
    android:inputType="textImeMultiLine|textEmailSubject"
    android:maxLength="70"
        >

</EditText>

<TextView
        android:id="@+id/textView2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/answer"
        android:textAppearance="@style/titleFont"/>

<EditText
        android:id="@+id/edit_question_answer"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:gravity="top"
        android:layout_weight="0.7"
        android:inputType="textMultiLine"/>

<LinearLayout
        android:id="@+id/linearLayout1"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_gravity="center_horizontal">

    <Button
            android:id="@+id/save_question"
            android:layout_width="120dp"
            android:layout_height="wrap_content"
            android:layout_gravity="center_horizontal"
            android:layout_weight="0.5"
            android:text="@string/save"/>

    <Button
            android:id="@+id/cancel_edit_question"
            android:layout_width="120dp"
            android:layout_height="wrap_content"
            android:layout_weight="0.5"
            android:text="@string/cancel"/>
</LinearLayout>

我想有这种行为的文字是这么做的用id等于@ + ID / edit_question_value。到目前为止,我已经能够要么使之真正多行(因此,用户可以输入,如果他想进入)或单行和文本不会换行。也许有些围绕文本的元素是有一些干扰,这就是为什么我包括整个视图。

The text I want to have this behaviour is theone with the id equals to @+id/edit_question_value. So far, I have been able to either make it truly multiline (so the user can type enter if he wants) or single-line and the text won't wrap. Perhaps some of the elements around the text are having some interference, and that's why I included the whole view.

没有人知道发生了什么事?

Does anyone knows what is happening?

推荐答案

这是东西,有前打扰了很多人,当我需要的时候我能找到答案。在纯XML,那是。如果我们可以用编程并不仅仅是标记,我们可以用一个小的破解的,如果你可以做到这一点。基本上,我们宣布一个多EDITTEXT,然后抓住回车键,以避免新的生产线。

This is something that has bother a lot of people before and I could find an answer when I needed it. In pure XML, that is. If we can use programming and not just markup, we can do this with a little hack if you may. Basically we are declaring a multiLine editText and then catching the return key to avoid new lines.

首先在申报的 XML

<EditText
    android:id="@+id/noReturnEditText"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:imeOptions="actionDone"
    android:inputType="textMultiLine|textCapSentences"
    android:text="lorem ipsum \n dolor site amet helping stackoverflow" />

是的,我知道你在想什么但是,嘿,胡安的,这是一个多线性的EditText ,我想一个单行像Gmail的主题区。我知道,我知道,但我们要照顾它,现在,通过删除我们不想多行部分的行为,同时保持文字环绕我们的想要的。

Yes, I know what you're thinking "But hey, Juan this is a multiliner EditText and I want a one liner like on the subject area of gmail". I know, I know, but we are going to take care of it right now, by removing the behavior of the multiline part we don't want, while keeping the text wrapping we do want.

现在在 code其余

EditText noReturnEditText = (EditText) findViewById(R.id.noReturnEditText);
         noReturnEditText.setOnKeyListener(new OnKeyListener(){
            @Override
            public boolean onKey(View v, int keyCode, KeyEvent event) {
                if (keyCode == KeyEvent.KEYCODE_ENTER) return true;
                return false;
            }
        });

您或许可以读取code很容易,但如果有人需要一个解释,我们正在建立一个 OnKeyListener 的EditText 我们想成为ONELINE-multilinelike,如果用户presses返回键(KEY code_ENTER)我们告诉该事件不会返回传播在这种情况下意味着我们已经处理的事件。

You can probably read the code quite easily, but in case someone needs an explanation, we are setting an OnKeyListener to the EditText we want to be "oneline-multilinelike" and if the user presses the return key (KEYCODE_ENTER) we tell the event not to propagate by returning true which in this context means we've handled the event.

结果:

注意事项

pressing回车键是不是进入一个新行的唯一途径。用户可以复制/粘贴一些文本并与单行的EditText多行条目结束。别担心!之前,我们存放或类似使用它我们可以去除值

Pressing the return key is not the only way of entering a new line. The user could copy/paste some text and end up with a multi-line entry on a single-line EditText. Worry not! We can strip the value before we store it or use it with something like

myData的= rawText.replace(System.getProperty(line.separator),);

如果我有很多的的EditText 的意见?

What if I have lots of EditText views?

好了,你基本上有两种选择。做一个一个拉你的眼睛做这样的事情,其上市的意见和循环为每个视图。

Well, you basically have two options. Do it one by one and pull your eyes out or do something like this, its listing the views and looping for each view.

//Declare the reusable listener
  OnKeyListener myListener = new OnKeyListener(){
        @Override
        public boolean onKey(View v, int keyCode, KeyEvent event) {
            if (keyCode == KeyEvent.KEYCODE_ENTER) return true;
            return false;
        }
    }  
//Loop through the views
    int viewIds[] = {R.id.edittext1,R.id.edittext2,R.id.edittext3,R.id.edittext4};
    for(int id : viewIds) ((EditText)findViewById).setOnKeyListener(myListener);

这篇关于EditText上不会换行它的文本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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