Java 在编辑文本的日期中加斜杠

Java 在编辑文本的日期中加斜杠,java,android,android-activity,android-edittext,Java,Android,Android Activity,Android Edittext,我读了很多类似的问题,但没有人回答我的问题,也没有人能解决我的问题。 我在布局中有这样的编辑文本: 我需要的是,当用户写月份和年份的数字时,在他写的时候会出现或消失斜杠。如果他写了两个数字,那么需要出现斜杠。如果他删除了,并且只有一个数字,那么斜杠需要消失 我需要在编辑文本中显示日期:14/06 这是我的代码,但它不工作 受保护的void onCreateBundle savedInstanceState{ EditText editText2=EditText findViewByIdR.id

我读了很多类似的问题,但没有人回答我的问题,也没有人能解决我的问题。 我在布局中有这样的编辑文本:

我需要的是,当用户写月份和年份的数字时,在他写的时候会出现或消失斜杠。如果他写了两个数字,那么需要出现斜杠。如果他删除了,并且只有一个数字,那么斜杠需要消失

我需要在编辑文本中显示日期:14/06

这是我的代码,但它不工作

受保护的void onCreateBundle savedInstanceState{ EditText editText2=EditText findViewByIdR.id.editText2; editText2.addTextChangedListenernew TextWatcher{ @凌驾 public void onTextChangedCharSequence s,int start,int before,int count{ } @凌驾 public void beforeTextChangedCharSequence s,int start,int count,int after{ } @凌驾 公共无效后文本更改可编辑文本{ 如果text.length==2{ text.追加“/”; } } }; 仅供参考,我制作了一个类来解决这个问题,谢谢

将您的逻辑添加到ContextChanged而不是PostTextChanged中

请尝试以下代码:

editText2 .addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }
            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
                String str=editText2 .getText().toString();
                int textLength=editText2 .getText().length();
                if (textLength == 3) {
                    if (!str.contains("/")) {
                        editText2 .setText(new StringBuilder(editText2 .getText().toString()).insert(str.length() - 1, "/").toString());
                        editText2 .setSelection(editText2 .getText().length());
                    }
                }
            }

            @Override
            public void afterTextChanged(Editable s) {

            }
        });
我希望它对您有用。

将您的逻辑添加到ContextChanged而不是PostTextChanged中

请尝试以下代码:

editText2 .addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }
            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
                String str=editText2 .getText().toString();
                int textLength=editText2 .getText().length();
                if (textLength == 3) {
                    if (!str.contains("/")) {
                        editText2 .setText(new StringBuilder(editText2 .getText().toString()).insert(str.length() - 1, "/").toString());
                        editText2 .setSelection(editText2 .getText().length());
                    }
                }
            }

            @Override
            public void afterTextChanged(Editable s) {

            }
        });
我希望它对您有用。

使用蒙面编辑文本:

在Moudle build gradle中添加:

allprojects {
    repositories {
        maven { url 'https://jitpack.io' }
    }
}
dependencies {
     implementation 'com.github.santalu:maskara:1.0.0'
}
应用内构建渐变添加:

allprojects {
    repositories {
        maven { url 'https://jitpack.io' }
    }
}
dependencies {
     implementation 'com.github.santalu:maskara:1.0.0'
}
在布局中,您可以使用MaskEditText:

<com.santalu.maskedittext.MaskEditText
    android:id="@+id/editText2"
    android:layout_width="248dp"
    android:layout_height="59dp"
    android:layout_marginStart="21dp"
    android:layout_marginTop="36dp"
    android:width="360dp"
    android:ems="5"
    android:hint="@string/ultimos4Dig"
    android:inputType="number"
    android:maxLength="10"
    android:textSize="24sp"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toTopOf="parent"
    app:mask="##/##"/>
使用遮罩编辑文本:

在Moudle build gradle中添加:

allprojects {
    repositories {
        maven { url 'https://jitpack.io' }
    }
}
dependencies {
     implementation 'com.github.santalu:maskara:1.0.0'
}
应用内构建渐变添加:

allprojects {
    repositories {
        maven { url 'https://jitpack.io' }
    }
}
dependencies {
     implementation 'com.github.santalu:maskara:1.0.0'
}
在布局中,您可以使用MaskEditText:

<com.santalu.maskedittext.MaskEditText
    android:id="@+id/editText2"
    android:layout_width="248dp"
    android:layout_height="59dp"
    android:layout_marginStart="21dp"
    android:layout_marginTop="36dp"
    android:width="360dp"
    android:ems="5"
    android:hint="@string/ultimos4Dig"
    android:inputType="number"
    android:maxLength="10"
    android:textSize="24sp"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toTopOf="parent"
    app:mask="##/##"/>

谢谢你,几天后我得到了解决方案,这门课:

