简体   繁体   English

setInputType为EditText而不丢失多行功能?

[英]setInputType for EditText without losing multiLine feature?

For some reason using setInputType for EditText forces it into a single line. 由于某些原因,对EditText使用setInputType会将其强制为一行。 I need the EditText to accept NumberSigned . 我需要EditText接受NumberSigned

I tried using InputFilter such that it returns "" if source.charAt(i) is not 0-9 or dot or + or - 我尝试使用InputFilter ,如果source.charAt(i)不是0-9或点或+或-

But how to allow newline character? 但是如何允许换行符呢? I need to be able to enter in multiple lines in the EditText. 我需要能够在EditText中输入多行。

I tried both in xml and code: 我在xml和代码中都尝试过:

et = new EditText(this);

et.setText(getResources().getString(R.string.default_value));

et.setInputType(InputType.TYPE_NUMBER_FLAG_SIGNED);

et.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT,LinearLayout.LayoutParams.WRAP_CONTENT));

linearLayout.addView(et);

the setText enters a string: setText输入一个字符串:

<string name="default_value">1,2,3\n4,5,6\n7,8,9</string>

which is shown as 1 2 3 4 5 6 7 8 9. 显示为1 2 3 4 5 6 7 8 9。

I tried using setLines() and setMaxLines() but no luck. 我尝试使用setLines()setMaxLines()但是没有运气。 I think the reason it is forced into a single line is because newline character is not a part of InputType NumberSigned. 我认为将其强制为单行的原因是因为换行符不是InputType NumberSigned的一部分。

If you want input as signed integer, then you can use 如果要输入带符号整数,则可以使用

...

android:digits="0123456789 \\n"

...

PS : Don't forget the SPACE between 9 and \\n PS:不要忘记9\\n之间的空格

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM