Don't like ads? PRO users don't see any ads ;-)
Guest

Untitled

By: a guest on Jun 22nd, 2012  |  syntax: None  |  size: 8.53 KB  |  hits: 22  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. android EditText ,keyboard textWatcher problem
  2. public class AppHome extends AppBaseActivity {
  3.     private EditText ed = null;
  4.     private NumberFormat amountFormatter = null;
  5.     private boolean  isUserInput = true;
  6.  
  7.     @Override
  8.     public void onCreate(Bundle savedInstanceState) {
  9.         super.onCreate(savedInstanceState);
  10.         setContentView(R.layout.app_home_screen);
  11.  
  12.         ed = (EditText)findViewById(R.id.main_amount_textfield);
  13.         amountFormatter = new DecimalFormat("##,##,###");
  14.  
  15.  
  16.         ed.setOnKeyListener(new View.OnKeyListener() {
  17.             @Override
  18.             public boolean onKey(View v, int keyCode, KeyEvent event) {
  19.                 if(event.getAction() == KeyEvent.ACTION_DOWN)
  20.                     return true;
  21.                 String strippedAmount = ed.getText().toString().replace(",", "");
  22.                 if(keyCode == KeyEvent.KEYCODE_DEL){
  23.                     //delete pressed, strip number of comas and then delete least significant digit.
  24.                     strippedAmount = strippedAmount.substring(0, strippedAmount.length() - 1);
  25.                     int amountNumeral = 0;
  26.                     try{
  27.                         amountNumeral = Integer.parseInt(strippedAmount);
  28.                     } catch(NumberFormatException e){
  29.                     }
  30.                     myObject.amount = amountNumeral;
  31.                     isUserInput = false;
  32.                     setFormattedAmount(amountNumeral,ed.getId());
  33.                 }else if(keyCode == KeyEvent.KEYCODE_ENTER){
  34.                     //enter pressed, save edits and resign keyboard
  35.                     int amountNumeral = 0;
  36.                     try{
  37.                         amountNumeral = Integer.parseInt(strippedAmount);
  38.                     } catch(NumberFormatException e){
  39.                     }
  40.                     myObject.amount = amountNumeral;
  41.                     isUserInput = false;
  42.                     setFormattedAmount(myObject.amount,ed.getId());
  43.                     //save edits
  44.                     save();
  45.                     //resign keyboard..
  46.                     InputMethodManager in = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
  47.                     in.hideSoftInputFromWindow(AppHome.this.getCurrentFocus().getWindowToken(),InputMethodManager.HIDE_NOT_ALWAYS);
  48.                 }
  49.                 return true;
  50.             }
  51.         });
  52.  
  53.         TextWatcher inputTextWatcher = new TextWatcher() {
  54.             public void afterTextChanged(Editable s) {
  55.                 if(isUserInput == false){
  56.                     //textWatcher is recursive. When editText value is changed from code textWatcher callback gets called. So this variable acts as a flag which tells whether change is user generated or not..Possibly buggy code..:(
  57.                     isUserInput = true;
  58.                     return;
  59.                 }
  60.                 String strippedAmount = ed.getText().toString().replace(",", "");
  61.                 int amountNumeral = 0;
  62.                 try{
  63.                     amountNumeral = Integer.parseInt(strippedAmount);
  64.                 } catch(NumberFormatException e){
  65.                 }
  66.                 isUserInput = false;
  67.                 setFormattedAmount(amountNumeral,ed.getId());
  68.             }
  69.  
  70.             public void beforeTextChanged(CharSequence s, int start, int count, int after){
  71.             }
  72.             public void onTextChanged(CharSequence s, int start, int before, int count) {
  73.             }
  74.         };
  75.  
  76.         ed.addTextChangedListener(inputTextWatcher);
  77.     }//end of onCreate...
  78.  
  79.     public void setFormattedAmount(Integer amount, Integer inputBoxId){
  80.         double amountValue = 0;
  81.         String textString =null;
  82.         TextView amountInputBox = (TextView) findViewById(inputBoxId);
  83.  
  84.         amountValue = Double.parseDouble(Integer.toString(amount));
  85.         textString = amountFormatter.format(amountValue).toString();
  86.         amountInputBox.setText(textString);
  87.     }
  88. }
  89.        
  90. InputFilter filter = new InputFilter() {
  91.     public CharSequence filter(CharSequence source, int start, int end, Spanned dest, int dstart, int dend) {
  92.             String strippedAmount = dest.toString() + source;
  93.             strippedAmount = strippedAmount.replace(",", "");
  94.  
  95.         int amountNumeral = 0;
  96.         try{
  97.             amountNumeral = Integer.parseInt(strippedAmount);
  98.         } catch(NumberFormatException e){
  99.         }          
  100.             return amountFormatter.format(amountNumeral).toString();
  101.     }
  102. };
  103.  
  104. ed.setFilters(new InputFilter[]{filter});
  105.        
  106. myObject.amount = 1234;
  107. ed.setText(amountFormatter.format(myObject.amount).toString());
  108.        
  109. TextWatcher inputTextWatcher = new TextWatcher() {
  110.         public void afterTextChanged(Editable s) {
  111.             if(isUserInput == false){
  112.                 //textWatcher is recursive. When editText value is changed from code textWatcher callback gets called. So this variable acts as a flag which tells whether change is user generated or not..Possibly buggy code..:(
  113.                 isUserInput = true;
  114.                 ed.setSelection(ed.getText().length());
  115.                 return;
  116.             }
  117.             String strippedAmount = ed.getText().toString().replace(",", "");
  118.             int amountNumeral = 0;
  119.             try{
  120.                 amountNumeral = Integer.parseInt(strippedAmount);
  121.             } catch(NumberFormatException e){
  122.             }
  123.             isUserInput = false;
  124.             setFormattedAmount(amountNumeral,ed.getId());
  125.         }
  126.  
  127.         public void beforeTextChanged(CharSequence s, int start, int count, int after){
  128.         }
  129.         public void onTextChanged(CharSequence s, int start, int before, int count) {
  130.         }
  131.     };
  132. ed.addTextChangedListener(inputTextWatcher);
  133.  
  134.  
  135. ed.setOnClickListener(new View.OnClickListener() {
  136.         @Override
  137.         public void onClick(View v) {
  138.             int length          =   ed.getText().length();
  139.             ed.setCursorVisible(true);
  140.             ed.setSelection(length);
  141.         }
  142.     });
  143.  
  144. ed.setOnKeyListener(new View.OnKeyListener() {
  145.     @Override
  146.     public boolean onKey(View v, int keyCode, KeyEvent event) {
  147.             if(event.getAction() == KeyEvent.ACTION_UP)
  148.                 return true;
  149.             String strippedAmount = ed.getText().toString().replace(",", "");
  150.             if(keyCode == KeyEvent.KEYCODE_DEL){
  151.                 //delete pressed, strip number of comas and then delete least significant digit.
  152.                 strippedAmount = strippedAmount.substring(0, strippedAmount.length() - 1);
  153.                 int amountNumeral = 0;
  154.                 try{
  155.                     amountNumeral = Integer.parseInt(strippedAmount);
  156.                 } catch(NumberFormatException e){
  157.                 }
  158.                 isUserInput = false;
  159.                 setFormattedAmount(amountNumeral,ed.getId());
  160.                 return true;
  161.             }else if(keyCode == KeyEvent.KEYCODE_ENTER){
  162.                 //enter pressed, save edits and resign keyboard
  163.                 int amountNumeral = 0;
  164.                 try{
  165.                     amountNumeral = Integer.parseInt(strippedAmount);
  166.                 } catch(NumberFormatException e){
  167.                 }
  168.                 isUserInput = false;
  169.                 setFormattedAmount(amountNumeral,ed.getId());
  170.                 //save edits
  171.                 //resign keyboard..
  172.                 InputMethodManager in = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
  173.                 in.hideSoftInputFromWindow(AppHome.this.getCurrentFocus().getWindowToken(),InputMethodManager.HIDE_NOT_ALWAYS);
  174.                 return true;
  175.             }
  176.             return false;
  177.     }
  178. });
  179.        
  180. /**
  181.      * This filter will capitalize all the lower case letters that are added
  182.      * through edits.
  183.      */
  184.     public static class AllCaps implements InputFilter {
  185.         public CharSequence filter(CharSequence source, int start, int end,
  186.                                    Spanned dest, int dstart, int dend) {
  187.             for (int i = start; i < end; i++) {
  188.                 if (Character.isLowerCase(source.charAt(i))) {
  189.                     char[] v = new char[end - start];
  190.                     TextUtils.getChars(source, start, end, v, 0);
  191.                     String s = new String(v).toUpperCase();
  192.  
  193.                     if (source instanceof Spanned) {
  194.                         SpannableString sp = new SpannableString(s);
  195.                         TextUtils.copySpansFrom((Spanned) source,
  196.                                                 start, end, null, sp, 0);
  197.                         return sp;
  198.                     } else {
  199.                         return s;
  200.                     }
  201.                 }
  202.             }
  203.  
  204.             return null; // keep original
  205.         }
  206.     }