0
で文字列の書式を設定します。どのように私は、文字列は次のように同じように私のEditTextに表示されている必要EDITTEXT
"AA-BB-11"
が、私は唯一のAABB11
の内側に置く必要がある、それは可能ですか?
で文字列の書式を設定します。どのように私は、文字列は次のように同じように私のEditTextに表示されている必要EDITTEXT
"AA-BB-11"
が、私は唯一のAABB11
の内側に置く必要がある、それは可能ですか?
私は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
あなたが持っている ' "AA-BB-11"'で
text.length() == 3 || text.length() == 7
を変更する必要があり、あなたは 'AABB11'をしたいですか?それとも別の方法ですか? – Sunshinator