2011-07-11 31 views
0

我在我的应用程序中使用了日期选择器和时间选择器。我想在同一个对话框中设置。或者在同一时间我想设置日期和时间。这是可能的吗?怎么样?请给我一个例子。在android中设置日期时间选择器

在此先感谢。

回答

0

下面的代码将帮助你得到两个,

date_time_picker.xml

<?xml version="1.0" encoding="utf-8"?> 

<ScrollView android:id="@+id/scrollView1" 
android:layout_width="match_parent" android:layout_height="match_parent" 
xmlns:android="http://schemas.android.com/apk/res/android"> 


<LinearLayout android:layout_width="match_parent" 
    android:id="@+id/linearLayout1" android:orientation="vertical" 
    android:layout_height="match_parent"> 
    <DatePicker android:layout_width="wrap_content" 
     android:layout_height="wrap_content" android:id="@+id/datePicker"></DatePicker> 
    <TimePicker android:id="@+id/timePicker" 
     android:layout_width="wrap_content" android:layout_height="wrap_content"></TimePicker> 
    <TableRow android:id="@+id/tableRow1" android:layout_height="wrap_content" 
     android:layout_width="match_parent" android:gravity="center"> 

     <Button android:id="@+id/btnSetDateTime" 
      android:layout_height="wrap_content" android:text="Set" 
      android:layout_width="120dp"></Button> 

    </TableRow> 
</LinearLayout> 

使用Java代码:

ScrollView DTPicker = (ScrollView) View.inflate(context, 
        R.layout.date_time_picker, null); 

     Button btnSet = (Button) DTPicker.findViewById(R.id.btnSetDateTime); 
      DatePicker dp = (DatePicker) DTPicker.findViewById(R.id.datePicker); 
      TimePicker tp = (TimePicker) DTPicker.findViewById(R.id.timePicker); 

      btnSet.setOnClickListener(new OnClickListener() { 

       @Override 
       public void onClick(View v) { 
        String strDateTime = dp.getYear() + "-" 
          + (dp.getMonth() + 1) + "-" 
          + dp.getDayOfMonth() + " " 
          + tp.getCurrentHour() + ":" 
          + tp.getCurrentMinute(); 
}}); 

alert = new AlertDialog.Builder(context).create(); 
      alert.setTitle("Reminder"); 
      alert.setView(DTPicker); 
      alert.show(); 
+0

@kartik什么是警报这里,我不得不使用上下文语境 –

+0

什么做工精细 - your_ActivityName.this和AlertDialog警报=新AlertDialog。助洗剂(上下文).create(); –

+0

嘿你的这条线有一些问题 'ScrollView DTPicker =(ScrollView)View.inflate(context, R.layout.date_time_picker,null);' –

4

使用下面的函数

private void showDateTimeDialog() { 
    // Create the dialog 
    final Dialog mDateTimeDialog = new Dialog(this); 
    // Inflate the root layout 
    final RelativeLayout mDateTimeDialogView = (RelativeLayout) getLayoutInflater().inflate(R.layout.date_time_dialog, null); 
    // Grab widget instance 
    final DateTimePicker mDateTimePicker = (DateTimePicker) mDateTimeDialogView.findViewById(R.id.DateTimePicker); 
    // Check is system is set to use 24h time (this doesn't seem to work as expected though) 
    final String timeS = android.provider.Settings.System.getString(getContentResolver(), android.provider.Settings.System.TIME_12_24); 
    final boolean is24h = !(timeS == null || timeS.equals("12")); 

    // Update demo TextViews when the "OK" button is clicked 
    ((Button) mDateTimeDialogView.findViewById(R.id.SetDateTime)).setOnClickListener(new OnClickListener() { 

     public void onClick(View v) { 
      // TODO Auto-generated method stub 
      /*((TextView) findViewById(R.id.Date)).setText(mDateTimePicker.get(Calendar.YEAR) + "/" + (mDateTimePicker.get(Calendar.MONTH)+1) + "/" 
        + mDateTimePicker.get(Calendar.DAY_OF_MONTH)); 
      if (mDateTimePicker.is24HourView()) { 
       ((TextView) findViewById(R.id.Time)).setText(mDateTimePicker.get(Calendar.HOUR_OF_DAY) + ":" + mDateTimePicker.get(Calendar.MINUTE)); 
      } else { 
       ((TextView) findViewById(R.id.Time)).setText(mDateTimePicker.get(Calendar.HOUR) + ":" + mDateTimePicker.get(Calendar.MINUTE) + " " 
         + (mDateTimePicker.get(Calendar.AM_PM) == Calendar.AM ? "AM" : "PM")); 
      }*/ 

      String date = (mDateTimePicker.get(Calendar.MONTH)+1) + "/" + mDateTimePicker.get(Calendar.DAY_OF_MONTH) + "/" 
        +mDateTimePicker.get(Calendar.YEAR)+" "+mDateTimePicker.get(Calendar.HOUR_OF_DAY) + ":" + mDateTimePicker.get(Calendar.MINUTE)+" "+(mDateTimePicker.get(Calendar.AM_PM) == Calendar.AM ? "AM" : "PM"); 
      edt_admin_due_date.setText(date); 
      mDateTimeDialog.dismiss(); 
     }//MM/dd/yyyy hh:mm 
    }); 

    // Cancel the dialog when the "Cancel" button is clicked 
    ((Button) mDateTimeDialogView.findViewById(R.id.CancelDialog)).setOnClickListener(new OnClickListener() { 

     public void onClick(View v) { 
      // TODO Auto-generated method stub 
      mDateTimeDialog.cancel(); 
     } 
    }); 

    // Reset Date and Time pickers when the "Reset" button is clicked 
    ((Button) mDateTimeDialogView.findViewById(R.id.ResetDateTime)).setOnClickListener(new OnClickListener() { 

     public void onClick(View v) { 
      // TODO Auto-generated method stub 
      mDateTimePicker.reset(); 
     } 
    }); 

    // Setup TimePicker 
    mDateTimePicker.setIs24HourView(is24h); 
    // No title on the dialog window 
    mDateTimeDialog.requestWindowFeature(Window.FEATURE_NO_TITLE); 
    // Set the dialog content view 
    mDateTimeDialog.setContentView(mDateTimeDialogView); 
    // Display the dialog 
    mDateTimeDialog.show(); 
} 

