使用TextWatcher进行editText验证和更正

时间:2014-11-23 01:49:11

标签: android validation android-edittext textwatcher

我是初学者在android中编程。对不起,如果我的问题很明显。

我有一个带有inputType =“numberDecimal”的editText。我用TextWatcher观察它的变化,从它的值(Double类型)计算,并在TextView上显示它。计算没问题,除非editText的值为“。”。如果editText的值为“。”,程序将丢弃错误(强制关闭)。

我的问题是:如何将editText值设置为“0”。如果editText获取值“。”,则使用addTextChangedListener?你有这种情况的最佳做法吗?在此先感谢您的帮助。

我的代码是:

public class ConversionsFragment extends Fragment{

    private EditText etDistBase;
    private TextView tvMeterResult;
    private TextView tvKmResult;

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

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

       etDistBase = (EditText) rootView.findViewById(R.id.textEdit_conv_distance);
       tvMeterResult = (TextView) rootView.findViewById(R.id.conv_dist_result_meter);
       tvKmResult = (TextView) rootView.findViewById(R.id.conv_dist_result_km);

       etDistBase.addTextChangedListener(new TextWatcher(){
            public void afterTextChanged(Editable s) {

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

            }
            public void onTextChanged(CharSequence s, int start, int before, int count){
                   calculateConvDistances(); 
            }
        });
       return rootView;

   }


   private void calculateConvDistances() {

       String baseText = etDistBase.getText().toString();
       Double baseValue = 0.0,
              meterResult = 0.0,
              kmResult = 0.0;

//     if (baseText == ".") {
//         etDistBase.setText("0.");
//         baseText = "0";
//     }

       if (baseText.length() == 0)
               baseText = "0";

       if (baseText != null) {
            baseValue = Double.parseDouble(baseText);
            meterResult = baseValue * 1000;
            kmResult = baseValue;
       } else {
           meterResult = 0.0;
           kmResult = 0.0;
       }

      tvMeterResult.setText(Double.toString(meterResult));
      tvKmResult.setText(Double.toString(kmResult));

    } 

}

2 个答案:

答案 0 :(得分:0)

试试这个

if( editText.getText().toString().equals(".") ){
    editText.setText("0.");
}

写入afterTextChanged()方法。

答案 1 :(得分:0)

工作的解决方案是改变"。"发短信到" 0。"在afterTextChanged()方法中,如下所示:

public class ConversionsFragment extends Fragment{

    private EditText etDistBase;
    private TextView tvMeterResult;
    private TextView tvKmResult;

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

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

       etDistBase = (EditText) rootView.findViewById(R.id.textEdit_conv_distance);
       tvMeterResult = (TextView) rootView.findViewById(R.id.conv_dist_result_meter);
       tvKmResult = (TextView) rootView.findViewById(R.id.conv_dist_result_km);

       etDistBase.addTextChangedListener(new TextWatcher(){
            public void afterTextChanged(Editable s) {
                if (etDistBase.getText().toString().equals(".")) 
                    etDistBase.setText("0.");
                    etDistBase.setSelection(etDistBase.getText().length());
            } 
            public void beforeTextChanged(CharSequence s, int start, int count, int after){

            }
            public void onTextChanged(CharSequence s, int start, int before, int count){
                    calculateConvDistances(); 
            }
        });
       return rootView;

   }


   private void calculateConvDistances() {

       String baseText = etDistBase.getText().toString();
       Double baseValue = 0.0,
              meterResult = 0.0,
              kmResult = 0.0;

       if (baseText.equals(".")) {
           baseText = "0";
       }

       if (baseText.length() == 0)
               baseText = "0";

       if (baseText != null) {
            baseValue = Double.parseDouble(baseText);
            meterResult = baseValue * 1000;
            kmResult = baseValue;
       } else {
           meterResult = 0.0;
           kmResult = 0.0;
       }

      tvMeterResult.setText(Double.toString(meterResult));
      tvKmResult.setText(Double.toString(kmResult));

    } 

}