我正在尝试编写一个 EditText
字段以具有特定的模式。模式是:
P20_ _ / _ _ / _ _ _ _
我想要的是,当用户开始键入时,字符会取代下划线,但保留正斜杠。
我是初学者,但这似乎很有挑战性。
这是我到目前为止所尝试过的,它在开头给出了 P20,但字符并没有取代下划线:
editPemacNo.setText("P20__/__/____");
Selection.setSelection(editPemacNo.getText(), editPemacNo.getText().length());
editPemacNo.addTextChangedListener(new TextWatcher() {
@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
// TODO Auto-generated method stub
}
@Override
public void beforeTextChanged(CharSequence s, int start, int count,
int after) {
// TODO Auto-generated method stub
}
@Override
public void afterTextChanged(Editable s) {
if(!s.toString().contains("P20")){
editPemacNo.setText("P20__/__/____");
Selection.setSelection(editPemacNo.getText(), editPemacNo.getText().length());
}
}
});
有人可以帮助我吗?
最佳答案
我找到了解决方案:
使用这个 Android 库 - https://github.com/PaXLiCh/FormattEditText .
这是我在 XML 中使用的代码:
<ru.kolotnev.formattedittext.MaskedEditText
android:id="@+id/editTextPemac"
android:layout_width="115dp"
android:layout_height="45dp"
android:layout_marginStart="11dp"
android:layout_marginTop="7dp"
android:layout_toEndOf="@+id/spinnerOptions"
android:background="@drawable/edittext"
android:maxLines="1"
android:textSize="15sp"
android:textAlignment="center"
app:mask="P20**/**/****"
app:placeholder="_"/>
希望这对遇到同样问题的人有所帮助!
关于java - 具有特定模式的编辑文本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50924187/