Java 编辑具有特定模式的文本
我正在尝试对Java 编辑具有特定模式的文本,java,android,design-patterns,android-edittext,Java,Android,Design Patterns,Android Edittext,我正在尝试对EditText字段进行编码,使其具有特定的模式。模式是: 第20页_ 我想要的是,当用户开始键入时,字符将取代下划线,但前斜杠仍保留 我是一个初学者,但这似乎是相当具有挑战性的 以下是我到目前为止尝试过的,它在开始时给出了P20,但字符不能代替下划线: editPemacNo.setText("P20__/__/____"); Selection.setSelection(editPemacNo.getText(), editPemacNo.getText().length
EditText
字段进行编码,使其具有特定的模式。模式是:
第20页_
我想要的是,当用户开始键入时,字符将取代下划线,但前斜杠仍保留
我是一个初学者,但这似乎是相当具有挑战性的
以下是我到目前为止尝试过的,它在开始时给出了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());
}
}
});
有人能帮我吗?此格式将提示添加到编辑文本框“P20\uuu/\ uu/\ uuuu!” 像
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/editbox"
android:hint="P20_ _ / _ _ / _ _ _ _"/>
我找到了一个解决方案: 使用这个android库- 以下是我在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="_"/>
希望这能帮助任何遇到同样问题的人 尝试以下操作:
1) 活动类-----------
2) layout2.xml-----------------
3) 输出:
我理解android:hint,但当用户进入编辑文本框时,该文本框消失,字段为空。我需要在键入@Android Team时保留预先固定的模式我在键入时关闭它,然后字符进入模式,但下划线不会消失。此时,不可能在值中使用下划线,因为如果定义任何文本,则需要使用下划线,然后将其覆盖。啊,好的,那么,我如何在没有下划线的情况下编写它,但保留正斜杠呢?
public class MnnActivity extends AppCompatActivity {
private EditText edt1;
private EditText edt2;
private EditText edt3;
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.layout2);
edt1 = (EditText) findViewById(R.id.edt1);
edt2 = (EditText) findViewById(R.id.edt2);
edt3 = (EditText) findViewById(R.id.edt3);
edt1.setFilters(new InputFilter[] {
new InputFilter.LengthFilter(2)});
edt1.addTextChangedListener(new TextWatcher() {
@Override
public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {
}
@Override
public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {
}
@Override
public void afterTextChanged(Editable editable) {
if(editable.toString().length() > 1) {
edt2.requestFocus();
openKeyboard();
}
}
});
edt2.setFilters(new InputFilter[] {
new InputFilter.LengthFilter(2)});
edt2.addTextChangedListener(new TextWatcher() {
@Override
public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {
}
@Override
public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {
}
@Override
public void afterTextChanged(Editable editable) {
if(editable.toString().length() > 1) {
edt3.requestFocus();
openKeyboard();
}
}
});
edt3.setFilters(new InputFilter[] {
new InputFilter.LengthFilter(4)});
edt3.addTextChangedListener(new TextWatcher() {
@Override
public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {
}
@Override
public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {
}
@Override
public void afterTextChanged(final Editable editable) {
if(editable.toString().length() > 3){
removeKeyboard();
}
}
});
}
private void openKeyboard(){
InputMethodManager imm = (InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE);
if(imm != null) {
imm.toggleSoftInput(InputMethodManager.SHOW_FORCED, InputMethodManager.HIDE_IMPLICIT_ONLY);
}
}
private void removeKeyboard(){
InputMethodManager imm = (InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE);
if(imm != null) {
imm.hideSoftInputFromWindow(getCurrentFocus().getWindowToken(), 0);
}
}
}
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:weightSum="100"
android:layout_marginTop="5dp"
android:layout_marginBottom="5dp"
android:layout_height="match_parent">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="5dp"
android:layout_gravity="center"
android:gravity="center"
android:layout_weight="15"
android:text="P20"/>
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/edt1"
android:layout_weight="15"
android:layout_gravity="center"
android:gravity="center"
android:background="@android:color/transparent"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="12.5"
android:layout_gravity="center"
android:gravity="center"
android:text="/"/>
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/edt2"
android:layout_gravity="center"
android:gravity="center"
android:layout_weight="15"
android:background="@android:color/transparent"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="12.5"
android:layout_gravity="center"
android:gravity="center"
android:text="/"/>
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/edt3"
android:layout_weight="30"
android:layout_gravity="center"
android:gravity="center"
android:layout_marginEnd="5dp"
android:background="@android:color/transparent"/>
</LinearLayout>