【问题标题】:OnEditTextChanger overwriting Android:Input methodOnEditTextChanger 覆盖 Android:输入法
【发布时间】:2012-01-24 03:08:46
【问题描述】:

使用我的 onEditTextchanger。当用户输入 100000 时,它可以正常工作,在 EditText 框中,当用户键入时,它会显示 $100,000.00。哪个是对的。 但是,问题是如果我尝试显示数字键盘而不是 Qwerty 键盘。通过添加 XML,我添加了 android: inputType=”numberDecimal” 我失去了 $ 和 the 的格式,并且 EditText 显示为 100000.00。我注意到如果我将 InputType 更改为 Number 或 Decimal 也会发生这种情况。我附上了代码。 有任何想法吗?再次提前感谢您的帮助。

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));
    }


}

文本观察器

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

    }

}

【问题讨论】:

    标签: java android


    【解决方案1】:

    默认情况下,inputType=”numberDecimal” 不接受除 .(dot) 之外的任何特殊字符。可能是你需要做一些技巧来完成这项工作。有一个有趣的 SO 讨论来破解特殊字符','。您可以尝试使用 $ 符号。这里是link

    【讨论】:

    • 谢谢我检查了你给我的链接。这解决了部分问题,但我正在研究如何实现数字键盘,而不是出现一个完整的 qwerty。想法?
    • 我从来没有这样做过,但是这个链接可能有用。 stackoverflow.com/questions/1654901/…
    • 再次感谢 Thinksteep。你让我指出了正确的方向,你的修复确实帮助解决了我的编辑文本问题。
    • 很高兴它对您有所帮助。享受编程。
    猜你喜欢
    • 1970-01-01
    • 2018-12-14
    • 2012-03-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多