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.
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.
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.
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.
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) { } }); } }
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With