2012-01-23 9 views
0

私のonEditTextchangerの使用。ユーザーが100000を入力するとうまく動作し、EditTextボックスにはユーザータイプとして$ 100,000.00が表示されます。どちらが正しい。 しかし、Qwertyキーボードではなく数値キーボードを表示しようとすると問題になります。 XMLを追加することでandroid:inputType = "numberDecimal"を追加すると、$と、の書式が失われ、100000.00のようなEditTextの表示が失われます。 InputTypeをNumberまたはDecimalに変更すると、これが発生することに気付きました。私はコードを添付しました。 アイデアもう一度あなたの助けをありがとうございます。デフォルトではAndroidを上書きするOnEditTextChanger:入力方法

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

私があなたに与えたリンクを確認していただきありがとうと思います。これで問題の一部が修正されましたが、私は数字キーボードを実装する方法を探しています。アイデア? – Calvin

+0

私はこれをやったことはありませんが、このリンクが役に立つかもしれません。 http://stackoverflow.com/questions/1654901/does-android-have-a-numeric-keypad – kosa

+0

もう一度ありがとうThinksteep。あなたは正しい方向に私を指摘してくれました。あなたの修正は私のedittextの問題に役立ちました。 – Calvin

関連する問題