Made a date with DatePickerDialog chosen to do so depressed for a few days now want to use the DatePicker control to achieve.
Function described below, press a button to pop up DatePicker, it has two buttons "OK" and "Cancel."
Press "OK" to set the date on the TextView and close the pop-up DatePicker, press Cancel directly off DatePicker.
Do not use showDialog (DATE_ID); this method, the editor suggesting that the method has been abandoned.
My code is as follows, not bombs DatePicker window click the button.
<-! Main posts under Banner (D4) -><-! Posts under the main text (D5) ->
public class MainActivity extends Activity {
private TextView text;
private Button button;
private DatePicker datepicker;
private LinearLayout mainlayout;
@ Override
protected void onCreate (Bundle savedInstanceState) {
super.onCreate (savedInstanceState);
setContentView (R.layout.activity_main);
text = (TextView) findViewById (R.id.textView1);
button = (Button) findViewById (R.id.button1);
button.setOnClickListener (new Button.OnClickListener () {
@ Override
public void onClick (View arg0) {
/ / TODO Auto-generated method stub
Calendar calendar = Calendar.getInstance (TimeZone.getDefault ());
int year = calendar.get (Calendar.YEAR);
int month = calendar.get (Calendar.MONTH);
int day = calendar.get (Calendar.DAY_OF_MONTH);
datepicker = new DatePicker (MainActivity.this);
datepicker.init (year, month, day, new OnDateChangedListener () {
@ Override
public void onDateChanged (DatePicker arg0, int arg1,
int arg2, int arg3) {
/ / TODO Auto-generated method stub
}
});
/ / MainActivity have layout, I would not be a problem this add?
mainlayout = new LinearLayout (MainActivity.this);
/ / Error
next sentence/ / MainLayout.setOrientation (LinearLayout.VERTICAL);
mainlayout.addView (datepicker);
}
});
}
Reply:
Take advantage of Android's API documentation - which can be found in the answer ~
https://developer.android.com/guide/topics/ui/controls/pickers.html
Reply:
I have done according to the document, but the selection box pops up to date with the documentation provided by the pictures are not the same.
Documentation has two buttons, my only an OK button. Depressed
Reply:
I used to ask how this DatePicker code created ah? On the right looks like a calendar and more stuff

Reply:
Get their own. Code stickers to share with you.
Choose the date on android4.0 only one button issue.
public class MainActivity extends Activity {
private Button button;
private int mYear, mMonth, mDay;
@ Override
protected void onCreate (Bundle savedInstanceState) {
super.onCreate (savedInstanceState);
setContentView (R.layout.activity_main);
button = (Button) findViewById (R.id.button1);
button.setOnClickListener (new Button.OnClickListener () {
@ Override
public void onClick (View arg0) {
/ / TODO Auto-generated method stub
Calendar c = Calendar.getInstance ();
mYear = c.get (Calendar.YEAR);
mMonth = c.get (Calendar.MONTH);
mDay = c.get (Calendar.DAY_OF_MONTH);
final DatePicker datepicker = new DatePicker (MainActivity.this);
datepicker.init (mYear, mMonth, mDay, null);
datepicker.setCalendarViewShown (false);
AlertDialog.Builder builder = new AlertDialog.Builder (MainActivity.this);
builder.setTitle ("date option");
/ / Builder.setCancelable (true);
/ / Builder.setCanceledOnTouchOutside (true);
builder.setView (datepicker);
builder.setPositiveButton ("Ok", new DialogInterface.OnClickListener () {
@ Override
public void onClick (DialogInterface arg0, int arg1) {
/ / TODO Auto-generated method stub
mYear = datepicker.getYear ();
mMonth = datepicker.getMonth () +1;
mDay = datepicker.getDayOfMonth ();
. Toast.makeText (MainActivity.this, mYear + "-" + mMonth + "-" + mDay, Toast.LENGTH_SHORT) show ();
}
});
builder.setNegativeButton ("Cancel", new DialogInterface.OnClickListener () {
@ Override
public void onClick (DialogInterface dialog, int which) {
/ / TODO Auto-generated method stub
}
});
builder.create () show ();.
}
});
}
}
No comments:
Post a Comment