2012-08-10 33 views
9

tôi có mã này ở đây trên menu tùy chọnAndroid: DatePicker và DatePicker Dialog

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(); 

khi, nói "Thêm sự kiện" trên menu option được nhấp, một Dialog xuất hiện, với một nút cho thấy một DatePickerDialog và bên cạnh đó là TextView phản ánh ngày được chọn trong DatePickerDialog, đây là lớp tôi nhận được từ Nhà phát triển Android về cách sử dụng 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; 
    } 
} 

Vì vậy, vấn đề của tôi là làm thế nào để tôi nhận được các giá trị trong milleseconds khi tôi bấm vào nút "Đặt" trên DatePickerDialog do đó sẽ tự động đóng nó, và quay trở lại hộp thoại của tôi, trong đó có các nút mà mở DatePickerDialog và một TextView phản ánh watever ngày được chọn DatePickerDialog ... i wont hiển thị một tôi đã chọn bên trong DatePickerDialog ...

Dưới đây là một bức tranh về những gì tôi có nghĩa là,

Vì vậy, khi tôi bấm vào Pick Nút Ngày một hộp DatePickerDialog xuất hiện như được thấy trong hình tiếp theo enter image description here

enter image description here

và khi tôi nhấp vào thiết lập, tôi muốn đưa vào tài khoản giá trị trong millseconds từ đó DatePickerDialog

Trả lời

13

Ở đây tôi Cung cấp một số mã Hope nó giúp bạn ..

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; 
} 
} 
+0

đây là là tủ quần áo với những gì tôi đã làm, khó khăn tôi đã thực hiện một số thay đổi và tôi đã không hoàn toàn sử dụng tất cả các phương pháp của bạn, nhưng đủ gần để tôi .. tnx – lemoncodes

+2

showDialog (DATE_DIALOG_ID); không được chấp nhận. Sử dụng phân đoạn hộp thoại thay vì http://stackoverflow.com/questions/11220820/the-method-showdialogint-from-the-type-activity-is-deprecated-in-android http://stackoverflow.com/questions/12710092/how -to-sử dụng-fragmentmanager-to-sobstitute-the-deprecated-showdialog –

13

Hộp thoại cũ không còn được dùng nữa. Thực hiện các mảnh vỡ:

Di chuyển Hoạt động của bạn để Fragment Hoạt động:

public class YourActivity extends FragmentActivity 

Tạo Fragment Dialog:

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); 
    } 

} 

thực hiện giao diện (gói: import android.app.DatePickerDialog.OnDateSetListener):

public class YourActivity extends FragmentActivity implements OnDateSetListener{ 

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

    } 

} 

Thêm chức năng 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) { 

    } 

} 
+0

Hoạt động như một sự quyến rũ. –

+0

Điều gì sẽ xảy ra nếu lần tiếp theo ai đó nhấp vào nút để nhận đoạn để hiển thị, tôi muốn ngày là ngày cuối cùng do người dùng chọn? –

+0

bạn gửi thông số hoặc tham số ban đầu với newInstance squema: http://developer.android.com/reference/android/app/DialogFragment.html – Hpsaturn

1

Bạn có thể thử mã này .. nó chắc chắn sẽ hữu ích cho bạn .. Không nghi ngờ gì!

protected Dialog onCreateDialog(int id) { 

    Calendar c=Calendar.getInstance(); 
    int Sysday=c.get(Calendar.DAY_OF_MONTH); 
    int Sysmonth=c.get(Calendar.MONTH); 
    int Sysyear=c.get(Calendar.YEAR); 

    int Sysmin=c.get(Calendar.MINUTE); 
    int Syshour=c.get(Calendar.HOUR); 


    switch (id) { 
    case TIME_DIALOG: 

     return new TimePickerDialog(this, myTimePicker , Syshour, Sysmin, false); 

    case DATE_DIALOG: 
     return new DatePickerDialog(this, myDatePicker, Sysyear, Sysmonth, Sysday); 
    } 
    return null; 
} 
1

