Android - 从对话框Timessquare库中获取所选日期

时间:2014-07-29 06:56:44

标签: android datetime

我是Android应用开发的新手。我正在使用timessquare库来显示datepicker。我无法从对话框中获取我的edittext中设置的选定日期。

我应该把et1.setText(sel_date);

放在哪里
  public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.avail_layout);


    final Calendar nextYear = Calendar.getInstance();
    nextYear.add(Calendar.YEAR, 1);

    final Calendar lastYear = Calendar.getInstance();
    lastYear.add(Calendar.YEAR, -1);



    et1 = (EditText) findViewById(R.id.date);

    et1.setOnClickListener(new OnClickListener() {
        @Override public void onClick(View view) {
          dialogView = (CalendarPickerView) getLayoutInflater().inflate(R.layout.dialog, null, false);
          dialogView.init(lastYear.getTime(), nextYear.getTime()) //
              .withSelectedDate(new Date());
          theDialog =
              new AlertDialog.Builder(avail_activity.this).setTitle("Select a Date")
                  .setView(dialogView)
                  .setNeutralButton("Done", new DialogInterface.OnClickListener() {
                    @Override public void onClick(DialogInterface dialogInterface, int i) {

                        dialogInterface.dismiss();


                    }
                  })
                  .create();
          theDialog.setOnShowListener(new DialogInterface.OnShowListener() {
            @Override
            public void onShow(DialogInterface dialogInterface) {
              Log.d(TAG, "onShow: fix the dimens!");
              dialogView.fixDialogDimens();
            }
          });
          theDialog.show();

        }

      }

            );

2 个答案:

答案 0 :(得分:1)

时域广告库拥有自己的单元格clicklistener

@Override
public boolean onCellClicked(Date date) {
   Log.i("DATE", " Date: " + date.toString());
   return false; }

答案 1 :(得分:0)

如果你的代码工作正常,你准备好了约会,试试这个:

dialogView.setDateSelectableFilter(new com.squareup.timessquare.CalendarPickerView.DateSelectableFilter()
 {
     @Override
     public boolean isDateSelectable(Date date2)
     {
       Calendar calendar2 = Calendar.getInstance();
       calendar2.setTime(date2);
       et1.setText(ConvertToDateStrig(calendar2.getTime()));
       dialog.dismiss();
       return true;
     }
 });

将日期转换为sting:

public static String ConvertToDateStrig(Date date){
        SimpleDateFormat dateformat= new SimpleDateFormat("dd MMM yyyy");
        return dateformat.format(date.getTime());
    }