5

我正在开发一个人们将输入账单的 android 应用程序。我有一个 EditText,人们输入金额。

我想要做的是,例如,如果一个人输入“2”,它应该自动转换为“0.02”。然后如果他/她想输入 22$,他应该按 2、2、0 和 0 按钮。就像“22.00”一样。我怎样才能使用 EditText 做到这一点。你能给我一些想法吗?

这是相关的代码片段:

            <EditText
            android:id="@+id/amount"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:layout_marginBottom="20dip"
            android:layout_marginTop="5dip"
            android:inputType="number"
            android:hint="@string/enter_amount"
            android:singleLine="true" />



            EditText payment = (EditText) findViewById(R.id.amount);
            if (payment.getText().toString().length() > 0){
                total_amount =Integer.parseInt(payment.getText().toString()) ;
            }
4

5 回答 5

3
EditText payment = (EditText) findViewById(R.id.amount);
payment.addTextChangedListener(new TextWatcher() {
        @Override
        public void onTextChanged(CharSequence s, int start, int before, int count) {

            // TODO Auto-generated method stub
        }

        @Override
        public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            // TODO Auto-generated method stub
        }

        @Override
        public void afterTextChanged(Editable s) {

             total_amount =Integer.parseInt(s) ;
               payment.setText(""+(total_amount/100));
        }
    });
于 2013-07-30T12:26:50.513 回答
2

For that you have to use textWatcher like this :

payment.addTextChangedListener(new TextWatcher() {
        @Override
        public void onTextChanged(CharSequence s, int start, int before, int count) {

            // TODO Auto-generated method stub
        }

        @Override
        public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            // TODO Auto-generated method stub
        }

        @Override
        public void afterTextChanged(Editable s) {

            // TODO Auto-generated method stub
        }
    });
于 2013-07-30T12:24:35.110 回答
1

利用 addTextChangedListener()

edittext.addTextChangedListener(new TextWatcher() {
    @Override
    public void onTextChanged(CharSequence s, int start, int before, int count) {

        // TODO Auto-generated method stub
    }

    @Override
    public void beforeTextChanged(CharSequence s, int start, int count, int after) {

        // TODO Auto-generated method stub
    }

    @Override
    public void afterTextChanged(Editable s) {

        // TODO Auto-generated method stub
    }
});
于 2013-07-30T12:20:07.487 回答
0

You are looking for the TextWatcher in order to alter their input as they enter it. Here is an example:

TextWatcher paymentWatcher = new TextWatcher() {
        public void afterTextChanged(Editable s) { 
            //Do your calculations here
        }
        public void beforeTextChanged(CharSequence s, int start, int count, int after) {

        }
        public void onTextChanged(CharSequence s, int start, int before, int count) {

        }
    };

    payment.addTextChangedListener(paymentWatcher);
于 2013-07-30T12:25:02.630 回答
0
EditText payment = (EditText) findViewById(R.id.amount);
        if (payment.getText().toString().length() > 0){
        double total_amount =Double.parseDouble(payment.getText().toString()) ;
        String amm = String.valueOf(total_amount/100);
        payment.setText(amm);
于 2013-07-30T12:21:33.373 回答