如何在Android上創建EditText,以便用戶不會輸入多行文本,但顯示仍然是多行的(即存在文字包裝,而不是文字向右)?防止在EditText上輸入鍵,但仍然顯示文本爲多行
它與內置SMS應用程序類似,我們無法輸入換行符,但文本顯示爲多行。
如何在Android上創建EditText,以便用戶不會輸入多行文本,但顯示仍然是多行的(即存在文字包裝,而不是文字向右)?防止在EditText上輸入鍵,但仍然顯示文本爲多行
它與內置SMS應用程序類似,我們無法輸入換行符,但文本顯示爲多行。
我會繼承窗口小部件並覆蓋關鍵事件,以阻止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);
}
}
試試這個:
@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
if (keyCode == KeyEvent.KEYCODE_ENTER)
{
//Nothing
return true;
}
return super.onKeyDown(keyCode, event);
}
不可以。這會將編輯限制爲單行。 OP需要多條線路;他只是不希望用戶能夠輸入換行符。 – 2011-05-20 10:59:21
看到我編輯的答案 – 2011-05-20 11:06:31
好的......我提議的同樣的東西 – 2011-05-20 11:11:46
這是一個方法,你不要必須重寫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();
}
});
+1這比我接受的答案更有用,原因很簡單,我可以使用它作爲TextWatcher的默認實現,它可以通過子類擴展(那麼只需要調用super.afterTextChanged(...)來保留檢查換行符。Works! – AgentKnopf 2012-07-11 10:42:23
For循環條件應該是'i> 0'而不是'i> = 0'。 ','s.subSequence(i-1,i)'得到一個'IndexOutOfBoundsException' – TalkLittle 2014-11-04 22:01:29
我認爲這會拋出一個IndexOutOfBoundsException,用於複製和粘貼多行新的行字符。 – Chriskot 2015-03-24 00:17:34
我測試這一點,似乎工作:
EditText editText = new EditText(context);
editText.setSingleLine(false);
editText.setInputType(android.text.InputType.TYPE_CLASS_TEXT | android.text.InputType.TYPE_TEXT_VARIATION_EMAIL_SUBJECT);
真棒謝謝。在XML中設置任何東西都會禁用我的ET單擊監聽器......所以這樣做並不是!鍵盤上的返回鍵變成「完成」按鈕。 (我只需要'editText.setSingleLine(false);')。 – Azurespot 2016-05-13 06:59:33
這完全取決於使用的鍵盤。許多人仍然會顯示一個輸入鍵 – 2017-06-15 21:01:02
添加這個屬性來EditText
XML工作對我來說:
android:lines="1"
它可以讓用戶輸入換行符但EditText
本身不增加身高。
對於URI你可以使用:
android:inputType="textUri"
android:lines="1"
android:maxLength="128"
否則android:inputType="textPersonName"
以上針對其他的EditText此用戶名的作品中提到,等
<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"," ");
接受的答案工作得很好,直到我用分行符將文本複製到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);
}
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;
}
.......
}
});
可以從這樣的XML設置:
android:imeOptions="actionDone"
android:inputType="text"
android:maxLines="10"
不要忘記android:inputType="text"
,如果不設置它,這是行不通的。我不知道爲什麼。 另外不要忘記將maxLines
更改爲您的首選值。
這完全取決於使用的鍵盤。許多人仍然會顯示一個輸入鍵 – 2017-06-15 21:01:21
由@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;
}
}
}
});
你應該編輯答案,而不是製作一個新答案。 – 2016-12-14 19:47:47
@RobertMartin我一直在想這個,並在Meta網站上尋求幫助,他們建議我應該發佈一個新的答案。因爲我所做的更改與作者的原始意圖相沖突。 – 2016-12-14 20:04:56
不錯!但是,如果您首先按Enter鍵,則在開始處仍會有一個新行(因此'i> = 0'應該可以工作)^^ – Fllo 2017-01-18 22:13:37
只需添加安卓的inputType = 「textPersonName」 的EditText上它會從按下進入 – Nepster 2014-05-21 11:06:48