Prevent enter key on EditText but still show the t

2019-01-16 13:29发布

How do I make an EditText on Android such that the user may not enter a multi-line text, but the display is still multi-line (i.e. there is word-wrap instead of the text going over to the right)?

It's similar to the built-in SMS application where we can't input newline but the text is displayed in multiple lines.

18条回答
Melony?
2楼-- · 2019-01-16 14:03
<EditText 
  android:id="@+id/Msg"
  android:layout_width="fill_parent"
  android:layout_height="wrap_content"              
  android:layout_marginTop="5dip"
  android:lines="5"
  android:selectAllOnFocus="true"               
  android:hint="Skriv meddelande...\n(max 100tkn)"/>


EditText et = (EditText)findViewById(R.id.Msg);
String strTmp = et.getText().toString();
strTmp = strTmp.replaceAll("\\n"," ");
查看更多
smile是对你的礼貌
3楼-- · 2019-01-16 14:07

I will give another option so you don't have to subclass EditText. Create an InputFilter that filters out linebreaks. Then use EditText.addInputFilter.

Source code for such an input filter is here: https://gist.github.com/CapnSpellcheck/7c72830e43927380daf5205100c93977

You can pass 0 in the constructor, and it won't allow any newlines. Also, you can combine this with one of the other tweaks such as android:imeOptions="actionDone", as this will help improve the experience on some devices.

查看更多
Luminary・发光体
4楼-- · 2019-01-16 14:08

This is a method, where you don't have to override the EditText class. You just catch and replace the newlines with empty strings.

myEditTextObject.addTextChangedListener(new TextWatcher() {

    public void onTextChanged(CharSequence s, int start, int before, int count) {

    }

    public void beforeTextChanged(CharSequence s, int start, int count, int after) {

    }

    public void afterTextChanged(Editable s) {

        for(int i = s.length(); i > 0; i--) {

            if(s.subSequence(i-1, i).toString().equals("\n"))
                s.replace(i-1, i, "");
        }

        String myTextString = s.toString();
    }
});
查看更多
该账号已被封号
5楼-- · 2019-01-16 14:09

Adding this property to the EditText XML works for me:

android:lines="1"

It lets the users input newline characters but the EditText itself does not increase in height.

查看更多
该账号已被封号
6楼-- · 2019-01-16 14:12

The answer provided by @Andreas Rudolph contains a critical bug and shouldn't be used. The code causes an IndexOutOfBoundsException when you past text inside the EditText which contains multiple new-line characters. This is caused by the type of loop which is used, the Editable object will call the afterTextChanged method as soon as its contents change (replace, delete, insert).

Correct code:

edittext.addTextChangedListener(new TextWatcher() {

    public void onTextChanged(CharSequence s, int start, int before, int count) {

    }

    public void beforeTextChanged(CharSequence s, int start, int count, int after) {

    }

    public void afterTextChanged(Editable s) {
        /*
         * The loop is in reverse for a purpose,
         * each replace or delete call on the Editable will cause
         * the afterTextChanged method to be called again.
         * Hence the return statement after the first removal.
         * http://developer.android.com/reference/android/text/TextWatcher.html#afterTextChanged(android.text.Editable)
         */
        for(int i = s.length()-1; i >= 0; i--){
            if(s.charAt(i) == '\n'){
                s.delete(i, i + 1);
                return;
            }
        }
    }
});
查看更多
男人必须洒脱
7楼-- · 2019-01-16 14:14

I'm testing this and it seems to work:

EditText editText = new EditText(context);
editText.setSingleLine(false);
editText.setInputType(android.text.InputType.TYPE_CLASS_TEXT | android.text.InputType.TYPE_TEXT_VARIATION_EMAIL_SUBJECT);
查看更多
登录 后发表回答