Đây là chức năng giúp đỡ tôi mà lấy bối cảnh và TextView như tham số và đặt ngày trên mà xem văn bản khi người dùng chọn một ngày:

 public static void showDate(final Context context, final TextView textView) { 

    if (textView != null) { 
     final Calendar myCalendar = Calendar.getInstance(); 
     DatePickerDialog.OnDateSetListener date = new DatePickerDialog.OnDateSetListener() { 
      @Override 
      public void onDateSet(DatePicker view, int year, int monthOfYear, int dayOfMonth) { 
       // TODO Auto-generated method stub 
       myCalendar.set(Calendar.YEAR, year); 
       myCalendar.set(Calendar.MONTH, monthOfYear); 
       myCalendar.set(Calendar.DAY_OF_MONTH, dayOfMonth); 
       String myFormat = "MM/dd/yy"; // In which you need put here 
       SimpleDateFormat sdf = new SimpleDateFormat(myFormat, Locale.US); 
       //UIHelper.showLongToastInCenter(context, sdf.format(myCalendar.getTime())); 
       textView.setText(sdf.format(myCalendar.getTime())); 
      } 

     }; 
     new DatePickerDialog(context, date, myCalendar.get(Calendar.YEAR), myCalendar.get(Calendar.MONTH), myCalendar.get(Calendar.DAY_OF_MONTH)).show(); 
    } else { 
     UIHelper.showLongToastInCenter(context, "Unable to show Date picker"); 
    } 
} 
4

Trong XML thêm một TextView và một nút

<TextView 
    android:id="@+id/searchText" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:text="TextView" /> 

<Button 
    android:id="@+id/search" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:text="Search" /> 

Thêm đoạn mã sau vào file java

public class DatePickerDialogExample extends Activity { 

    TextView txtDate; 
    private int mYear, mMonth, mDay, mHour, mMinute; 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.main); 


     txtDate = (TextView) findViewById(R.id.searchText); 
     Button search = (Button) findViewById(R.id.search); 
     search.setOnClickListener(new View.OnClickListener() { 

     @Override 
     public void onClick(View v) { 
      // Process to get Current Date 
      final Calendar c = Calendar.getInstance(); 
      mYear = c.get(Calendar.YEAR); 
      mMonth = c.get(Calendar.MONTH); 
      mDay = c.get(Calendar.DAY_OF_MONTH); 

      // Launch Date Picker Dialog 
      DatePickerDialog dpd = new DatePickerDialog(PrayTimeActivity.this, 
        new DatePickerDialog.OnDateSetListener() { 

         @Override 
         public void onDateSet(DatePicker view, int year, 
           int monthOfYear, int dayOfMonth) { 
          // Display Selected date in textbox 
          txtDate.setText(dayOfMonth + "-" 
            + (monthOfYear + 1) + "-" + year); 

         } 
        }, mYear, mMonth, mDay); 
       dpd.show(); 
      } 
     }); 


    } 

} 
0
Calendar c1 = Calendar.getInstance();  
int year = c1.get(Calendar.YEAR); 
int month = c1.get(Calendar.MONTH); 
int day = c1.get(Calendar.DAY_OF_MONTH); 
DatePickerDialog.OnDateSetListener myDateListener = new DatePickerDialog.OnDateSetListener() { 

     public void onDateSet(DatePicker arg0, int arg1, int arg2, int arg3) { 
        if(arg0.isShown()){ 

            //do further code here 

           } 
          } 

          }; 
     DatePickerDialog dp = new DatePickerDialog(YourActivity.this, myDateListener, year, month, day); 
     dp.show();  
1

Hãy thử mã này nó là công việc chắc chắn:

  final Calendar c = Calendar.getInstance(); 
      mYear = c.get(Calendar.YEAR); 
      mMonth = c.get(Calendar.MONTH); 
      mDay = c.get(Calendar.DAY_OF_MONTH); 

      DatePickerDialog dpd = new DatePickerDialog(getActivity(), 
        new DatePickerDialog.OnDateSetListener() { 

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

          birth_Date.setText(dayOfMonth + "-" 
            + (monthOfYear + 1) + "-" + year); 
         } 
        }, mYear, mMonth, mDay); 
      dpd.show();