code to append -(hyphen) to Text on EditText and allows to delete one digit at a time in Android

edt1=(EditText)findViewById(R.id.editText1);    
        edt1.addTextChangedListener(new TextWatcher() {
            public void afterTextChanged(Editable s) {
                //XXX do something                
            }
            public void beforeTextChanged(CharSequence s, int start, int count,int after) {
                    //XXX do something                
            }
            public void onTextChanged(CharSequence s, int start, int before, int count) {
                 ///Code  set listener for BackButton
                edt1.setOnKeyListener(new OnKeyListener() {                
                    @Override
                 public boolean onKey(View v, int keyCode, KeyEvent event) {
                     //You can identify which key pressed buy checking keyCode value with KeyEvent.KEYCODE_
                      if(keyCode != KeyEvent.KEYCODE_DEL && edt1.getText().length() !=0)
                      {
               
                if(edt1.getText().length()==5){
                String newtext = edt1.getText().toString() + "-";
                edt1.setText("");
                edt1.append(newtext);
                }
               
                      }                
                    
             return false ;      
                 }
         });}});       
        ///Code  set listener for BackButton
        edt1.setOnKeyListener(new OnKeyListener() {                
            @Override
            public boolean onKey(View v, int keyCode, KeyEvent event) {
                //You can identify which key pressed buy checking keyCode value with KeyEvent.KEYCODE_
                 if(keyCode == KeyEvent.KEYCODE_DEL && edt1.getText().length() !=0){
                     if(edt1.getText().length()==6)
                     {
                         String tempString=(edt1.getText().toString()).substring(0, edt1.getText().length()-2);
                         System.out.println("------tempString--"+tempString);
                         String newtext = tempString+edt1.getText().toString().charAt(edt1.getText().length()-2);
                         System.out.println("------newtext--"+newtext);
                         System.out.println("------char--"+edt1.getText().toString().charAt(edt1.getText().length()-2));
                         edt1.setText("");
                         edt1.append(newtext);
                     }                   
                     }               
            return false ;      
                }
        });

Comments

Popular posts from this blog

Custom Dialog with no title and full screen in Android

code to display .pdf,.text,.DOC,.DOCX,.doc extension files exists in SDCard

Best working code to set Date limits on Date Picker Dialog,in Android