Ran*_*ku' 65 android multiline android-edittext
如何在Android上创建一个EditText,使得用户可能不会输入多行文本,但显示仍然是多行的(即有文字换行而不是文本转到右侧)?
它类似于内置的SMS应用程序,我们无法输入换行符,但文本以多行显示.
Ton*_*ony 46
我将子窗口小部件并覆盖键事件处理以阻止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);
}
}
Run Code Online (Sandbox Code Playgroud)
And*_*lph 42
这是一种方法,您不必重写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();
}
});
Run Code Online (Sandbox Code Playgroud)
ser*_*y.n 24
XML中的属性
android:lines="5"
android:inputType="textPersonName"
Run Code Online (Sandbox Code Playgroud)
sog*_*ger 14
这个对我有用:
<EditText
android:inputType="textShortMessage|textMultiLine"
android:minLines="3"
... />
Run Code Online (Sandbox Code Playgroud)
它显示一个笑脸而不是Enter键.
这是一个更正确的答案,不会在 IME 键盘上显示回车键:
// IMPORTANT, do this before any of the code following it
myEditText.setSingleLine(true);
// IMPORTANT, to allow wrapping
myEditText.setHorizontallyScrolling(false);
// IMPORTANT, or else your edit text would wrap but not expand to multiple lines
myEditText.setMaxLines(6);
Run Code Online (Sandbox Code Playgroud)
此外,您可以setSingleLine(true)
使用android:inputType
XML 布局文件或setInputType(InputType.*)
代码中的显式替换,其中使用的输入类型是您知道将输入限制为单行的任何内容(即,任何setSingleLine(true)
已经隐式调用的内容)。
解释:
什么setSingleLine(true)
是调用setHorizontallyScrolling(true)
和setLines(1)
隐式,同时更改一些 IME 键盘设置以禁用回车键。
反过来,调用 tosetLines(1)
就像调用setMinLines(1)
和setMaxLines(1)
在一个调用中。
某些输入类型(即来自 的常量InputType.TYPE_*
)setSingleLine(true)
隐式调用,或至少达到相同的效果。
结论:
因此,为了实现 OP 想要的,我们只需通过恢复那些隐式调用来对抗那些隐式设置。
@Andreas Rudolph提供的答案包含一个严重的错误,不应该使用.IndexOutOfBoundsException
当您在EditText
其中包含多个换行符的文本时,代码会导致该错误.这是由使用的循环类型引起的,Editable
对象将在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;
}
}
}
});
Run Code Online (Sandbox Code Playgroud)
试试这个:
@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
if (keyCode == KeyEvent.KEYCODE_ENTER)
{
//Nothing
return true;
}
return super.onKeyDown(keyCode, event);
}
Run Code Online (Sandbox Code Playgroud)
小智 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);
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
60805 次 |
最近记录: |