具有特定模式的EditText

时间:2018-06-19 08:46:44

标签: java android design-patterns android-edittext

我正在尝试将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());

            }

        }
    });

任何人都可以帮助我吗?

3 个答案:

答案 0 :(得分:0)

此格式添加到edittext框中的提示" P20_ _ / _ _ / _ _ _ _"

喜欢..

    <EditText
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:id="@+id/editbox"
    android:hint="P20_ _ / _ _ / _ _ _ _"/>

答案 1 :(得分:0)

我找到了一个解决方案:

使用此安卓库 - 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="_"/>

希望这可以帮助遇到同样问题的人!

答案 2 :(得分:0)

尝试以下方法:

1)MnnActivity.class -----------

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);
    }
}
}

2)layout2.xml -----------------

<?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>

3)输出:

Code Output