edittext 光标保持在起始位置

edittext cursor remains at starting position

我有 3 个 Number(Decimal) 类型的 editText 字段。其中两个具有 onTextChanged 侦听器,应该在输入任何整数时执行数学函数。我的问题是带有监听器的 edittext 字段的光标停留在起始位置,即如果我尝试输入 25,它最终会变成 52。

代码:

package com.example.Prototype;


import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Spinner;

public class productsFragmentTab extends Fragment {

Button newProduct, clear;
Spinner productList;
EditText quantity, unit, total;
String invoice_id;
ShowAlert alert = new ShowAlert();
int unitcost, totalcost;

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {

    View rootView = inflater.inflate(R.layout.product_layout, container, false);

    newProduct = (Button)rootView.findViewById(R.id.button4);
    clear = (Button)rootView.findViewById(R.id.button5);

    productList = (Spinner)rootView.findViewById(R.id.spinner2);
    quantity = (EditText)rootView.findViewById(R.id.editText5);
    unit = (EditText)rootView.findViewById(R.id.editText7);
    total = (EditText)rootView.findViewById(R.id.editText8);

    invoice_id = GlobalApp.data().id;



    newProduct.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            //Save current records in array
            //Clear all textboxes



        }
    });

    clear.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {

            //clear all textboxes


        }
    });

    unit.addTextChangedListener(new TextWatcher() {
        boolean isChangingByCode = false;

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


        }

        @Override
        public void onTextChanged(CharSequence s, int start, int before, int count) {
            if (isChangingByCode) {
                return;
            }
            String qty = quantity.getText().toString();
            if (qty.matches("")) {


            } else {

                    totalcost = Integer.parseInt(qty) * Integer.parseInt(unit.getText().toString());
                    isChangingByCode = true;
                    total.setText(Integer.toString(totalcost));
                    isChangingByCode = false;

            }

        }

        @Override
        public void afterTextChanged(Editable s) {

        }
    });

    total.addTextChangedListener(new TextWatcher() {
        boolean isChangingByCode = false;

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

        }

        @Override
        public void onTextChanged(CharSequence s, int start, int before, int count) {
            if (isChangingByCode) {
                return;
            }
            String qty = quantity.getText().toString();
            if (qty.matches("")) {


            } else {

                    unitcost = Integer.parseInt(total.getText().toString()) / Integer.parseInt(qty);
                    isChangingByCode = true;
                    unit.setText(Integer.toString(unitcost));
                    isChangingByCode = false;

            }

        }

        @Override
        public void afterTextChanged(Editable s) {

        }
    });

    return rootView;
}

}

最后使用setSelection()方法设置光标位置。 添加以下代码:

total.setText(Integer.toString(totalcost));
total.setSelection(total.getText().length());

代替 onTextChanged() 回调中的 total.setText(Integer.toString(totalcost));

 @Override 
    public void onTextChanged(CharSequence s, int start, int before, int count) {
        if (isChangingByCode) {
            return; 
        } 
        String qty = quantity.getText().toString();
        if (qty.matches("")) {


        } else { 

                totalcost = Integer.parseInt(qty) * Integer.parseInt(unit.getText().toString());
                isChangingByCode = true;
                total.setText(Integer.toString(totalcost));
                total.setSelection(total.getText().length());
                isChangingByCode = false;

        } 

    }