2010-12-01 6 views
5

DatePicker 예제를 제공하는 자습서를 제안하고 OnDateChangedListener, onDateChanged 등의 메서드를 사용하는 방법을 제안하십시오. 실제로 일부 사이트를 통해 가고 있지만 명확한 아이디어는 얻지 못했습니다.DatePicker 예제 in android

고맙습니다.

답변

9

DatePicker에 대한 Android 참조는 매우 좋습니다. 그것을보십시오 here.

private DatePicker datePicker; 
//monthofYear is between 0-11 
datePicker.init(2010, 11, 1, new OnDateChangedListener() { 
@Override 
public void onDateChanged(DatePicker view, int year, int monthOfYear,int dayOfMonth) { 
    // Notify the user. 

} 
}); 
+1

이 예제는 DatePicker가 아니라 DatePickerDialog를 사용하고 있다고 생각합니다. 이러한 메서드 (OnDateChangedListener, onDateChanged)는 DatePickerDialog와 함께 만 사용할 수 있습니까? – Jomia

+1

onDateChangedListener는 콜백 메소드 일뿐입니다. public void init (int year, int monthOfYear, int dayOfMonth, DatePicker.OnDateChangedListener onDateChangedListener) – Raunak

+1

@Miya가 도움이됩니까? – Raunak

2

참조. Example(); 여기

+1

이것은 Dialog에 관한 것입니다. 반면 Miya는 Activity –

+0

에서 DatePicker를 필요로합니다. @Paresh Mayani Miya는 동일해야합니다. –

+0

예 ... DatePicker가 필요합니다. 도와주세요. – Jomia

0
Step 1 : create a java file: 

package com.example.babs; 

import java.util.Calendar; 

import android.app.Activity; 
import android.app.DatePickerDialog; 
import android.app.Dialog; 
import android.app.DialogFragment; 
import android.os.Bundle; 
import android.view.View; 
import android.widget.DatePicker; 
import android.app.FragmentManager; 


public class EditUserInfo extends Activity { 


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

    } 

    public class DatePickerFragment extends DialogFragment 
    implements DatePickerDialog.OnDateSetListener { 


     // pgrm mark ---- ---- ----- ---- ---- ----- ---- ---- ----- ---- ---- ----- 

     @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) { 
      // Do something with the date chosen by the user 
     } 

    } 


    public void showDatePickerDialog(View v) { 

     FragmentManager fragmentManager = getFragmentManager(); 

     DialogFragment newFragment = new DatePickerFragment(); 
     newFragment.show(fragmentManager, "datePicker"); 
    } 

}// end main class EditUserInfo 

step 2: your xml file must contain : 

<?xml version="1.0" encoding="utf-8"?> 
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:background="@android:color/white" 
    android:fillViewport="true" > 
</ScrollView> 
<Button 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:text="@string/pick_date" 
    android:onClick="showDatePickerDialog" /> 
0

이 코드를 시도 할 수 있습니다 :

public 
static class DatePickerFragment extends DialogFragment implements 
DatePickerDialog.OnDateSetListener { 

@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) { 
// Do something with the date chosen by the user 
DateEdit.setText(day + "/" + (month + 1) + "/" + year); 
} 
} 

Example of DatePickerFragment and TimePickerFragment에서 촬영.