Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Custom format edit text input android to accept credit card number

Tags:

how to make edit text accept input in format

4digitnumber-4dignumber-4dignumber-4dignumber    

The code

text.addTextChangedListener(new TextWatcher() {     int len = 0;     String string ;     @Override      public void afterTextChanged(Editable s) {          text.setOnKeyListener(new OnKeyListener()         {   public boolean onKey(View v, int keyCode, KeyEvent event)             {                                   if (keyCode == KeyEvent.KEYCODE_DEL)                     {                      }                     else{                          string = text.getText().toString();                         len = string.length()+1;                         if(len%5==0){text.append("-");}               }                  return false;      }   });     } }); 

works fine upon adding, but deleting or editing causes problem.

like image 633
Rahul Avatar asked May 10 '11 09:05

Rahul


People also ask

How do I make text not editable on android?

android:editable="false" should work, but it is deprecated, you should be using android:inputType="none" instead. Alternatively, if you want to do it in the code you could do this : EditText mEdit = (EditText) findViewById(R.

How do you edit text on a mobile app?

On the 'Handset Display', select the text area on the 'Handset Display' that you wish to edit (as shown above highlighted in blue). Use the the 'DESIGN TOOLBOX' (on the left side of the screen), to edit the text as required. You can change the font type, text color, text size, justification etc.

How do I change the style of edit text in android Studio?

You can use the attribute style="@style/your_style" that is defined for any widget. The attribute parent="@android:style/Widget. EditText" is important because it will ensure that the style being defined extends the basic Android EditText style, thus only properties different from the default style need to be defined.


1 Answers

Now this works fine for soft/hard keyboard for all delete/edit ops. tx 4 ur help..

package com.and;  import android.app.Activity; import android.app.AlertDialog; import android.inputmethodservice.KeyboardView; import android.os.Bundle; import android.telephony.PhoneNumberFormattingTextWatcher; import android.text.Editable; import android.text.Selection; import android.text.Spannable; import android.text.TextWatcher; import android.text.format.Formatter; import android.text.method.NumberKeyListener; import android.view.KeyEvent; import android.view.View; import android.view.View.OnKeyListener; import android.widget.EditText; import android.widget.Toast;  public class ccformat extends Activity {      String a;     int keyDel;      /** Called when the activity is first created. */     @Override     public void onCreate(Bundle savedInstanceState) {         super.onCreate(savedInstanceState);         setContentView(R.layout.main);          final EditText text = (EditText) findViewById(com.and.R.id.editText1);          text.addTextChangedListener(new TextWatcher() {              @Override             public void onTextChanged(CharSequence s, int start, int before, int count) {                  boolean flag = true;                 String eachBlock[] = text.getText().toString().split("-");                 for (int i = 0; i < eachBlock.length; i++) {                     if (eachBlock[i].length() > 4) {                         flag = false;                     }                 }                 if (flag) {                      text.setOnKeyListener(new OnKeyListener() {                          @Override                         public boolean onKey(View v, int keyCode, KeyEvent event) {                              if (keyCode == KeyEvent.KEYCODE_DEL)                                 keyDel = 1;                             return false;                         }                     });                      if (keyDel == 0) {                          if (((text.getText().length() + 1) % 5) == 0) {                              if (text.getText().toString().split("-").length <= 3) {                                 text.setText(text.getText() + "-");                                 text.setSelection(text.getText().length());                             }                         }                         a = text.getText().toString();                     } else {                         a = text.getText().toString();                         keyDel = 0;                     }                  } else {                     text.setText(a);                 }              }              @Override             public void beforeTextChanged(CharSequence s, int start, int count,                     int after) {                 // TODO Auto-generated method stub              }              @Override             public void afterTextChanged(Editable s) {             }         });     } } 
like image 101
Rahul Avatar answered Sep 22 '22 19:09

Rahul