[英]How set the string format in 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
编辑:
您可以在此处查看完整代码: https : //stackoverflow.com/a/28043245/6450234 。
你只需要改变text.length() == 3 || text.length() == 7
text.length() == 3 || text.length() == 7
by text.length() == 2 || text.length() == 5
text.length() == 2 || text.length() == 5
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.