我有3个类型为Number(Decimal)的editText字段。当输入任何整数时,其中两个onTextChanged监听器应该执行数学函数。我的问题是带有监听器的edittext字段将游标卡在起始位置,即如果我尝试键入25,则最终为52。
CODE:
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;
}
}
答案 0 :(得分:1)
使用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;
}
}