Czy ktoś może wysłać kod przykładowy do prostego selektora dat w systemie Android.Selekcja dat w systemie Android
Jeśli wybór daty nie jest możliwy w systemie Android, wymagana jest opcja wyboru daty.
Czy ktoś może wysłać kod przykładowy do prostego selektora dat w systemie Android.Selekcja dat w systemie Android
Jeśli wybór daty nie jest możliwy w systemie Android, wymagana jest opcja wyboru daty.
Użyj DatePicker
http://developer.android.com/reference/android/widget/DatePicker.html
Jest availible od API Level 1
tu przykład jak używać DatePickerDialog.
Pierwszy dodać TextView oraz przycisk do layout.xml
<Button android:id="@+id/myDatePickerButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Choose Date"/>
<TextView android:id="@+id/showMyDate"
android:layout_width="fill_parent"
android:layout_height="wrap_content"/>
Dalej trzeba zainicjować przycisk i TextView w Metodzie onCreate swojego układu. potrzebujesz tej klasy zmienne
private int mYear;
private int mMonth;
private int mDay;
private TextView mDateDisplay;
private Button mPickDate;
static final int DATE_DIALOG_ID = 0;
tu metoda onCreate
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
mDateDisplay = (TextView) findViewById(R.id.showMyDate);
mPickDate = (Button) findViewById(R.id.myDatePickerButton);
mPickDate.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
showDialog(DATE_DIALOG_ID);
}
});
// get the current date
final Calendar c = Calendar.getInstance();
mYear = c.get(Calendar.YEAR);
mMonth = c.get(Calendar.MONTH);
mDay = c.get(Calendar.DAY_OF_MONTH);
// display the current date
updateDisplay();
}
metoda UpdateDisplay:
private void updateDisplay() {
this.mDateDisplay.setText(
new StringBuilder()
// Month is 0 based so add 1
.append(mMonth + 1).append("-")
.append(mDay).append("-")
.append(mYear).append(" "));
}
Słuchacz zwrotna dla DatePickDialog
private DatePickerDialog.OnDateSetListener mDateSetListener =
new DatePickerDialog.OnDateSetListener() {
public void onDateSet(DatePicker view, int year,
int monthOfYear, int dayOfMonth) {
mYear = year;
mMonth = monthOfYear;
mDay = dayOfMonth;
updateDisplay();
}
};
Metoda onCreateDialog, zwany przez ShowDialog()
@Override
protected Dialog onCreateDialog(int id) {
switch (id) {
case DATE_DIALOG_ID:
return new DatePickerDialog(this,
mDateSetListener,
mYear, mMonth, mDay);
}
return null;
}
Nadzieja pomaga, używane i to działa dobrze.
Przykład z
http://developer.android.com/guide/tutorials/views/hello-datepicker.html
public class dateresult extends Activity
{
private TextView tvdisplaydate;
private DatePicker dpResult;
private Button bntchangedate;
private int year;
private int month;
private int day;
static final int DATE_DIALOG_ID = 999;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.result);
setCurrentdateonView();
addListenerOnButton();
}
public void setCurrentdateonView(){
tvdisplaydate = (TextView)findViewById(R.id.tvdate);
dpResult = (DatePicker) findViewById(R.id.dpResult);
final Calendar c = Calendar.getInstance();
year = c.get(Calendar.YEAR);
month = c.get(Calendar.MONTH) ;
day = c.get(Calendar.DAY_OF_MONTH);
tvdisplaydate.setText(new StringBuffer() .append(month+1).append("-").append(day).append("-").append(year).append(""));
dpResult.init(year, month, day, null);
}
public void addListenerOnButton(){
bntchangedate = (Button)findViewById(R.id.bntchangedate);
bntchangedate.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
showDialog(DATE_DIALOG_ID);
}
});
}
@Override
protected Dialog onCreateDialog(int id) {
// TODO Auto-generated method stub
switch(id){
case DATE_DIALOG_ID:
return new DatePickerDialog(this,datePickerLisner,year,month,day);
}
return null;
}
private DatePickerDialog.OnDateSetListener datePickerLisner = new DatePickerDialog.OnDateSetListener() {
@Override
public void onDateSet(DatePicker view, int Selectyear,int Selectmonth, int Selectday) {
year= Selectyear;
month= Selectmonth;
day = Selectday;
tvdisplaydate.setText(new StringBuilder()
.append(Selectmonth+1).append("-").append(Selectday).append("-").append(Selectyear).append(""));
dpResult.init(year, month, day, null);
}
};
}
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<Button
android:id="@+id/bntchangedate"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Change Date" />
<TextView
android:id="@+id/lbldate"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Current Date (M-D-YYYY) :"
android:textAppearance="?android:attr/textAppearanceLarge"/>
<TextView
android:id="@+id/tvdate"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text=""
android:textAppearance="?android:attr/textAppearanceLarge" />
<DatePicker
android:id="@+id/dpResult"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</LinearLayout>
public class DatePickerDialogFragment extends DialogFragment{
//ResidenceActivity date = new ResidenceActivity();
Handler mHandler ;
int mDay;
int mMonth;
int mYear;
public DatePickerDialogFragment(Handler h){
/** Getting the reference to the message handler instantiated in MainActivity class */
mHandler = h;
}
@Override
public Dialog onCreateDialog(Bundle savedInstanceState){
/** Creating a bundle object to pass currently set date to the fragment */
Bundle b = getArguments();
/** Getting the day of month from bundle */
mDay = b.getInt("set_day");
/** Getting the month of year from bundle */
mMonth = b.getInt("set_month");
/** Getting the year from bundle */
mYear = b.getInt("set_year");
/** DatePickerDialog's "Set" click listener */
DatePickerDialog.OnDateSetListener listener = new DatePickerDialog.OnDateSetListener() {
// @Override
public void onDateSet(DatePicker view, int year, int monthOfYear, int dayOfMonth) {
mDay = dayOfMonth;
mMonth = monthOfYear;
mYear = year;
/** Creating a bundle object to pass currently set date to the fragment */
Bundle b = new Bundle();
/** Adding currently set day to bundle object */
b.putInt("set_day", mDay);
/** Adding currently set month to bundle object */
b.putInt("set_month", mMonth);
/** Adding currently set year to bundle object */
b.putInt("set_year", mYear);
/** Adding Current date in a string to bundle object */
b.putString("set_date", Integer.toString(mDay) + "/" + Integer.toString(mMonth+1) + "/" + Integer.toString(mYear));
/** Creating an instance of Message */
Message m = new Message();
/** Setting bundle object on the message object m */
m.setData(b);
/** Message m is sending using the message handler instantiated in MainActivity class */
mHandler.sendMessage(m);
}
};
/** Opening the DatePickerDialog window */
return new DatePickerDialog(getActivity(), listener, mYear, mMonth, mDay);
}
}
Przepraszam, że to widziałem i musiałem przekazać jakieś uwagi. 1. Handler w konstruktorze Aahh !!!? Nigdy nie używaj konstruktorów z komponentami Lifecycled. 2. Użyj wartości stałych dla wartości pakietu, co zmniejszy prawdopodobieństwo popełnienia błędów. 3. Handlery to trochę hackowanie, które nie zadziała, jeśli twoja aktywność jest lifecycled. Więc może poszukaj lepszego podejścia do tego. –
Na czym polega problem (Handler)? –
Oto zaktualizowana wersja, która dokumentuje wsteczną kompatybilność z Biblioteki Support:
http://developer.android.com/guide/topics/ui/controls/pickers.html#DatePicker
Przyjęty odpowiedź nie obsłużyć użytkownika klikając Anuluj w th e Wybór daty. W takim przypadku nic nie powinno być aktualizowane.
Chciałem zrobić to samo, co OP, ale chciałem tylko to w linii. Oto, co kiedyś:
Tworzenie wywołania zwrotnego, która będzie wywoływana po naciśnięciu przycisku OK wyboru daty kliknięciu:
public void datePicked(int year, int month, int day) {
buttonDate.setText(String.valueOf(year) + "/" +
String.valueOf(month) + "/" + String.valueOf(day));
}
Następnie należy użyć następującego kodu w metodzie gdzie utworzyć przycisk, aby wybrać datę:
buttonDate = (Button) rootView.findViewById(R.id.button_select_date);
buttonDate.setText("Select Date");
buttonDate.setOnClickListener(new View.OnClickListener() {
public void setReturnDate(int year, int month, int day) {
datePicked(year, month, day);
}
@Override
public void onClick(View v) {
Dialog datePickerDialog = new DatePickerDialog(activity, new DatePickerDialog.OnDateSetListener() {
@Override
public void onDateSet(DatePicker view, int year, int monthOfYear, int dayOfMonth) {
}
}, leagueEndDate.get(Calendar.YEAR), leagueEndDate.get(Calendar.MONTH), leagueEndDate
.get(Calendar.DAY_OF_MONTH));
datePickerDialog.setOnDismissListener(new DialogInterface.OnDismissListener() {
@Override
public void onDismiss(DialogInterface dialog) {
setReturnDate(((DatePickerDialog) dialog).getDatePicker().getYear(),
((DatePickerDialog) dialog).getDatePicker().getMonth(), ((DatePickerDialog) dialog)
.getDatePicker().getDayOfMonth());
}
});
datePickerDialog.show();
}
});
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" />
zastosowanie: import java.util.Kalendarz;
zamiast: importować android.icu.util.Calendar.
ten pakiet obsługuje tylko interfejs API poziomu 24 nie mniej niż ten. Jest to pakiet, którego musimy użyć, aby pobrać kalendarz w oknie dialogowym, działa to poprawnie.
Jak tego użyć w Fragmentie? – Milad