Android实现日期时间选择对话框
时间:2022-09-26 09:35:15|栏目:Android代码|点击: 次
日期/时间选择对话框(DatePickerDialog和TimePickerDialog)的使用,供大家参考,具体内容如下
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" > <EditText android:id="@+id/edit" android:layout_width="200dp" android:layout_height="wrap_content" /> <Button android:id="@+id/button1" android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_marginTop="20dp" android:text="日期选择器" /> <Button android:id="@+id/button2" android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_marginTop="20dp" android:text="时间选择器" /> </LinearLayout>
public class MainActivity extends Activity { // 实例化控件 private Button dateButton; private Button timeButton; private EditText editText; private DatePickerDialog dateDialog; private TimePickerDialog timeDialog; private int year, monthOfYear, dayOfMonth, hourOfDay, minute; @Override protected void onCreate(Bundle savedInstanceState) { // TODO Auto-generated method stub super.onCreate(savedInstanceState); setContentView(R.layout.main); // 通过findViewById找到控件 dateButton = (Button) findViewById(R.id.button1); timeButton = (Button) findViewById(R.id.button2); editText = (EditText) findViewById(R.id.edit); // 通过Calendar对象来获取年、月、日、时、分的信息 Calendar calendar = Calendar.getInstance(); year = calendar.get(calendar.YEAR); monthOfYear = calendar.get(calendar.MONTH); dayOfMonth = calendar.get(calendar.DAY_OF_MONTH); hourOfDay = calendar.get(calendar.HOUR_OF_DAY); minute = calendar.get(calendar.MINUTE); /* * 实例化DatePickerDialog */ dateDialog = new DatePickerDialog(this, new OnDateSetListener() { @Override public void onDateSet(DatePicker arg0, int year, int monthOfYear, int dayOfMonth) { // 把获取的日期显示在文本框内,月份从0开始计数,所以要加1 String text = year + "-" + (monthOfYear + 1) + "-" + dayOfMonth; editText.setText(text); } }, year, monthOfYear, dayOfMonth); // 后面的三个参数对应于上面的年、月、日 /** * 对日期选择器按钮设置监听事件 */ dateButton.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View arg0) { // 点击日期选择器按钮时显示出日期对话框 dateDialog.show(); } }); /* * 实例化TimePickerDialog */ timeDialog = new TimePickerDialog(this, new OnTimeSetListener() { @Override public void onTimeSet(TimePicker view, int hourOfDay, int minute) { // TODO Auto-generated method stub Toast.makeText(MainActivity.this, hourOfDay + ":" + minute, Toast.LENGTH_LONG).show(); } }, hourOfDay, minute, true); // 最后一个参数设置是否为24小时制 /** * 对时间选择器按钮设置监听事件 */ timeButton.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View arg0) { // 点击时间选择器按钮时显示出时间对话框 timeDialog.show(); } }); } }