使用textwatchers的多个edittexts

时间:2013-07-29 04:56:29

标签: android android-edittext listener textwatcher

所以我有一个视图,它有两个编辑文本,使用textwatchers作为文本更改侦听器。 当第一个编辑文本的值更改时,计算新值并将其设置为第二个编辑文本,反之亦然。我在设置文本之前删除了textwatchers。 我面临的问题是当我在第一个或第二个编辑文本框中输入内容时,它只需要一个或两个数字并停止接受任何输入。光标从结尾到开始。这里是类

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

    }
}

1 个答案:

答案 0 :(得分:0)

我认为问题在于:

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

请记住,两个EditTexts都要经过该方法,因此在编辑type1Value时,s.toString().equals(type1Value.getText().toString())将为true。

我建议您为每个EditText使用TextWatcher,这样您就不必区分每个。