• <small id='TR1s5'></small><noframes id='TR1s5'>

    <i id='TR1s5'><tr id='TR1s5'><dt id='TR1s5'><q id='TR1s5'><span id='TR1s5'><b id='TR1s5'><form id='TR1s5'><ins id='TR1s5'></ins><ul id='TR1s5'></ul><sub id='TR1s5'></sub></form><legend id='TR1s5'></legend><bdo id='TR1s5'><pre id='TR1s5'><center id='TR1s5'></center></pre></bdo></b><th id='TR1s5'></th></span></q></dt></tr></i><div id='TR1s5'><tfoot id='TR1s5'></tfoot><dl id='TR1s5'><fieldset id='TR1s5'></fieldset></dl></div>
    1. <tfoot id='TR1s5'></tfoot>

          <bdo id='TR1s5'></bdo><ul id='TR1s5'></ul>
        <legend id='TR1s5'><style id='TR1s5'><dir id='TR1s5'><q id='TR1s5'></q></dir></style></legend>

        EditText 自定义字符串格式

        时间:2023-08-31

          <tbody id='rySMR'></tbody>
        • <bdo id='rySMR'></bdo><ul id='rySMR'></ul>
          <i id='rySMR'><tr id='rySMR'><dt id='rySMR'><q id='rySMR'><span id='rySMR'><b id='rySMR'><form id='rySMR'><ins id='rySMR'></ins><ul id='rySMR'></ul><sub id='rySMR'></sub></form><legend id='rySMR'></legend><bdo id='rySMR'><pre id='rySMR'><center id='rySMR'></center></pre></bdo></b><th id='rySMR'></th></span></q></dt></tr></i><div id='rySMR'><tfoot id='rySMR'></tfoot><dl id='rySMR'><fieldset id='rySMR'></fieldset></dl></div>
          <tfoot id='rySMR'></tfoot>

            <legend id='rySMR'><style id='rySMR'><dir id='rySMR'><q id='rySMR'></q></dir></style></legend>
          1. <small id='rySMR'></small><noframes id='rySMR'>

                • 本文介绍了EditText 自定义字符串格式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

                  问题描述

                  我搜索了与我的问题类似的每个问题,但没有得到解决.我的问题是这样的:

                  I searched every question similar to my problem but didn't get it working. My problem is this:

                  我想在输入时格式化 EditText 中的字符串.格式如下(始终是 19 位数字):

                  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

                  如您所见,我想在用户输入时添加空格.我知道我必须使用 TextWatcher 但我所做的一切都没有得到我想要的.

                  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.

                  编辑:

                  这是我上次尝试的代码:

                  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);
                              }
                          }
                  

                  推荐答案

                  给你.

                  ma​​in.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.

                  注意:我在edittext中添加了额外的功能,这样就只能输入数字,同时默认只会弹出数字键盘.有关用户输入类型的更多信息,this 可能会对你有所帮助.

                  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 自定义字符串格式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!

                  上一篇:xcode4模板格式问题 下一篇:Android 数字格式在某种程度上是错误的,而不是

                  相关文章

                  最新文章

                  • <bdo id='p9y4q'></bdo><ul id='p9y4q'></ul>

                  <small id='p9y4q'></small><noframes id='p9y4q'>

                  <legend id='p9y4q'><style id='p9y4q'><dir id='p9y4q'><q id='p9y4q'></q></dir></style></legend>
                  1. <i id='p9y4q'><tr id='p9y4q'><dt id='p9y4q'><q id='p9y4q'><span id='p9y4q'><b id='p9y4q'><form id='p9y4q'><ins id='p9y4q'></ins><ul id='p9y4q'></ul><sub id='p9y4q'></sub></form><legend id='p9y4q'></legend><bdo id='p9y4q'><pre id='p9y4q'><center id='p9y4q'></center></pre></bdo></b><th id='p9y4q'></th></span></q></dt></tr></i><div id='p9y4q'><tfoot id='p9y4q'></tfoot><dl id='p9y4q'><fieldset id='p9y4q'></fieldset></dl></div>
                    1. <tfoot id='p9y4q'></tfoot>