And also how when choosing a date to score it in EditText? Can there be any methods? Tell me please.
2 answers
From the DatePickerDialog example :
main.xml
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="vertical"> <TextView android:id="@+id/tvDate" android:layout_width="fill_parent" android:layout_height="wrap_content" android:clickable="true" android:onClick="onclick" android:text="@string/hello" android:textSize="22sp"> </TextView> </LinearLayout> MainActivity.java
public class MainActivity extends Activity { int DIALOG_DATE = 1; int myYear = 2011; int myMonth = 02; int myDay = 03; TextView tvDate; public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); tvDate = (TextView) findViewById(R.id.tvDate); } public void onclick(View view) { showDialog(DIALOG_DATE); } protected Dialog onCreateDialog(int id) { if (id == DIALOG_DATE) { DatePickerDialog tpd = new DatePickerDialog(this, myCallBack, myYear, myMonth, myDay); return tpd; } return super.onCreateDialog(id); } OnDateSetListener myCallBack = new OnDateSetListener() { public void onDateSet(DatePicker view, int year, int monthOfYear, int dayOfMonth) { myYear = year; myMonth = monthOfYear; myDay = dayOfMonth; tvDate.setText("Today is " + myDay + "/" + myMonth + "/" + myYear); } }; } |
I recommend using MaterialDateAndTimePicker. In the gradle file, set the dependency compile 'com.wdullaer:materialdatetimepicker:1.5.4'
Call this method:
private void showDatePickerDialog(final EditText yourEditText) { Calendar now = Calendar.getInstance(); DatePickerDialog dpd = DatePickerDialog.newInstance( new DatePickerDialog.OnDateSetListener() { @SuppressLint("SetTextI18n") @Override public void onDateSet(DatePickerDialog view, int year, int monthOfYear, int dayOfMonth) { String month = String.valueOf(monthOfYear + 1); String day = String.valueOf(dayOfMonth); month = addZeroIfNeed(month); day = addZeroIfNeed(day); yourEditText.setText(day + "." + (month) + "." + year);//здесь в Ваш editText вставляйте } }, now.get(Calendar.YEAR), now.get(Calendar.MONTH), now.get(Calendar.DAY_OF_MONTH) ); dpd.show(self.getFragmentManager(), "Datepickerdialog"); } private String addZeroIfNeed(String string) { if (string.length() == 1) string = "0" + string; return string; } - Do not tell me that in the line 'dpd.show (self.getFragmentManager (), "Datepickerdialog"); 'executes' self'. - Kirill
- if you use fragments then getActivity () - Android Android
- self points to the context of this activity - Vladimir VSeos
|
DatePickeror "score" inEditTextand why inEditText, if the dialog box is used for input? See this answer /TextViewused here. You can use the style fromEditText(orSpinner) to make it look like an input box. - pavlofff