1

所以我有一个视图,它有两个使用 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);

    }
}
4

1 回答 1

0

我认为问题出在这里:

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

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

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

于 2013-07-29T07:30:30.803 回答