Note that there are some explanatory texts on larger screens.

plurals
  1. POtextChangedMethod for Multiple EditText boxes
    primarykey
    data
    text
    <p>Hi All I have followed the following example <a href="http://www.google.com/codesearch#search/&amp;q=NumberFormattingTextWatcher&amp;exact_package=android&amp;type=cs" rel="nofollow">http://www.google.com/codesearch#search/&amp;q=NumberFormattingTextWatcher&amp;exact_package=android&amp;type=cs</a> </p> <p>I have CurrencyTextWatcher as a seperate class. I need this as I will be applying to several pages. I can't figure out why, but if I use <strong>setContentView(text)</strong> it will work as only 1 big text box, then I can't see the rest of my xml .<br> If I use <strong>setContentView(R.layout.main);</strong> my xml works properly except for the TextWatcher wont fire for my <strong>txta EditText box</strong> </p> <p>Java</p> <pre><code>public class CalcTestActivity extends Activity { private EditText txta; private TextView txtb; private TextView txtc; private EditText text; private double a = 0; private double b = 0; private double c = 0; private Button buttonCalc; /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); initControls(); text = new EditText(this); text.addTextChangedListener(new CurrencyTextWatcher()); //setContentView(text); } private String FormatValue(double value) { NumberFormat nf = NumberFormat.getInstance(); return "$ "+ nf.format(value); } private void initControls() { txta = (EditText)findViewById(R.id.txta); txtb = (TextView)findViewById(R.id.txtb); txtc = (TextView)findViewById(R.id.txtc); buttonCalc = (Button)findViewById(R.id.buttonCalc); buttonCalc.setOnClickListener(new Button.OnClickListener() { public void onClick(View v) {calculate(); } private void calculate() { a=Double.parseDouble(txta.getText().toString()); b=Math.round(a*.88); txtb.setText(FormatValue(b)); c=Math.round((a*.87)-(b*.28)); txtc.setText(FormatValue(c)); } }); } } </code></pre> <p>CurrencyTextWatcher Class</p> <pre><code>public class CurrencyTextWatcher implements TextWatcher { boolean mEditing; public CurrencyTextWatcher() { mEditing = false; } public void afterTextChanged(Editable s) { // TODO Auto-generated method stub if(!mEditing) { mEditing = true; String digits = s.toString().replaceAll("\\D", ""); NumberFormat nf = NumberFormat.getCurrencyInstance(); try{ String formatted = nf.format(Double.parseDouble(digits)/100); s.replace(0, s.length(), formatted); } catch (NumberFormatException nfe) { s.clear(); } mEditing = false; } } public void beforeTextChanged(CharSequence s, int start, int count, int after) { // TODO Auto-generated method stub } public void onTextChanged(CharSequence s, int start, int before, int count) { // TODO Auto-generated method stub } </code></pre> <p>}</p> <p>XML</p> <pre><code>&lt;TextView android:id="@+id/textView1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Number1" android:textAppearance="?android:attr/textAppearanceMedium" /&gt; &lt;EditText android:id="@+id/txta" android:layout_width="match_parent" android:layout_height="wrap_content" android:numeric="integer"/&gt; &lt;TextView android:id="@+id/textView2" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Number2" android:textAppearance="?android:attr/textAppearanceMedium" /&gt; &lt;TextView android:id="@+id/txtb" android:layout_width="match_parent" android:layout_height="wrap_content" /&gt; &lt;TextView android:id="@+id/textView3" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Your Answer is" android:textAppearance="?android:attr/textAppearanceMedium" /&gt; &lt;TextView android:id="@+id/txtc" android:layout_width="wrap_content" android:layout_height="wrap_content" android:hint="0" /&gt; &lt;Button android:id="@+id/buttonCalc" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Calculate" /&gt; &lt;/LinearLayout&gt; </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload