android 日期控件对话框
有时候,我用到的日期控件的时候,要用对话的形式体现出来的,下面开始吧:
1.创建一个android-time
2.包名:com.app
3.主activity的名字:MainActivity
4.布局文件: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/tv" android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="@string/hello" /> <Button android:id="@+id/button1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Button" /> </LinearLayout>
5.activity的代码
package com.app;
import java.text.SimpleDateFormat; import java.util.Calendar; import java.util.Locale; import com.app.R; import com.app.view.DatePickText; import android.app.Activity; import android.app.DatePickerDialog; import android.app.Dialog; import android.app.TimePickerDialog; import android.content.Context; import android.os.Bundle; import android.view.View; import android.view.View.OnClickListener; import android.widget.Button; import android.widget.DatePicker; import android.widget.TextView; import android.widget.TimePicker; public class MainActivity extends Activity { public static final int DATE = 0; public static final int TIME = 1; private final Calendar cal = Calendar .getInstance(Locale.SIMPLIFIED_CHINESE); private SimpleDateFormat df; private TextView edit ; private Button button1 ; /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); df = new SimpleDateFormat(); this.edit = (TextView) this.findViewById(R.id.tv) ; this.button1 = (Button) this.findViewById(R.id.button1) ; this.button1.setOnClickListener(new OnClickListener(){ @Override public void onClick(View v) { // TODO Auto-generated method stub buildDateOrTimeDialog(MainActivity.this) ; } }) ; } private void buildDateOrTimeDialog(Context context) { df = new SimpleDateFormat("HH:mm:SS"); switch (444) { case DATE: date: new DatePickerDialog(context, listener, cal.get(Calendar.YEAR), cal.get(Calendar.MONTH), cal.get(Calendar.DAY_OF_MONTH) ).show(); break; case TIME: System.out.println("----------time---------------"); new TimePickerDialog(context, timeListen, cal.get(Calendar.HOUR_OF_DAY), cal.get(Calendar.MINUTE), true).show(); break; default: new DatePickerDialog(context, listener, cal.get(Calendar.YEAR), cal.get(Calendar.MONTH), cal.get(Calendar.DAY_OF_MONTH) ).show(); } } private DatePickerDialog.OnDateSetListener listener = new DatePickerDialog.OnDateSetListener() { // @Override public void onDateSet(DatePicker arg0, int arg1, int arg2, int arg3) { cal.set(Calendar.YEAR, arg1); cal.set(Calendar.MONTH, arg2); cal.set(Calendar.DAY_OF_MONTH, arg3); updateDate(); } }; private void updateDate() { edit.setText(df.format(cal.getTime())); } TimePickerDialog.OnTimeSetListener timeListen = new TimePickerDialog.OnTimeSetListener() { // 同DatePickerDialog控件 @Override public void onTimeSet(TimePicker view, int hourOfDay, int minute) { cal.set(Calendar.HOUR_OF_DAY, hourOfDay); cal.set(Calendar.MINUTE, minute); cal.set(Calendar.SECOND, cal.get(Calendar.SECOND)); updateTimes(); } }; // 更新页面TextView的方法 private void updateTimes() { edit.setText(df.format(cal.getTime())); } }
说明的:
其中 SimpleDateFormat df = new SimpleDateFormat("HH:mm:SS"); //定义获取时间的样子
switch (444) { case DATE: date: new DatePickerDialog(context, listener, cal.get(Calendar.YEAR), cal.get(Calendar.MONTH), cal.get(Calendar.DAY_OF_MONTH) ).show(); break; case TIME: System.out.println("----------time---------------"); new TimePickerDialog(context, timeListen, cal.get(Calendar.HOUR_OF_DAY), cal.get(Calendar.MINUTE), true).show(); break; default: new DatePickerDialog(context, listener, cal.get(Calendar.YEAR), cal.get(Calendar.MONTH), cal.get(Calendar.DAY_OF_MONTH) ).show(); }
定义的对话框中,我们要不要显示时间,你可以更改switch(DATA||TIME)来选择你要的样子。
如果你还是看不懂上面的代码,或者用起来有问题,那么你可以联系:qq 1019990976。
觉得是无图有真相的。
相关推荐
huha 2020-10-16
xfcyhades 2020-11-20
sgafdsg 2020-11-04
Michael 2020-11-03
fengyeezju 2020-10-14
ziyexiaoxiao 2020-10-14
业余架构师 2020-10-09
OuNuo0 2020-09-29
moses 2020-09-22
Angelia 2020-09-11
qinxu 2020-09-10
刘炳昭 2020-09-10
Nostalgiachild 2020-09-07
Nostalgiachild 2020-08-17
leavesC 2020-08-14
一青年 2020-08-13
AndroidAiStudy 2020-08-07
ydc0 2020-07-30
绿豆饼 2020-07-28