国内最全IT社区平台 联系我们 | 收藏本站
华晨云阿里云优惠2
您当前位置:首页 > php开源 > 综合技术 > 【Android学习笔记】DatePickerDialog和TimePickerDialog日期选择器和时间选择器

【Android学习笔记】DatePickerDialog和TimePickerDialog日期选择器和时间选择器

来源:程序员人生   发布时间:2015-02-11 08:52:46 阅读次数:5618次


(1)布局文件

<RelativeLayout 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:paddingBottom="@dimen/activity_vertical_margin" android:paddingLeft="@dimen/activity_horizontal_margin" android:paddingRight="@dimen/activity_horizontal_margin" android:paddingTop="@dimen/activity_vertical_margin" tools:context=".MainActivity" > <Button android:id="@+id/button2" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentTop="true" android:layout_centerHorizontal="true" android:layout_marginTop="156dp" android:text="时间对话框" /> <Button android:id="@+id/button1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignLeft="@+id/button2" android:layout_below="@+id/button2" android:layout_marginTop="14dp" android:text="日期对话框" /> <EditText android:id="@+id/editText1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentTop="true" android:layout_marginTop="41dp" android:ems="10" > <requestFocus /> </EditText> </RelativeLayout>

(2)类文件

package com.example.date_time_dailog; import java.util.Calendar; import android.app.Activity; import android.app.DatePickerDialog; import android.app.DatePickerDialog.OnDateSetListener; import android.app.TimePickerDialog; import android.app.TimePickerDialog.OnTimeSetListener; import android.os.Bundle; import android.view.Menu; import android.view.View; import android.view.View.OnClickListener; import android.widget.Button; import android.widget.DatePicker; import android.widget.EditText; import android.widget.TimePicker; import android.widget.Toast; public class MainActivity extends Activity { private Button button1; private Button button2; private EditText editText; private DatePickerDialog datePickerDialog; // 日期对话框 private int year, monthOfYear, dayOfMonth, hourOfDay, minute; private TimePickerDialog timePickerDialog;// 时间对话框 @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); button1 = (Button) this.findViewById(R.id.button1); button2 = (Button) this.findViewById(R.id.button2); editText = (EditText) this.findViewById(R.id.editText1); /* * 获得当前的日期及时间 */ Calendar calendar = Calendar.getInstance(); year = calendar.get(Calendar.YEAR); monthOfYear = calendar.get(Calendar.MONTH) + 1; dayOfMonth = calendar.get(Calendar.DAY_OF_MONTH); hourOfDay = calendar.get(Calendar.HOUR_OF_DAY); minute = calendar.get(Calendar.MINUTE); /* * 对日期选择器进行设置 */ datePickerDialog = new DatePickerDialog(this, new OnDateSetListener() { @Override public void onDateSet(DatePicker view, int year, int monthOfYear, int dayOfMonth) { String text = year + "-" + monthOfYear + "-" + dayOfMonth; editText.setText(text); } }, year, monthOfYear, dayOfMonth); button1.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { datePickerDialog.show(); } }); /* * 时间选择器的设置 */ timePickerDialog = new TimePickerDialog(MainActivity.this, new OnTimeSetListener() { @Override public void onTimeSet(TimePicker view, int hourOfDay, int minute) { String text = hourOfDay + ":" + minute; editText.setText(text); } }, hourOfDay, minute, true); button2.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { timePickerDialog.show(); } }); } @Override public boolean onCreateOptionsMenu(Menu menu) { getMenuInflater().inflate(R.menu.main, menu); return true; } }

(3)运行结果




生活不易,码农辛苦
如果您觉得本网站对您的学习有所帮助,可以手机扫描二维码进行捐赠
程序员人生
------分隔线----------------------------
分享到:
------分隔线----------------------------
关闭
程序员人生