0

我有个问题。我有两个 Edittexts 我有一个日期。我addTextChangeListener用来检查文本的变化。这是代码:

tvDateFrom.addTextChangedListener(new TextWatcher() {

    @Override
    public void onTextChanged(CharSequence s, int start, int before, int count) {
        SimpleDateFormat sdf = new SimpleDateFormat("dd-MM-yyyy");
        Date dateFromPicker = null;
        if(listOfSBPData!=null)
            try {
                dateFromPicker = sdf.parse(tvDateFrom.getText().toString());
                String dt = tvDateFrom.getText().toString();
                SimpleDateFormat dateFormat = new SimpleDateFormat( "dd-MM-yyyy" );
                Calendar cal = Calendar.getInstance();
                cal.setTime( dateFormat.parse( tvDateFrom.getText().toString() ) );
                cal.add( Calendar.DATE, 7 );
                  int  year = cal.get(Calendar.YEAR);
                  int  month = cal.get(Calendar.MONTH);
                  int day = cal.get(Calendar.DAY_OF_MONTH);
                    tvDateTo.setText(""+day+"-"+(month+1)+"-"+year);
                    addSBPSeries();
            } catch (ParseException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
    }

    @Override
    public void beforeTextChanged(CharSequence s, int start, int count,
            int after) {
        // TODO Auto-generated method stub

    }

    @Override
    public void afterTextChanged(Editable s) {


    }
});

tvDateTo.addTextChangedListener(new TextWatcher() {

    @Override
    public void onTextChanged(CharSequence s, int start, int before, int count) {
        SimpleDateFormat sdf = new SimpleDateFormat("dd-MM-yyyy");
        Date dateFromPicker = null;
        if(listOfSBPData!=null)
            try {
                dateFromPicker = sdf.parse(tvDateTo.getText().toString());
                String dt = tvDateTo.getText().toString();
                SimpleDateFormat dateFormat = new SimpleDateFormat( "dd-MM-yyyy" );
                Calendar cal = Calendar.getInstance();
                cal.setTime( dateFormat.parse( tvDateTo.getText().toString() ) );
                cal.add( Calendar.DATE, 7 );
                  int  year = cal.get(Calendar.YEAR);
                  int  month = cal.get(Calendar.MONTH);
                  int day = cal.get(Calendar.DAY_OF_MONTH);
                    tvDateFrom.setText(""+day+"-"+(month+1)+"-"+year);
                    addSBPSeries();
            } catch (ParseException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
    }

    @Override
    public void beforeTextChanged(CharSequence s, int start, int count,
            int after) {
        // TODO Auto-generated method stub

    }

    @Override
    public void afterTextChanged(Editable s) {


    }
});

当我在 tvDateFrom 中更改日期时,我会从 tvDateTo 编辑视图添加一周的日期。当我将日期添加到 tvDateTo 时,我会从日期中减去一周。当我只有一个文本观察器时,一切正常。例如:当我只为 tvDateFrom 设置 textChangeListener 时,一切正常,但是当我第二次添加 textchangelistener 时出现错误:

03-17 14:55:04.440: E/AndroidRuntime(25190): FATAL EXCEPTION: main
03-17 14:55:04.440: E/AndroidRuntime(25190): java.lang.StackOverflowError
03-17 14:55:04.440: E/AndroidRuntime(25190):    at android.graphics.Paint.getTextRunAdvances(Paint.java:1681)
03-17 14:55:04.440: E/AndroidRuntime(25190):    at android.text.MeasuredText.addStyleRun(MeasuredText.java:170)
03-17 14:55:04.440: E/AndroidRuntime(25190):    at android.text.MeasuredText.addStyleRun(MeasuredText.java:210)
03-17 14:55:04.440: E/AndroidRuntime(25190):    at android.text.StaticLayout.generate(StaticLayout.java:283)
03-17 14:55:04.440: E/AndroidRuntime(25190):    at android.text.DynamicLayout.reflow(DynamicLayout.java:298)
03-17 14:55:04.440: E/AndroidRuntime(25190):    at android.text.DynamicLayout.<init>(DynamicLayout.java:174)
03-17 14:55:04.440: E/AndroidRuntime(25190):    at android.widget.TextView.makeSingleLayout(TextView.java:6613)
03-17 14:55:04.440: E/AndroidRuntime(25190):    at android.widget.TextView.makeNewLayout(TextView.java:6495)
03-17 14:55:04.440: E/AndroidRuntime(25190):    at android.widget.TextView.checkForRelayout(TextView.java:7064)
03-17 14:55:04.440: E/AndroidRuntime(25190):    at android.widget.TextView.setText(TextView.java:3479)
03-17 14:55:04.440: E/AndroidRuntime(25190):    at android.widget.TextView.setText(TextView.java:3335)
03-17 14:55:04.440: E/AndroidRuntime(25190):    at android.widget.EditText.setText(EditText.java:90)
03-17 14:55:04.440: E/AndroidRuntime(25190):    at android.widget.TextView.setText(TextView.java:3310)
03-17 14:55:04.440: E/AndroidRuntime(25190):    at MeasurementsChart$20.onTextChanged(AmmsMeasurementsChart.java:572)
03-17 14:55:04.440: E/AndroidRuntime(25190):    at android.widget.TextView.sendOnTextChanged(TextView.java:8155)
03-17 14:55:04.440: E/AndroidRuntime(25190):    at android.widget.TextView.setText(TextView.java:3482)
03-17 14:55:04.440: E/AndroidRuntime(25190):    at android.widget.TextView.setText(TextView.java:3335)
03-17 14:55:04.440: E/AndroidRuntime(25190):    at android.widget.EditText.setText(EditText.java:90)
03-17 14:55:04.440: E/AndroidRuntime(25190):    at android.widget.TextView.setText(TextView.java:3310)
03-17 14:55:04.440: E/AndroidRuntime(25190):    at MeasurementsChart$21.onTextChanged(AmmsMeasurementsChart.java:611)
03-17 14:55:04.440: E/AndroidRuntime(25190):    at android.widget.TextView.sendOnTextChanged(TextView.java:8155)
03-17 14:55:04.440: E/AndroidRuntime(25190):    at android.widget.TextView.setText(TextView.java:3482)
03-17 14:55:04.440: E/AndroidRuntime(25190):    at android.widget.TextView.setText(TextView.java:3335)
03-17 14:55:04.440: E/AndroidRuntime(25190):    at android.widget.EditText.setText(EditText.java:90)
03-17 14:55:04.440: E/AndroidRuntime(25190):    at android.widget.TextView.setText(TextView.java:3310)
03-17 14:55:04.440: E/AndroidRuntime(25190):    at MeasurementsChart$20.onTextChanged(AmmsMeasurementsChart.java:572)
03-17 14:55:04.440: E/AndroidRuntime(25190):    at android.widget.TextView.sendOnTextChanged(TextView.java:8155)
03-17 14:55:04.440: E/AndroidRuntime(25190):    at android.widget.TextView.setText(TextView.java:3482)
03-17 14:55:04.440: E/AndroidRuntime(25190):    at android.widget.TextView.setText(TextView.java:3335)
03-17 14:55:04.440: E/AndroidRuntime(25190):    at android.widget.EditText.setText(EditText.java:90)
03-17 14:55:04.440: E/AndroidRuntime(25190):    at android.widget.TextView.setText(TextView.java:3310)
03-17 14:55:04.440: E/AndroidRuntime(25190):    at MeasurementsChart$21.onTextChanged(AmmsMeasurementsChart.java:611)
03-17 14:55:04.440: E/AndroidRuntime(25190):    at android.widget.TextView.sendOnTextChanged(TextView.java:8155)
03-17 14:55:04.440: E/AndroidRuntime(25190):    at android.widget.TextView.setText(TextView.java:3482)
03-17 14:55:04.440: E/AndroidRuntime(25190):    at android.widget.TextView.setText(TextView.java:3335)
03-17 14:55:04.440: E/AndroidRuntime(25190):    at android.widget.EditText.setText(EditText.java:90)
03-17 14:55:04.440: E/AndroidRuntime(25190):    at android.widget.TextView.setText(TextView.java:3310)
03-17 14:55:04.440: E/AndroidRuntime(25190):    at MeasurementsChart$20.onTextChanged(AmmsMeasurementsChart.java:572)
03-17 14:55:04.440: E/AndroidRuntime(25190):    at android.widget.TextView.sendOnTextChanged(TextView.java:8155)
03-17 14:55:04.440: E/AndroidRuntime(25190):    at android.widget.TextView.setText(TextView.java:3482)
03-17 14:55:04.440: E/AndroidRuntime(25190):    at android.widget.TextView.setText(TextView.java:3335)
03-17 14:55:04.440: E/AndroidRuntime(25190):    at android.widget.EditText.setText(EditText.java:90)
03-17 14:55:04.440: E/AndroidRuntime(25190):    at android.widget.TextView.setText(TextView.java:3310)
03-17 14:55:04.440: E/AndroidRuntime(25190):    at MeasurementsChart$21.onTextChanged(AmmsMeasurementsChart.java:611)
03-17 14:55:04.440: E/AndroidRuntime(25190):    at android.widget.TextView.sendOnTextChanged(TextView.java:8155)
03-17 14:55:04.440: E/AndroidRuntime(25190):    at android.widget.TextView.setText(TextView.java:3482)
03-17 14:55:04.440: E/AndroidRuntime(25190):    at android.widget.TextView.setText(TextView.java:3335)
03-17 14:55:04.440: E/AndroidRuntime(25190):    at android.widget.EditText.setText(EditText.java:90)
03-17 14:55:04.440: E/AndroidRuntime(25190):    at android.widget.TextView.setText(TextView.java:3310)
03-17 14:55:04.440: E/AndroidRuntime(25190):    at MeasurementsChart$20.onTextChanged(AmmsMeasurementsChart.java:572)
03-17 14:55:04.440: E/AndroidRuntime(25190):    at android.widget.TextView.sendOnTextChanged(TextView.java:8155)
03-17 14:55:04.440: E/AndroidRuntime(25190):    at android.widget.TextView.setText(TextView.java:3482)
03-17 14:55:04.440: E/AndroidRuntime(25190):    at android.widget.TextView.setText(TextView.java:3335)
03-17 14:55:04.440: E/AndroidRuntime(25190):    at android.widget.EditText.setText(EditText.java:90)
03-17 14:55:04.440: E/AndroidRuntime(25190):    at android.widget.TextView.setText(TextView.java:3310)
03-17 14:55:04.440: E/AndroidRuntime(25190):    at MeasurementsChart$21.onTextChanged(AmmsMeasurementsChart.java:611)
03-17 14:55:04.440: E/AndroidRuntime(25190):    at android.widget.TextView.sendOnTextChanged(TextView.java:8155)
03-17 14:55:04.440: E/AndroidRuntime(25190):    at android.widget.TextView.setText(TextView.java:3482)
03-17 14:55:04.440: E/AndroidRuntime(25190):    at android.widget.TextView.setText(TextView.java:3335)
03-17 14:55:04.440: E/AndroidRuntime(25190):    at android.widget.EditText.setText(EditText.java:90)
03-17 14:55:04.440: E/AndroidRuntime(25190):    at android.widget.TextView.setText(TextView.java:3310)
03-17 14:55:04.440: E/AndroidRuntime(25190):    at MeasurementsChart$20.onTextChanged(AmmsMeasurementsChart.java:572)
03-17 14:55:04.440: E/AndroidRuntime(25190):    at android.widget.TextView.sendOnText
4

1 回答 1

1

在这里试试我的答案:

https://stackoverflow.com/a/7222993/557179

根据您的需要进行调整。

希望能帮助到你。

于 2013-03-17T14:01:00.863 回答