2011-05-20 20 views
59

如何在Android上創建EditText,以便用戶不會輸入多行文本,但顯示仍然是多行的(即存在文字包裝,而不是文字向右)?防止在EditText上輸入鍵,但仍然顯示文本爲多行

它與內置SMS應用程序類似,我們無法輸入換行符,但文本顯示爲多行。

+9

只需添加安卓的inputType = 「textPersonName」 的EditText上它會從按下進入 – Nepster 2014-05-21 11:06:48

回答

43

我會繼承窗口小部件並覆蓋關鍵事件,以阻止Enter鍵操作:

class MyTextView extends EditText 
{ 
    ... 
    @Override 
    public boolean onKeyDown(int keyCode, KeyEvent event) 
    { 
     if (keyCode==KeyEvent.KEYCODE_ENTER) 
     { 
      // Just ignore the [Enter] key 
      return true; 
     } 
     // Handle all other keys in the default way 
     return super.onKeyDown(keyCode, event); 
    } 
} 
+4

如果這一行停止它:'返回super.onKeyDown(的keyCode,事件); '在onKeyDown或我想象? :P – necixy 2011-05-20 11:04:30

+0

錯字,感謝您的接收 – 2011-05-20 11:11:17

+0

我們都爲同一個社區工作。 不客氣。 :) – necixy 2011-05-20 11:16:54

5

試試這個:

@Override 
public boolean onKeyDown(int keyCode, KeyEvent event) { 
    if (keyCode == KeyEvent.KEYCODE_ENTER) 
    { 
     //Nothing 
     return true; 
    } 
    return super.onKeyDown(keyCode, event); 
} 
+2

不可以。這會將編輯限制爲單行。 OP需要多條線路;他只是不希望用戶能夠輸入換行符。 – 2011-05-20 10:59:21

+0

看到我編輯的答案 – 2011-05-20 11:06:31

+0

好的......我提議的同樣的東西 – 2011-05-20 11:11:46

37

這是一個方法,你不要必須重寫EditText類。你只需用空字符串捕捉並替換換行符。

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(); 
    } 
}); 
+3

+1這比我接受的答案更有用,原因很簡單,我可以使用它作爲TextWatcher的默認實現,它可以通過子類擴展(那麼只需要調用super.afterTextChanged(...)來保留檢查換行符。Works! – AgentKnopf 2012-07-11 10:42:23

+1

For循環條件應該是'i> 0'而不是'i> = 0'。 ','s.subSequence(i-1,i)'得到一個'IndexOutOfBoundsException' – TalkLittle 2014-11-04 22:01:29

+1

我認爲這會拋出一個IndexOutOfBoundsException,用於複製和粘貼多行新的行字符。 – Chriskot 2015-03-24 00:17:34

5

我測試這一點,似乎工作:

EditText editText = new EditText(context); 
editText.setSingleLine(false); 
editText.setInputType(android.text.InputType.TYPE_CLASS_TEXT | android.text.InputType.TYPE_TEXT_VARIATION_EMAIL_SUBJECT); 
+0

真棒謝謝。在XML中設置任何東西都會禁用我的ET單擊監聽器......所以這樣做並不是!鍵盤上的返回鍵變成「完成」按鈕。 (我只需要'editText.setSingleLine(false);')。 – Azurespot 2016-05-13 06:59:33

+0

這完全取決於使用的鍵盤。許多人仍然會顯示一個輸入鍵 – 2017-06-15 21:01:02

0

添加這個屬性來EditText XML工作對我來說:

android:lines="1" 

它可以讓用戶輸入換行符但EditText本身不增加身高。

23

屬性,XML

android:lines="5" 
android:inputType="textPersonName" 
+0

謝謝,最簡單!雖然我注意到它阻止了ET上的點擊監聽器正在工作......所以使用'editText.setSingleLine(false);'如果是這樣。 – Azurespot 2016-05-13 07:00:59

+3

android:inputType =「textPersonName」不起作用 – axd 2016-06-16 15:41:46

+0

這完全取決於使用的鍵盤。許多人仍然會顯示一個輸入鍵 – 2017-06-15 21:00:08

0

對於URI你可以使用:

android:inputType="textUri" 
android:lines="1" 
android:maxLength="128" 

否則android:inputType="textPersonName"以上針對其他的EditText此用戶名的作品中提到,等

0
<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"," "); 
13

這一個適合我:

<EditText 
    android:inputType="textShortMessage|textMultiLine" 
    android:minLines="3" 
    ... /> 

它顯示笑臉,而不是回車鍵。

+2

最佳答案,作品無需更改太多的代碼! – Jaydeep 2015-01-31 23:26:21

+2

從經驗來看,這不適用於每個鍵盤。我們周圍的HTC手機仍然顯示輸入密鑰。 – Treeline 2015-03-18 14:38:34

+0

這完全取決於使用的鍵盤。許多人仍然會顯示輸入鍵 – 2017-06-15 20:59:54

1

接受的答案工作得很好,直到我用分行符將文本複製到EditText中。所以我添加onTextContextMenuItem來監視粘貼操作。

@Override 
public boolean onTextContextMenuItem(int id) { 
    boolean ret = super.onTextContextMenuItem(id); 
    switch (id) { 
     case android.R.id.paste: 
      onTextPaste(); 
      break; 
    } 
    return ret; 
} 

public void onTextPaste() { 
    if (getText() == null) 
     return; 
    String text = getText().toString(); 
    text = text.replaceAll(System.getProperty("line.separator"), " "); 
    text = text.replaceAll("\\s+", " "); 
    setText(text); 
} 
0
EditText textView = new EditText(activity); 
    ... 
    textView.setOnEditorActionListener(new TextView.OnEditorActionListener() { 
     @Override 
     public boolean onEditorAction(TextView textView, int i, KeyEvent keyEvent) { 
      if(KeyEvent.KEYCODE_ENTER == keyEvent.getKeyCode()) { 
       return false; 
      } 
      ....... 

     } 
    }); 
3

可以從這樣的XML設置:

android:imeOptions="actionDone" 
android:inputType="text" 
android:maxLines="10" 

不要忘記android:inputType="text",如果不設置它,這是行不通的。我不知道爲什麼。 另外不要忘記將maxLines更改爲您的首選值。

+0

這完全取決於使用的鍵盤。許多人仍然會顯示一個輸入鍵 – 2017-06-15 21:01:21

3

只需添加

 android:singleLine="true" 

您的EditText

+1

這是棄用! – Blasco73 2017-05-17 17:15:27

3

由@Andreas魯道夫提供的答案包含一個關鍵的錯誤,不應該被使用。當您在EditText中包含多個換行符時,代碼會導致IndexOutOfBoundsException。這是由於使用的循環類型造成的,一旦內容更改(替換,刪除,插入),對象將立即調用afterTextChanged方法。

正確的代碼:

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; 
      } 
     } 
    } 
}); 
+0

你應該編輯答案,而不是製作一個新答案。 – 2016-12-14 19:47:47

+0

@RobertMartin我一直在想這個,並在Meta網站上尋求幫助,他們建議我應該發佈一個新的答案。因爲我所做的更改與作者的原始意圖相沖突。 – 2016-12-14 20:04:56

+1

不錯!但是,如果您首先按Enter鍵,則在開始處仍會有一個新行(因此'i> = 0'應該可以工作)^^ – Fllo 2017-01-18 22:13:37

相關問題