和datetimepicker文件如下。

public class DateTimePicker extends RelativeLayout implements View.OnClickListener, OnDateChangedListener, OnTimeChangedListener { 

// DatePicker reference 
private DatePicker  datePicker; 
// TimePicker reference 
private TimePicker  timePicker; 
// ViewSwitcher reference 
private ViewSwitcher viewSwitcher; 
// Calendar reference 
private Calendar  mCalendar; 

// Constructor start 
public DateTimePicker(Context context) { 
    this(context, null); 
} 

public DateTimePicker(Context context, AttributeSet attrs) { 
    this(context, attrs, 0); 
} 

public DateTimePicker(Context context, AttributeSet attrs, int defStyle) { 
    super(context, attrs, defStyle); 

    // Get LayoutInflater instance 
    final LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
    // Inflate myself 
    inflater.inflate(R.layout.datetimepicker, this, true); 

    // Inflate the date and time picker views 
    final LinearLayout datePickerView = (LinearLayout) inflater.inflate(R.layout.datepicker, null); 
    final LinearLayout timePickerView = (LinearLayout) inflater.inflate(R.layout.timepicker, null); 

    // Grab a Calendar instance 
    mCalendar = Calendar.getInstance(); 
    // Grab the ViewSwitcher so we can attach our picker views to it 
    viewSwitcher = (ViewSwitcher) this.findViewById(R.id.DateTimePickerVS); 

    // Init date picker 
    datePicker = (DatePicker) datePickerView.findViewById(R.id.DatePicker); 
    datePicker.init(mCalendar.get(Calendar.YEAR), mCalendar.get(Calendar.MONTH), mCalendar.get(Calendar.DAY_OF_MONTH), this); 

    // Init time picker 
    timePicker = (TimePicker) timePickerView.findViewById(R.id.TimePicker); 
    timePicker.setOnTimeChangedListener(this); 

    // Handle button clicks 
    ((Button) findViewById(R.id.SwitchToTime)).setOnClickListener(this); // shows the time picker 
    ((Button) findViewById(R.id.SwitchToDate)).setOnClickListener(this); // shows the date picker 

    // Populate ViewSwitcher 
    viewSwitcher.addView(datePickerView, 0); 
    viewSwitcher.addView(timePickerView, 1); 
} 
// Constructor end 

// Called every time the user changes DatePicker values 
public void onDateChanged(DatePicker view, int year, int monthOfYear, int dayOfMonth) { 
    // Update the internal Calendar instance 
    mCalendar.set(year, monthOfYear, dayOfMonth, mCalendar.get(Calendar.HOUR_OF_DAY), mCalendar.get(Calendar.MINUTE)); 
} 

// Called every time the user changes TimePicker values 
public void onTimeChanged(TimePicker view, int hourOfDay, int minute) { 
    // Update the internal Calendar instance 
    mCalendar.set(mCalendar.get(Calendar.YEAR), mCalendar.get(Calendar.MONTH), mCalendar.get(Calendar.DAY_OF_MONTH), hourOfDay, minute); 
} 

// Handle button clicks 
public void onClick(View v) { 
    switch (v.getId()) { 
     case R.id.SwitchToDate: 
      v.setEnabled(false); 
      findViewById(R.id.SwitchToTime).setEnabled(true); 
      viewSwitcher.showPrevious(); 
      break; 

     case R.id.SwitchToTime: 
      v.setEnabled(false); 
      findViewById(R.id.SwitchToDate).setEnabled(true); 
      viewSwitcher.showNext(); 
      break; 
    } 
} 

// Convenience wrapper for internal Calendar instance 
public int get(final int field) { 
    return mCalendar.get(field); 
} 

// Reset DatePicker, TimePicker and internal Calendar instance 
public void reset() { 
    final Calendar c = Calendar.getInstance(); 
    updateDate(c.get(Calendar.YEAR), c.get(Calendar.MONTH), c.get(Calendar.DAY_OF_MONTH)); 
    updateTime(c.get(Calendar.HOUR_OF_DAY),c.get(Calendar.MINUTE)); 
} 

// Convenience wrapper for internal Calendar instance 
public long getDateTimeMillis() { 
    return mCalendar.getTimeInMillis(); 
} 

// Convenience wrapper for internal TimePicker instance 
public void setIs24HourView(boolean is24HourView) { 
    timePicker.setIs24HourView(is24HourView); 
} 

// Convenience wrapper for internal TimePicker instance 
public boolean is24HourView() { 
    return timePicker.is24HourView(); 
} 

// Convenience wrapper for internal DatePicker instance 
public void updateDate(int year, int monthOfYear, int dayOfMonth) { 
    datePicker.updateDate(year, monthOfYear, dayOfMonth); 
} 

// Convenience wrapper for internal TimePicker instance 
public void updateTime(int currentHour, int currentMinute) { 
    timePicker.setCurrentHour(currentHour); 
    timePicker.setCurrentMinute(currentMinute); 
} 

}

这将在我的代码

+0

感谢您使用'Settings.System.TIME_12_24'的解决方法...我的逻辑用于工作,该错误来自哪里?! – LostNomad311

相关问题