2012-01-23 54 views
0

使用我的onEditTextchanger。它可以在用戶輸入100000時正常工作,在EditText框中它顯示$ 100,000.00用戶類型。哪個是對的。 然而問題是如果我嘗試顯示數字鍵盤而不是Qwerty鍵盤。通過在XML中添加我添加android:inputType =「numberDecimal」我失去了$和格式,並且EditText顯示爲100000.00。我注意到,如果我將InputType更改爲Number或Decimal,則會發生這種情況。我附上了代碼。 任何想法?再次感謝您的幫助。OnEditTextChanger覆蓋Android:輸入法

XML

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
android:layout_width="fill_parent" 
android:layout_height="fill_parent" 
android:orientation="vertical" > 

<TextView 
    android:id="@+id/textView1" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:text="Number1" 
    android:textAppearance="?android:attr/textAppearanceMedium" /> 

<EditText 
    android:id="@+id/txta" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:hint="0" /> 

<TextView 
    android:id="@+id/textView2" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:text="Number2" /> 

<EditText 
    android:id="@+id/txtb" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:hint="0" /> 

<TextView 
    android:id="@+id/textView3" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:text="Number3" /> 

<TextView 
    android:id="@+id/txtc" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" /> 

<TextView 
    android:id="@+id/textView4" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:text="Your Answer is" 
    android:textAppearance="?android:attr/textAppearanceMedium" /> 

<TextView 
    android:id="@+id/txtd" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" /> 

<Button 
    android:id="@+id/buttonCalc" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:text="Calculate" /> 

的Java

public class CalcTestActivity extends Activity { 
    private EditText txta; 
    private EditText txtb; 
    private TextView txtc; 
    private TextView txtd; 


    private double a = 0; 
    private double b = 0; 
    private double c = 0; 
    private double d = 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(); 

     txta.addTextChangedListener(new CurrencyTextWatcher()); 
     txtb.addTextChangedListener(new CurrencyTextWatcher()); 

    } 



    private void initControls() { 

     txta = (EditText)findViewById(R.id.txta); 
     txtb = (EditText)findViewById(R.id.txtb); 
     txtc = (TextView)findViewById(R.id.txtc); 
     txtd = (TextView)findViewById(R.id.txtd); 

     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().replace("$", "").replace(",", "")); 
     b=Double.parseDouble(txtb.getText().toString().replace("$", "").replace(",", "")); 
     c=Math.round(a*.88);     
     txtc.setText(GlobalMoney.FormatValue(c)); 
     d=Math.round((a*.87)+(b*.61)*(c*.25)); 
     txtd.setText(GlobalMoney.FormatValue(d)); 
    } 


} 

TextWatcher

import java.text.NumberFormat; 
import android.text.Editable; 
import android.text.TextWatcher; 

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 

    } 

} 

回答

1

默認的inputType =」 numberDecimal」不接受除任何特殊字符。( d OT)。可能你需要做一些破解來完成這項工作。有一個有趣的SO討論,以特殊字符'''入侵。你可以嘗試擁有$符號。這裏是link

+0

謝謝你,我檢查了你給我的鏈接。這解決了問題的一部分,但我正在尋找如何實現數字鍵盤,而不是出現一個完整的Qwerty。想法? – Calvin

+0

我從來沒有這樣做過,但這個鏈接可能會有用。 http://stackoverflow.com/questions/1654901/does-android-have-a-numeric-keypad – kosa

+0

再次感謝Thinksteep。你讓我指出了正確的方向,並且你的修復對我的edittext問題有幫助。 – Calvin