2013-07-29 121 views
1

所以我有一個視圖有兩個使用textwatchers作爲文本更改偵聽器的編輯文本。 當第一個編輯文本的值被改變時,一個新值被計算並被設置爲第二個編輯文本,反之亦然。我在設置文本之前刪除了textwatchers。 我面臨的問題是當我在第一個或第二個編輯文本框中輸入某些內容時,它只需要一個或兩個數字並停止輸入任何內容。光標從結尾到開始。這裏是類使用textwatchers的多個edittext

public class CurrencyView extends RelativeLayout implements ICustomView{ 

    private class CustomOnClick implements OnClickListener{ 
     @Override 
     public void onClick(View v) { 
      final TextView view = (TextView)v; 
      final String options[] = ConversionTypes.getCurrencyTypes(); 

      AlertDialog.Builder builder = new AlertDialog.Builder(rContext); 

      builder.setItems(options, new DialogInterface.OnClickListener() { 
       @Override 
       public void onClick(DialogInterface dialog, int which) { 
        view.setText(options[which]); 
        convert(view.getId());  
       } 


      }); 
      builder.show(); 

     } 
    } 

    private class CustomTextWatcher implements TextWatcher{ 

     int ID; 
     @Override 
     public void afterTextChanged(Editable s) { 
      convert(ID); 

     } 

     @Override 
     public void beforeTextChanged(CharSequence s, int start, int count, 
       int after) { 
      if(s.toString().equals(type1Value.getText().toString())){ 
       ID=11; 
      } 
      else{ 
       ID=22; 
      } 

     } 

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

    } 

    private TextView currencyType1; 
    private LayoutParams currencyType1LP; 
    private TextView currencyType2; 
    private LayoutParams currencyType2LP; 
    private EditText type1Value; 
    private LayoutParams type1ValueLP; 
    private EditText type2Value; 
    private LayoutParams type2ValueLP; 
    private CustomTextWatcher fCustomTextWatcher = new CustomTextWatcher(); 
    private ArrayList<Double> rates = new ArrayList<Double>(); 
    private Context rContext; 
    private int screenWidth; 

    private ListView rPopularConversions; 
    private long lastRefreshed; 
    private TextView lastRefeshedView; 

    private void addListeners() { 
     currencyType1.setOnClickListener(new CustomOnClick()); 
     currencyType2.setOnClickListener(new CustomOnClick()); 
     addEditTextListeners(); 

    } 

    private void addEditTextListeners() { 
     type1Value.addTextChangedListener(fCustomTextWatcher); 
     type2Value.addTextChangedListener(fCustomTextWatcher); 
    } 

    private void convert(int i) { 
     removeAllListeners(); 

      if(i == 11 || i == 1){ 
       if(!Formatting.isEmptyOrNull(type2Value)){ 
        double from = rates.get(getLocation(currencyType2)); 
        double to = rates.get(getLocation(currencyType1)); 
        Currency c = new Currency(from,to, Double.valueOf(type2Value.getText().toString())); 
        type1Value.setText(String.valueOf(Formatting.roundOff(c.getResult()))); 
       } 

      } 

      if(i == 22 || i==2){ 
       if(!Formatting.isEmptyOrNull(type1Value)){ 
        double from = rates.get(getLocation(currencyType1)); 
        double to = rates.get(getLocation(currencyType2)); 
        Currency c = new Currency(from, to, Double.valueOf(type1Value.getText().toString())); 
        type2Value.setText(String.valueOf(Formatting.roundOff(c.getResult()))); 
       } 
      } 

      addEditTextListeners(); 
    } 




    private int getLocation(TextView tv) { 
     for (int i = 0; i < ConversionTypes.getCurrencyTypes().length; i++) { 
      if(tv.getText().toString().equals(ConversionTypes.getCurrencyTypes()[i])){ 
       return i; 
      } 
     } 
     return 0; 
    } 


    private void initialize() { 



     currencyType1 = CustomObject.getCustomTextView(rContext, 1, Color.parseColor("#63879F"), Color.WHITE, 16, ConversionTypes.getCurrencyTypes()[121]); 
     currencyType2 = CustomObject.getCustomTextView(rContext, 2, Color.parseColor("#63879F"), Color.WHITE, 16, ConversionTypes.getCurrencyTypes()[121]); 
     type1Value = CustomObject.getCustomInputBox(rContext, 35, "1", "Enter value", new int[]{}, 11); 
     type1Value.setInputType(InputType.TYPE_CLASS_NUMBER | InputType.TYPE_NUMBER_FLAG_DECIMAL); 
     type2Value = CustomObject.getCustomInputBox(rContext, 35, "1", "Enter value", new int[]{}, 22); 
     type2Value.setInputType(InputType.TYPE_CLASS_NUMBER | InputType.TYPE_NUMBER_FLAG_DECIMAL); 

     int orientation = ((WindowManager) rContext.getSystemService(Context.WINDOW_SERVICE)).getDefaultDisplay().getRotation(); 
     if (orientation == Surface.ROTATION_0 || orientation == Surface.ROTATION_180) { 
      loadPotraitView(); 
     } 
     else { 
      loadLandscapeView(); 
     } 





    } 

