问题描述
我搜索类似我的问题,每一个问题,但没有得到它的工作。我的问题是这样的:
我想格式化字符串的EditText
打字时。格式是这样的(它总是一个19位的数字):
012345 01 0123456789 0
正如你可以看到我要加空格的时候需要当用户输入他们。我知道,我必须使用 TextWatcher
,但我所做的一切我没有得到我想要的。
修改
下面是我最后一次尝试的code:
@覆盖
公共无效afterTextChanged(编辑S){
如果(s.length()== 7 || s.length()== 10 || s.length()== 21){
editText.removeTextChangedListener(本);
字符串为newValue;
为newValue = s.insert((s.length() - 1),)的ToString();
//Log.d("AFTER",newValue);
editText.setText(newValue)以;
editText.setSelection(newValue.length());
editText.addTextChangedListener(本);
}
}
在这里,你去用它。
main.xml中:
< LinearLayout中的xmlns:机器人=http://schemas.android.com/apk/res/android
机器人:layout_width =match_parent
机器人:layout_height =match_parent>
<的EditText
机器人:ID =@ + ID / EDITTEXT
机器人:layout_width =match_parent
机器人:layout_height =WRAP_CONTENT
机器人:位数=0123456789
机器人:inputType =号/>
< / LinearLayout中>
MainActivity.java:
公共类MainActivity延伸活动{
INT长度限制:Textlength = 0;
的EditText EDITTEXT;
@覆盖
保护无效的onCreate(包savedInstanceState){
super.onCreate(savedInstanceState);
的setContentView(R.layout.activity_main);
EDITTEXT =(EditText上)findViewById(R.id.editText);
editText.addTextChangedListener(新TextWatcher()
{
公共无效afterTextChanged(编辑S)
{
}
公共无效beforeTextChanged(CharSequence中,诠释开始,
诠释计数,诠释后)
{
}
公共无效onTextChanged(CharSequence中,诠释开始,
INT之前,诠释计数)
{
字符串文本= editText.getText()的toString()。
。=长度限制:Textlength editText.getText()长度();
如果(text.endsWith())
返回;
如果(==长度限制:Textlength 7 || ==长度限制:Textlength 10 || ==长度限制:Textlength 21)
{
editText.setText(新的StringBuilder(文本).insert(text.length() - 1,)的ToString());
editText.setSelection(editText.getText()长度());
}
}});
}
}
在这样,我刚才设法将在特定的时间间隔的数字之间加空格。
注意:我添加额外的功能到EditText上,这样只有数字可以进入,并在同一时间的数字键盘只能在默认情况下弹出。欲了解更多的方式为用户输入的类型,this可能会帮助你。
I searched every question similar to my problem but didn't get it working. My problem is this:
I want to format a string in EditText
while typing. The format is this (it's always a 19 digit number):
012345 01 0123456789 0
As you can see I want to add spaces when they are needed while the user is typing. I know that I have to use the TextWatcher
but everything I do I don't get what i want.
Edit:
Here is the code of my last try:
@Override
public void afterTextChanged(Editable s) {
if(s.length() == 7 || s.length() == 10 || s.length() == 21){
editText.removeTextChangedListener(this);
String newValue;
newValue= s.insert((s.length()-1), " ").toString();
//Log.d("AFTER",newValue);
editText.setText(newValue);
editText.setSelection(newValue.length());
editText.addTextChangedListener(this);
}
}
Here you go with it.
main.xml:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<EditText
android:id="@+id/editText"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:digits="0123456789"
android:inputType="number" />
</LinearLayout>
MainActivity.java:
public class MainActivity extends Activity {
int textlength = 0;
EditText editText;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
editText = (EditText)findViewById(R.id.editText);
editText.addTextChangedListener(new TextWatcher()
{
public void afterTextChanged(Editable s)
{
}
public void beforeTextChanged(CharSequence s, int start,
int count, int after)
{
}
public void onTextChanged(CharSequence s, int start,
int before, int count)
{
String text = editText.getText().toString();
textlength = editText.getText().length();
if(text.endsWith(" "))
return;
if(textlength == 7 || textlength == 10 || textlength == 21)
{
editText.setText(new StringBuilder(text).insert(text.length()-1, " ").toString());
editText.setSelection(editText.getText().length());
}
}});
}
}
In this way, I have just managed to add spaces between the digits at particular intervals.
Note: I have added extra features to the edittext, so that only numbers can be entered and at the same time the number keyboard only pops up by default. For more on the way for the type of user inputs, this might help you.
这篇关于EditText上自定义字符串格式化的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!