公共类EditMMYY扩展AppCompativeEditText实现TextWatcher { 私有字符串sPrev=; 私有整数iMon=0; 私密情报=0; 私有无效初始值 { 设置InputType InputType.TYPE\类别\编号; setFiltersnew InputFilter[]{new InputFilter.LengthFilter5}; setHintMM/YY; } 公共编辑上下文 { 超级上下文; 初始值; } 公共编辑MMYY上下文,属性集属性 { 超级上下文; 初始值; } public EditMMYYContext上下文,AttributeSet attrs,int defStyleAttr { 超级上下文,attrs,defStyleAttr; 初始值; } @凌驾 public void onTextChangedCharSequence s,int start,int before,int count { //Chequeo que el-Ingreo sea MM/YY 字符串sNew=s.toString; int newLen=新长度; ifsNew.equalssPrev { 回来 } 开关纽伦 { 案例0: iMon=0; iYear=0; sPrev=sNew; 打破 案例1: iMon=Integer.parseIntsNew; iYear=0; ifsPrev.length==0&&iMon>1 {//Si-se描述了一个城市的市长que 1,lo tomo como-mes sPrev=字符串。格式%02d/,iMon; } 其他的 { sPrev=sNew; } 打破 案例2: iMon=Integer.parseIntsNew; iYear=0; ifsPrev.length==1 { //我是瓦利多人,我是康普莱托人,中国人 //在坎比奥斯的斯普雷夫·斯普雷夫·斯普雷夫·斯普雷夫·斯普雷夫·斯普雷夫·斯普雷夫·斯普雷夫·斯普雷夫·斯普雷夫·斯普雷夫·斯普雷夫·斯普雷
ifiMon>=1&&iMon谢谢你,几天后我得到了解决方案,本课程:

公共类EditMMYY扩展AppCompativeEditText实现TextWatcher { 私有字符串sPrev=; 私有整数iMon=0; 私密情报=0; 私有无效初始值 { 设置InputType InputType.TYPE\类别\编号; setFiltersnew InputFilter[]{new InputFilter.LengthFilter5}; setHintMM/YY; } 公共编辑上下文 { 超级上下文; 初始值; } 公共编辑MMYY上下文,属性集属性 { 超级上下文; 初始值; } public EditMMYYContext上下文,AttributeSet attrs,int defStyleAttr { 超级上下文,attrs,defStyleAttr; 初始值; } @凌驾 public void onTextChangedCharSequence s,int start,int before,int count { //Chequeo que el-Ingreo sea MM/YY 字符串sNew=s.toString; int newLen=新长度; ifsNew.equalssPrev { 回来 } 开关纽伦 { 案例0: iMon=0; iYear=0; sPrev=sNew; 打破 案例1: iMon=Integer.parseIntsNew; iYear=0; ifsPre v、 长度==0&&iMon>1 {//Si-se描述了一个城市的市长que 1,lo tomo como-mes sPrev=字符串。格式%02d/,iMon; } 其他的 { sPrev=sNew; } 打破 案例2: iMon=Integer.parseIntsNew; iYear=0; ifsPrev.length==1 { //我是瓦利多人,我是康普莱托人,中国人 //在坎比奥斯的斯普雷夫·斯普雷夫·斯普雷夫·斯普雷夫·斯普雷夫·斯普雷夫·斯普雷夫·斯普雷夫·斯普雷夫·斯普雷夫·斯普雷夫·斯普雷夫·斯普雷
ifiMon>=1&&iMon我认为这在某种程度上解决了问题。将app:endIconMode=clear\u text添加到TextInputlayout

 dateOfBirth.getEditText().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) {
                if (charSequence.length() == 2 || charSequence.length() == 5) {
                    if (!Pattern.compile("([0-9]{2})/").matcher(charSequence).matches()) {
                        dateOfBirth.getEditText().setText(new StringBuilder(dateOfBirth.getEditText().getText().toString()).insert(charSequence.length(), "/").toString());
                        dateOfBirth.getEditText().setSelection(dateOfBirth.getEditText().getText().length());
                    }
                }
            }

            @Override
            public void afterTextChanged(Editable editable) {

            }
        });


我认为这在某种程度上解决了问题。将app:endIconMode=clear\u text添加到TextInputlayout

 dateOfBirth.getEditText().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) {
                if (charSequence.length() == 2 || charSequence.length() == 5) {
                    if (!Pattern.compile("([0-9]{2})/").matcher(charSequence).matches()) {
                        dateOfBirth.getEditText().setText(new StringBuilder(dateOfBirth.getEditText().getText().toString()).insert(charSequence.length(), "/").toString());
                        dateOfBirth.getEditText().setSelection(dateOfBirth.getEditText().getText().length());
                    }
                }
            }

            @Override
            public void afterTextChanged(Editable editable) {

            }
        });


改变安卓系统:从数字输入到“文本”,看看会发生什么。嗨!如果我这样做,键盘会变成字母而不是数字。我认为日期不是数字。为什么不改用日期选择器呢?嗨,谢谢你的回复,因为我的老板让我这样做。用数字键盘插入一个数字可能会重复改变安卓系统d:把数字输入到“文本”中,看看会发生什么。嗨!如果我这样做,键盘会变成字母而不是数字。我认为日期不是数字。为什么不使用日期选择器呢?嗨,谢谢你的回答,因为我的老板告诉我这样做。用数字键盘插入一个数字可能是Hello的重复,它不是大量的slashsHello,its doesnhi!编写了大量的slashsHi,当我尝试实现它时,当我编译时显示此错误:Manifest merge failed:Attributeapplication@appComponentFactoryvalue=android.support.v4.app.CoreComponentFactory,来自[com.android.support:support compat:28.0.0]AndroidManifest.xml:22:18-91,也位于[androidx.core:core:1.0.1]AndroidManifest.xml:22:18-86 value=androidx.core.app.CoreComponentFactory。建议:在AndroidManifest.xml:6:5-28:19处的元素中添加“tools:replace=android:appComponentFactory”以覆盖。嗨,当我尝试实现此功能时,在编译时显示此错误:Manifest合并失败:属性application@appComponentFactoryvalue=android.support[com.android.support:support compat:28.0.0]AndroidManifest.xml:22:18-91也出现在[androidx.core:core:1.0.1]AndroidManifest.xml:22:18-86 value=androidx.core.app.CoreComponentFactory中。建议:将“tools:replace=android:appComponentFactory”添加到AndroidManifest.xml:6:5-28:19处的元素以覆盖。