0

当使用某个日期选择 datepicker 时,我正在尝试更新 textview。但它是 textview 没有更新。我正在使用两个日期选择器来更新两个不同的文本视图。这是我更新 TextViews 的代码:

public class SearchingActivity extends Activity {
    static final int DATE_PICKER_TO = 0;
    static final int DATE_PICKER_FROM = 1;

    private int fYear;
    private int fMonth;
    private int fDay;
    private int tYear;
    private int tMonth;
    private int tDay;
    TextView txtFrom;
    TextView txtTo;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        requestWindowFeature(Window.FEATURE_NO_TITLE);
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
                WindowManager.LayoutParams.FLAG_FULLSCREEN);
        setContentView(R.layout.layout_search);
        ImageButton btnFrom = (ImageButton) findViewById(R.id.btnDatePicker1);
        btnFrom.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View arg0) {
                // TODO Auto-generated method stub
                showDialog(DATE_PICKER_FROM);
            }
        });
        ImageButton btnTo = (ImageButton) findViewById(R.id.btnDatePicker2);
        btnTo.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View arg0) {
                // TODO Auto-generated method stub
                showDialog(DATE_PICKER_TO);
            }
        });
        txtFrom = (TextView) findViewById(R.id.lblsDate);
        txtTo = (TextView) findViewById(R.id.lbleDate);
        Button btnSubmit = (Button) findViewById(R.id.btnSubmitDate);
        btnSubmit.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View arg0) {
                // TODO Auto-generated method stub
                Intent intent = new Intent(getApplicationContext(),
                        MixTourListActivity.class);
                String varSDate = txtFrom.getText().toString();
                String varEDate = txtTo.getText().toString();
                intent.putExtra("startDate", varSDate);
                intent.putExtra("endDate", varEDate);
                startActivity(intent);
            }
        });
        final Calendar c = Calendar.getInstance();
        fYear = c.get(Calendar.YEAR);
        fMonth = c.get(Calendar.MONTH);
        fDay = c.get(Calendar.DAY_OF_MONTH);
        tYear = c.get(Calendar.YEAR);
        tMonth = c.get(Calendar.MONTH);
        tDay = c.get(Calendar.DAY_OF_MONTH);
        updateStartDisplay();
        updateEndDisplay();
    }

    private void updateEndDisplay() {
        // TODO Auto-generated method stub
        txtFrom.setText(new StringBuilder()
                // Month is 0 based so add 1
                .append(fMonth + 1).append("-").append(fDay).append("-")
                .append(fYear).append(" "));
    }

    private void updateStartDisplay() {
        // TODO Auto-generated method stub
        txtTo.setText(new StringBuilder()
                // Month is 0 based so add 1
                .append(tMonth + 1).append("-").append(tDay).append("-")
                .append(tYear).append(" "));
    }

    private DatePickerDialog.OnDateSetListener fDateSetListener = new DatePickerDialog.OnDateSetListener() {

        public void onDateSet(DatePicker view, int year, int monthOfYear,
                int dayOfMonth) {
            fYear = year;
            fMonth = monthOfYear;
            fDay = dayOfMonth;
            updateStartDisplay();
        }
    };
    private DatePickerDialog.OnDateSetListener tDateSetListener = new DatePickerDialog.OnDateSetListener() {

        public void onDateSet(DatePicker view, int year, int monthOfYear,
                int dayOfMonth) {
            tYear = year;
            tMonth = monthOfYear;
            tDay = dayOfMonth;
            updateEndDisplay();
        }
    };

    @Override
    protected Dialog onCreateDialog(int id) {
        switch (id) {
        case DATE_PICKER_FROM:
            return new DatePickerDialog(this, fDateSetListener, fYear, fMonth,
                    fDay);
        case DATE_PICKER_TO:
            return new DatePickerDialog(this, tDateSetListener, tYear, tMonth,
                    tDay);
        }
        return null;
    }

}
4

1 回答 1

0

fDateSetListener有:

fYear = year;
fMonth = monthOfYear;
fDay = dayOfMonth;
updateStartDisplay();

并且updateStartDisplay()有:

txtTo.setText(new StringBuilder()
// Month is 0 based so add 1
.append(tMonth + 1).append("-").append(tDay).append("-")
.append(tYear).append(" "));

因此tMonth,当您这样称呼时,家人并没有确定,fMonth而兄弟则是。

类似的原因tDateSetListener所以实际上,只需反转它们中设置的变量

fDateSetListener应该有:

tYear = year;
tMonth = monthOfYear;
tDay = dayOfMonth;
updateStartDisplay();

tDateSetListener应该有:

fYear = year;
fMonth = monthOfYear;
fDay = dayOfMonth;
updateEndDisplay();
于 2013-01-03T18:43:36.223 回答