2012-01-23 5 views
0

내 onEditTextchanger 사용. 사용자가 100000을 입력하면 잘 작동하고, EditText 상자에는 사용자 유형으로 $ 100,000.00가 표시됩니다. 어느 것이 옳은가. 그러나 Qwerty 키보드가 아닌 숫자 키보드를 표시하려고하면 문제가 발생합니다. XML을 추가하여 android : inputType = "numberDecimal"을 추가하면 $ 및 and의 서식이 손실되고 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" /> 

자바 기본적으로

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는"제외한 모든 특수 문자를 허용하지 않습니다. (디 ot). 이 작업을하기 위해 해킹을해야 할 수도 있습니다. 특수 문자 ','를 해킹하는 재미있는 SO 토론이 있습니다. 당신은 $ 기호를 가지고 그것을 시도 할 수 있습니다. 여기에 link입니다.

+0

감사합니다. 내게 준 링크를 확인해보세요. 문제의 일부가 수정되었지만, 전체 쿼티가 오히려 숫자 키보드를 구현하는 방법을 찾고 있습니다. 아이디어? – Calvin

+0

본 적이 없지만이 링크가 유용 할 수 있습니다. http://stackoverflow.com/questions/1654901/does-android-have-a-numeric-keypad – kosa

+0

다시 감사드립니다. Thinksteep. 당신은 올바른 방향으로 나를 지적 해 주었고, 당신의 수정이 제 편집 문 문제에 도움이되었습니다. – Calvin

관련 문제