2016-06-14 3 views
0

で文字列の書式を設定します。どのように私は、文字列は次のように同じように私のEditTextに表示されている必要EDITTEXT

"AA-BB-11"が、私は唯一のAABB11の内側に置く必要がある、それは可能ですか?

+0

あなたが持っている ' "AA-BB-11"'でtext.length() == 3 || text.length() == 7を変更する必要があり、あなたは 'AABB11'をしたいですか?それとも別の方法ですか? – Sunshinator

答えて

2

私はTextWatcherを実装する必要があると思います。そのようなことを試してみてください:

@Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_main); 

     EditText test = (EditText) findViewById(R.id.editText) 
     test.addTextChangedListener(this); 
    } 

    @Override 
    public void beforeTextChanged(CharSequence s, int start, int count, int after) { 

    } 

    @Override 
    public void onTextChanged(CharSequence s, int start, int before, int count) { 

    } 

    @Override 
    public void afterTextChanged(Editable text) { 
     if (text.length() == 2 || text.length() == 5) { 
     text.append('-'); 
     } 
    } 

参照:https://developer.android.com/reference/android/text/TextWatcher.html

EDIT:https://stackoverflow.com/a/28043245/6450234

あなたがここに完全なコードを見ることができます。

は、あなただけのtext.length() == 2 || text.length() == 5

関連する問題