日期对话框的toast总是提前显示
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 | public class MainActivity extends AppCompatActivity { int year1,month,day; @Override protected void onCreate(Bundle savedInstanceState) { super .onCreate(savedInstanceState); setContentView(R.layout.activity_main); } public void myclick(View v){ switch (v.getId()){ case R.id.button_date: buttonData(); break ; } } private void buttonData() { new DatePickerDialog( this , new DatePickerDialog.OnDateSetListener() { @Override public void onDateSet(DatePicker view, int year, int monthOfYear, int dayOfMonth) { year1 = year; month = monthOfYear; day = dayOfMonth; } }, 2016 , 10 , 8 ).show(); DateShow(); } private void DateShow(){ Toast.makeText(MainActivity. this ,year1+ "年" +month+ "月" +day+ "日" ,Toast.LENGTH_SHORT).show(); } } |
DatePickerDialog有没有类似setPositiveButton的方法啊?
20
收起
正在回答
1回答
给你个小例子,你可以去尝试一下哦,手动设置确定和取消按钮。
public void pickDate(View view) { Calendar cal = Calendar.getInstance(); final DatePickerDialog mDialog = new DatePickerDialog(this, null, cal.get(Calendar.YEAR), cal.get(Calendar.MONTH), cal.get(Calendar.DAY_OF_MONTH)); //手动设置按钮 mDialog.setButton(DialogInterface.BUTTON_POSITIVE, "完成", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { //通过mDialog.getDatePicker()获得dialog上的DatePicker组件,然后可以获取日期信息 DatePicker datePicker = mDialog.getDatePicker(); int year = datePicker.getYear(); int month = datePicker.getMonth(); int day = datePicker.getDayOfMonth(); System.out.println(year + "," + month + "," + day); } }); //取消按钮,如果不需要直接不设置即可 mDialog.setButton(DialogInterface.BUTTON_NEGATIVE, "取消", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { System.out.println("BUTTON_NEGATIVE~~"); } }); mDialog.show(); }
Android零基础入门2018版
- 参与学习 人
- 提交作业 5461 份
- 解答问题 7238 个
此次推出的专题为Android攻城狮培养计划的第一部分语法与界面基础篇,将带大家从0开始学习Android开发。
了解课程
恭喜解决一个难题,获得1积分~
来为老师/同学的回答评分吧
0 星