2014-11-23 69 views
0

我非常習慣在android中編程。對不起,如果我的問題很明顯。editText使用TextWatcher驗證和修正

我有一個editText與inputType =「numberDecimal」。我看着TextWatcher的變化,從它的值(Double類型)計算出來,並在TextView上顯示它。計算是可以的,除非editText的值是「。」。如果editText的值爲「。」,則程序將丟失一個錯誤(強制關閉)。

我的問題是:如何將editText值設置爲「0」。使用addTextChangedListener如果editText獲取值「。」?你有沒有得到這個情景最佳做法?在此先感謝您的幫助。

我的代碼是:

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

    } 

} 

回答

0

嘗試用這種

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

寫入到afterTextChanged()方法。

+0

謝謝你的建議。主要問題是,我試圖使用if(baseText ==「。」)而不是if(baseText.equals(「。」))。等於的使用解決了這個問題。 – Seba 2014-11-23 12:29:03

+0

不客氣 – 2014-11-24 03:59:23

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

    } 

}