我的DatePicker在第一次尝试时无法将setText()设置为textView

时间:2018-02-04 03:47:27

标签: java android datepicker

我遇到了这个问题,因为我的DatePicker在第一次尝试时没有将文本设置为textView但是在第一次尝试之后它可以设置没有任何问题。

这是我的setDate函数,点击Linearlayout后会有效。

public void setBirthDate(){
    final Calendar calendar = Calendar.getInstance();
    int year = calendar.get(Calendar.YEAR);
    int month = calendar.get(calendar.MONTH);
    int day = calendar.get(calendar.DAY_OF_MONTH);

    DatePickerDialog dialog = new DatePickerDialog( setting.this,
            datePickerDialog,
            year, month, day);
    dialog.getWindow();
    dialog.show();
    datePickerDialog = new DatePickerDialog.OnDateSetListener() {
        @Override
        public void onDateSet(DatePicker datePicker, int year, int month, int day) {
            month += 1;
            String date = null;
            if (month < 10) {
                if (day < 10) {
                    date = "0" + day + "/" + "0" + month + "/" + year;
                } else if (day >= 10) {
                    date = day + "/" + "0" + month + "/" + year;
                }
            } else if (month >= 10) {
                if (day < 10) {
                    date = "0" + day + "/" + month + "/" + year;
                } else if (day >= 10) {
                    date = day + "/" + month + "/" + year;
                }
            }
            Toast.makeText(setting.this, date, Toast.LENGTH_SHORT).show();
            textView_birthdate.setText(date);
        }
    };
}

这就是我处理事件的方式。

@Override
public void onClick(View v){
    if(v.getId() == R.id.settinglo_birthdate){
        setBirthDate();
    }
else if(){}
...
...
...
}

这是我的onCreate

 @Override
public void onCreate(Bundle savedInstanceState){
    super.onCreate(savedInstanceState);
    setContentView(R.layout.setting_02);
    list_linear = new ArrayList<>();
    list_textview = new ArrayList<>();
    toolbar = (android.support.v7.widget.Toolbar) findViewById(R.id.settingtb);
    toolbar.setCollapsible(true);
    setSupportActionBar(toolbar);
    getSupportActionBar().setTitle("Setting");
    getSupportActionBar().setDisplayHomeAsUpEnabled(true);
    getSupportActionBar().setDefaultDisplayHomeAsUpEnabled(true);

    initialVIew();
}

这是initialView()方法

public void initialVIew(){

    /* layout initial & add to list */
    list_linear.add(linear_fname = (LinearLayout) findViewById(R.id.settinglo_fname));
    list_linear.add(linear_lname = (LinearLayout) findViewById(R.id.settinglo_lname));
    list_linear.add(linear_birthdate = (LinearLayout) findViewById(R.id.settinglo_birthdate));
    list_linear.add(linear_gender = (LinearLayout) findViewById(R.id.settinglo_gender));
    list_linear.add(linear_tel = (LinearLayout) findViewById(R.id.settinglo_tel));
    list_linear.add(linear_carefname = (LinearLayout) findViewById(R.id.settinglo_c_fname) );
    list_linear.add(linear_carelname = (LinearLayout) findViewById(R.id.settinglo_c_lname) );
    list_linear.add(linear_careemail = (LinearLayout) findViewById(R.id.settinglo_c_email) );
    list_linear.add(linear_caretel = (LinearLayout) findViewById(R.id.settinglo_c_tel) );
    list_linear.add(linear_min = (LinearLayout) findViewById(R.id.settinglo_glucose_min) );
    list_linear.add(linear_max = (LinearLayout) findViewById(R.id.settinglo_glucose_max) );

    /* textView initial & add to list */
    list_textview.add(textView_fname = (TextView) findViewById(R.id.settingtv_fname));
    list_textview.add(textView_lname = (TextView) findViewById(R.id.settingtv_lname));
    list_textview.add(textView_birthdate = (TextView) findViewById(R.id.settingtv_birthdate));
    list_textview.add(textView_gender = (TextView) findViewById(R.id.settingtv_gender));
    list_textview.add(textView_tel = (TextView) findViewById(R.id.settingtv_tel));
    list_textview.add(textView_carefname = (TextView) findViewById(R.id.settingtv_c_fname) );
    list_textview.add(textView_carelname = (TextView) findViewById(R.id.settingtv_c_lname) );
    list_textview.add(textView_careemail = (TextView) findViewById(R.id.settingtv_c_email) );
    list_textview.add(textView_caretel = (TextView) findViewById(R.id.settingtv_c_tel) );
    list_textview.add(textView_min = (TextView) findViewById(R.id.settingtv_glucose_min) );
    list_textview.add(textView_max = (TextView) findViewById(R.id.settingtv_glucose_max) );

    // layout set event
    for(LinearLayout linearLayout : list_linear){
        linearLayout.setOnClickListener(this);
    }

    // textView set event
    for(TextView textView : list_textview){
        textView.setOnClickListener(this);
    }
}

现在我对其他onClick混淆onClick其他LinearLayout工作正常没有任何问题。只有第一次不是第一次的setDate。

1 个答案:

答案 0 :(得分:0)

您应该像下面这样修改setBirthDate(): -

在初始化新的DatePickerDialog()之前,您应首先启动新的DatePickerDialog.OnDateSetListener()

public void setBirthDate(){
        final Calendar calendar = Calendar.getInstance();
        int year = calendar.get(Calendar.YEAR);
        int month = calendar.get(calendar.MONTH);
        int day = calendar.get(calendar.DAY_OF_MONTH);


        datePickerDialog = new DatePickerDialog.OnDateSetListener() {
            @Override
            public void onDateSet(DatePicker datePicker, int year, int month, int day) {
                month += 1;
                String date = null;
                if (month < 10) {
                    if (day < 10) {
                        date = "0" + day + "/" + "0" + month + "/" + year;
                    } else if (day >= 10) {
                        date = day + "/" + "0" + month + "/" + year;
                    }
                } else if (month >= 10) {
                    if (day < 10) {
                        date = "0" + day + "/" + month + "/" + year;
                    } else if (day >= 10) {
                        date = day + "/" + month + "/" + year;
                    }
                }
                Toast.makeText(SimpleActivity.this, date, Toast.LENGTH_SHORT).show();
                textView_birthdate.setText(date);
            }
        };

        DatePickerDialog dialog = new DatePickerDialog( SimpleActivity.this,
                datePickerDialog,
                year, month, day);
        dialog.getWindow();
        dialog.show();
    }