    @Override 
    public void loadLandscapeView() { 
     lastRefreshed = rContext.getSharedPreferences("com.rokzin.converto_preferences", 0).getLong("LastRefreshed", 0); 
     screenWidth = rContext.getResources().getDisplayMetrics().widthPixels; 

     removeAllViews(); 

     rPopularConversions = new ListView(rContext); 
     RelativeLayout.LayoutParams listLP = CustomObject.getCustomParams(screenWidth, LayoutParams.WRAP_CONTENT, new int[]{}); 
     listLP.addRule(RelativeLayout.BELOW, type2Value.getId()); 

     currencyType1LP = CustomObject.getCustomParams(screenWidth * 9/20, 120, new int[]{RelativeLayout.ALIGN_PARENT_LEFT,RelativeLayout.ALIGN_PARENT_TOP}); 
     currencyType2LP = CustomObject.getCustomParams(screenWidth * 9/20, 120, new int[]{RelativeLayout.ALIGN_PARENT_RIGHT,RelativeLayout.ALIGN_PARENT_TOP}); 

     type1ValueLP = CustomObject.getCustomParams(screenWidth * 9/20, 120, new int[]{RelativeLayout.ALIGN_PARENT_LEFT}); 
     type2ValueLP = CustomObject.getCustomParams(screenWidth * 9/20, 120, new int[]{RelativeLayout.ALIGN_PARENT_RIGHT}); 

     type1ValueLP.addRule(RelativeLayout.BELOW, currencyType1.getId()); 
     type2ValueLP.addRule(RelativeLayout.BELOW, currencyType2.getId()); 

     this.addView(currencyType1, currencyType1LP); 

      TextView equalTo = CustomObject.getCustomTextView(rContext, 111, Color.TRANSPARENT, Color.parseColor("#63879F"), 24, "="); 
      RelativeLayout.LayoutParams params = CustomObject.getCustomParams(screenWidth*2/20, 240, new int[]{RelativeLayout.ALIGN_PARENT_TOP, RelativeLayout.CENTER_HORIZONTAL}); 
      equalTo.setGravity(Gravity.CENTER_HORIZONTAL | Gravity.CENTER_VERTICAL); 
      this.addView(equalTo, params); 

     this.addView(currencyType2, currencyType2LP); 
     this.addView(type1Value,type1ValueLP); 
     this.addView(type2Value,type2ValueLP); 

     this.addView(rPopularConversions,listLP); 
     removeAllListeners(); 
     addListeners(); 

    } 


    @Override 
    public void loadPotraitView() { 
     lastRefreshed = rContext.getSharedPreferences("com.rokzin.converto_preferences", 0).getLong("LastRefreshed", 0); 
     screenWidth = rContext.getResources().getDisplayMetrics().widthPixels; 

     removeAllViews(); 

     rPopularConversions = new ListView(rContext); 
     RelativeLayout.LayoutParams listLP = CustomObject.getCustomParams(screenWidth, LayoutParams.WRAP_CONTENT, new int[]{}); 
     listLP.addRule(RelativeLayout.BELOW, currencyType2.getId()); 

     currencyType1LP = CustomObject.getCustomParams(screenWidth * 1/3, 120, new int[]{RelativeLayout.ALIGN_PARENT_LEFT,RelativeLayout.ALIGN_PARENT_TOP}); 
     currencyType2LP = CustomObject.getCustomParams(screenWidth * 1/3, 120, new int[]{}); 

     type1ValueLP = CustomObject.getCustomParams(screenWidth * 2/3, 120, new int[]{RelativeLayout.ALIGN_PARENT_TOP}); 
     type2ValueLP = CustomObject.getCustomParams(screenWidth * 2/3, 120, new int[]{}); 

     type1ValueLP.addRule(RelativeLayout.RIGHT_OF, currencyType1.getId()); 
     type2ValueLP.addRule(RelativeLayout.BELOW, currencyType1.getId()); 
     currencyType2LP.addRule(RelativeLayout.RIGHT_OF, type2Value.getId()); 
     currencyType2LP.addRule(RelativeLayout.BELOW, type1Value.getId()); 


     this.addView(currencyType1, currencyType1LP); 
     this.addView(type1Value,type1ValueLP); 
     this.addView(type2Value,type2ValueLP); 
     this.addView(currencyType2, currencyType2LP); 
     this.addView(rPopularConversions,listLP); 
     removeAllListeners(); 
     addListeners(); 



    } 


    public void removeAllListeners(){ 
     type1Value.removeTextChangedListener(fCustomTextWatcher); 
     type2Value.removeTextChangedListener(fCustomTextWatcher); 

    } 
} 

回答

0

我認爲這個問題是在這裏:

if(s.toString().equals(type1Value.getText().toString())){ 
    ID=11; 
} 
else{ 
    ID=22; 
} 

採取記住,這兩個EditTexts正在經歷這種方法,所以正在編輯type1Value時,s.toString().equals(type1Value.getText().toString())將是真實的。

我建議你爲每個EditText使用TextWatcher,這樣你就不必區分每個EditText。

+0

使用兩個不同的textwatchers幫助。感謝:D –