本文介紹了防止在 EditText 上輸入鍵,但仍將文本顯示為多行的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學習吧!
問題描述
限時送ChatGPT賬號..
如何在 Android 上制作 EditText,這樣用戶可能不會輸入多行文本,但顯示仍然是多行的(即,有自動換行而不是向右移動的文本)?
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.
推薦答案
我將子類化小部件并覆蓋鍵事件處理以阻止 Enter
鍵:
I would subclass the widget and override the key event handling in order to block the Enter
key:
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);
}
}
這篇關于防止在 EditText 上輸入鍵,但仍將文本顯示為多行的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!
【網站聲明】本站部分內容來源于互聯網,旨在幫助大家更快的解決問題,如果有圖片或者內容侵犯了您的權益,請聯系我們刪除處理,感謝您的支持!