Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/212.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 键入时在edittext上放置一个字符_Android - Fatal编程技术网

Android 键入时在edittext上放置一个字符

Android 键入时在edittext上放置一个字符,android,Android,我有一个EditText,我想用它来输入日期,我想当用户插入日期时,我的应用程序会自动在EditText上添加“-”,所以用户只需要插入数字, 前 我试过这个 edtTxt1.addTextChangedListener(new TextWatcher() { public void onTextChanged(CharSequence s, int start, int before, int count) { } @Override p

我有一个EditText,我想用它来输入日期,我想当用户插入日期时,我的应用程序会自动在EditText上添加“-”,所以用户只需要插入数字, 前

我试过这个

edtTxt1.addTextChangedListener(new TextWatcher() {

    public void onTextChanged(CharSequence s, int start, int before,
          int count) {
    }

    @Override
    public void afterTextChanged(Editable s) {
          // TODO Auto-generated method stub
          Integer textlength1 = edtTxt1.getText().length();
          if (textlength1 == 2) {
                edtTxt1.getText().insert(2, "-");
                edtTxt1.setSelection(3);
                /*I also tried this, no luck
                edtTxt1.setSelection(edtTxt1.getText().length());*/
          }
    }

    @Override
    public void beforeTextChanged(CharSequence s, int start, int count,
           int after) {
           // TODO Auto-generated method stub
       }
    });
结果:

user type: 21   -> device automatically 21-
user type: 01   -> device automatically 2101-
我不能将光标放在“-”之后

编辑 我尝试了@svenoaks-answer,我能够将光标放在“-”之后,但结果如下:

user type: 21   -> device automatically 21-
user type: 0    -> device automatically 21-210

我想你可以试试
edtTxt1.setText(“…”)
我想你可以试试
edtTxt1.setText(“…”)
我想你可以试试
edtTxt1.setText(“…”)

我想你可以试试
edtTxt1.setText(“…”)
你知道吗:你可以随时使用日期选择器从用户那里获取日期输入

假设要打印dd-mm-yyyy,请尝试以下操作

if(textlength1==2||textlength1==5||textlength1==7)
edtTxt1.setText(edtTxt1.getText().insert(textlength1, "-"));

您知道吗:您可以始终使用DatePicker从用户处获取日期输入

假设要打印dd-mm-yyyy,请尝试以下操作

if(textlength1==2||textlength1==5||textlength1==7)
edtTxt1.setText(edtTxt1.getText().insert(textlength1, "-"));

您知道吗:您可以始终使用DatePicker从用户处获取日期输入

假设要打印dd-mm-yyyy,请尝试以下操作

if(textlength1==2||textlength1==5||textlength1==7)
edtTxt1.setText(edtTxt1.getText().insert(textlength1, "-"));

您知道吗:您可以始终使用DatePicker从用户处获取日期输入

假设要打印dd-mm-yyyy,请尝试以下操作

if(textlength1==2||textlength1==5||textlength1==7)
edtTxt1.setText(edtTxt1.getText().insert(textlength1, "-"));

DatePicker对我来说太大了,我想使用EditText,因为它更适合我的应用。这里改进了我的答案。祝你好运:)同样的事情也会发生,就像svenoaks的答案一样,但因为它很有用,我会投票给你的答案,我会找到另一种方法,thanksDatePicker对我来说太大了,我想使用EditText,因为它更适合我的应用。这里改进了我的答案。祝你好运:)同样的事情也会发生,就像svenoaks的答案一样,但因为它很有用,我会投票给你的答案,我会找到另一种方法,thanksDatePicker对我来说太大了,我想使用EditText,因为它更适合我的应用。这里改进了我的答案。祝你好运:)同样的事情也会发生,就像svenoaks的答案一样,但因为它很有用,我会投票给你的答案,我会找到另一种方法,thanksDatePicker对我来说太大了,我想使用EditText,因为它更适合我的应用。这里改进了我的答案。祝你好运:)同样的事情也会发生,就像斯维诺克的答案一样,但因为它很有用,我会投票支持你的答案,我会找到另一种方法,谢谢