Android:DatePicker和DatePicker对话框

lem*_*des 10 android android-datepicker

我在选项菜单上有这个代码

Dialog dialog = new Dialog(ScheduleActivity.this);
dialog.setTitle("Add Event");
dialog.setContentView(R.layout.add_even_on);
Button datePicker = (Button) dialog.findViewById(R.id.datePicker);
final DialogFragment dateFrag = new MyDatePicker();
    datePicker.setOnClickListener(new OnClickListener() {

            public void onClick(View v) {
                dateFrag.show(getSupportFragmentManager(), "datePicker");               
            }
        });
dialog.show();
Run Code Online (Sandbox Code Playgroud)

时说,选择菜单中的"添加事件"被点击时,会出现一个对话框,有一个按钮,显示DatePickerDialog和它旁边是一个TextView的,反映在选定的日期DatePickerDialog,这里是我的机器人会开发上了班如何使用DatePickerDialog.

class MyDatePicker extends DialogFragment implements DatePickerDialog.OnDateSetListener {
 int pYear;
 int pDay;
 int pMonth;

@Override
  public Dialog onCreateDialog(Bundle savedInstanceState) {
    // Use the current date as the default date in the picker
    final Calendar c = Calendar.getInstance();
    int year = c.get(Calendar.YEAR);
    int month = c.get(Calendar.MONTH);
    int day = c.get(Calendar.DAY_OF_MONTH);

    // Create a new instance of DatePickerDialog and return it
    return new DatePickerDialog(getActivity(), this, year, month, day);
  }

  public void onDateSet(DatePicker view, int year, int month, int day) {
    pYear = year;
    pDay = day;
    pMonth = month;
  }
}
Run Code Online (Sandbox Code Playgroud)

所以我的问题是,我如何获得milleseconds值当我点击"设置"就这又自动关闭它的DatePickerDialog,并返回到我的对话,其中包含打开它反映watever日期DatePickerDialog和一个TextView是按钮选择了DatePickerDialog ...我不会显示我在DatePickerDialog中选择的那个...

这是我的意思,

所以当我点击选择日期按钮时,会出现一个DatePickerDialog框,如下图所示 在此输入图像描述

在此输入图像描述

当我点击设置时,我想考虑该DatePickerDialog的毫秒值

Hps*_*urn 14

不推荐使用旧对话框.实现片段:

将您的活动迁移到片段活动:

public class YourActivity extends FragmentActivity 
Run Code Online (Sandbox Code Playgroud)

创建片段对话框:

public class TimePickerFragment extends DialogFragment {

    private OnDateSetListener listener;

    public TimePickerFragment(OnDateSetListener listener) {
        this.listener=listener;
    }

    @Override
    public Dialog onCreateDialog(Bundle savedInstanceState) {
        // Use the current time as the default values for the picker
        final Calendar c = Calendar.getInstance();
        int year = c.get(Calendar.YEAR);
        int month = c.get(Calendar.MONTH);
        int day = c.get(Calendar.DAY_OF_MONTH);

        // Create a new instance of TimePickerDialog and return it
        return new DatePickerDialog(getActivity(), listener, year,month,day);
    }

}
Run Code Online (Sandbox Code Playgroud)

实现接口(包:import android.app.DatePickerDialog.OnDateSetListener):

public class YourActivity extends FragmentActivity implements OnDateSetListener{

    @Override
    public void onDateSet(DatePicker view, int year, int monthOfYear,int dayOfMonth) {

    }

}
Run Code Online (Sandbox Code Playgroud)

添加showDialog函数:

public class YourActivity extends FragmentActivity implements OnDateSetListener{

    showDateDialog(){

        FragmentManager fm = getSupportFragmentManager();
        TimePickerFragment newFragment = new TimePickerFragment(this);
        newFragment.show(fm, "date_picker");

    }

    @Override
    public void onDateSet(DatePicker view, int year, int monthOfYear,int dayOfMonth) {

    }

}
Run Code Online (Sandbox Code Playgroud)


jan*_*sad 12

在这里,我提供一些代码希望它帮助你..

public class NewReminder extends Activity {
private static final int DATE_DIALOG_ID = 1;
private int year;
private int month;
private int day;
EditText editTextDate;
private String currentDate;
private Context context;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.addnewreminder);
    initialize();
    context = getApplicationContext();
    OnClickListener listenerDate = new OnClickListener() {

        @Override
        public void onClick(View arg0) {

            final Calendar c = Calendar.getInstance();
            year = c.get(Calendar.YEAR);
            month = c.get(Calendar.MONTH);
            day = c.get(Calendar.DAY_OF_MONTH);
            showDialog(DATE_DIALOG_ID);
        }
    };
    editTextDate.setOnClickListener(listenerDate);

}

private void initialize() {
    // TODO Auto-generated method stub

    editTextDate = (EditText) findViewById(R.id.editTextDate);

}


private void updateDisplay() {
    currentDate = new StringBuilder().append(day).append(".")
            .append(month + 1).append(".").append(year).toString();

    Log.i("DATE", currentDate);
}

OnDateSetListener myDateSetListener = new OnDateSetListener() {

    @Override
    public void onDateSet(DatePicker datePicker, int i, int j, int k) {

        year = i;
        month = j;
        day = k;
        updateDisplay();
        editTextDate.setText(currentDate);
    }
};



@Override
protected Dialog onCreateDialog(int id) {
    switch (id) {
    case DATE_DIALOG_ID:
        return new DatePickerDialog(this, myDateSetListener, year, month,
                day);
    }
    return null;
}
}
Run Code Online (Sandbox Code Playgroud)

  • 的ShowDialog(DATE_DIALOG_ID); 已弃用.使用对话框片段http://stackoverflow.com/questions/11220820/the-method-showdialogint-from-the-type-activity-is-deprecated-in-android http://stackoverflow.com/questions/12710092/how -to-使用-fragmentmanager到sobstitute最弃用-的ShowDialog (2认同)