2014-08-02 16:39:00|?次阅读|上传:huigezrx【已有?条评论】发表评论
关键词:Java, Android, 界面设计|来源:唯设编程网
在Android开发的应用程序中,通常都会有时间和日期选择的需求,Android开发之DatePicker和TimePicker的用法介绍了日期选择控件DatePicker和时间选择控件TimePicker的基本用法,但是那样还不够,因为DatePicker和TimePicker控件比较复杂,如果我们直接将其布局在对话框中,会使我们的对话框看起来很不协调,通常我们不会预先将DatePicker和TimePicker放在窗口中,而是展示一个输入框控件,用户点击输入框控件的时候弹出对话框让用户选择时间和日期。
本文介绍的例子包括两个Activity,一个主Activity用来显示开始时间和结束时间的输入框,如下图所示:
当用户在输入框点击的时候,会弹出时间和日期选择对话框:
主对话框对应的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" android:padding="10dip" > <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="开始时间" android:id="@+id/textView" /> <EditText android:id="@+id/et_start_time" android:layout_width="match_parent" android:layout_height="wrap_content" /> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="结束时间" android:id="@+id/textView2" /> <EditText android:id="@+id/et_end_time" android:layout_width="match_parent" android:layout_height="wrap_content" /> </LinearLayout>
这段代码比较简单,直接使用LinearLayout将两个TextView和EditText放在Activity中,对应的Activity类实现代码如下:
package com.example.myapplication2.app; import java.util.Calendar; import android.app.Activity; import android.app.AlertDialog; import android.app.Dialog; import android.content.DialogInterface; import android.os.Bundle; import android.text.InputType; import android.view.MotionEvent; import android.view.View; import android.widget.DatePicker; import android.widget.EditText; import android.widget.TimePicker; /** * 功能描述:实现日期时间选择器 * * @author android_ls */ public class MainActivity extends Activity implements View.OnTouchListener { private EditText etStartTime; private EditText etEndTime; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main_dialog); etStartTime = (EditText) this.findViewById(R.id.et_start_time); etEndTime = (EditText) this.findViewById(R.id.et_end_time); etStartTime.setOnTouchListener(this); etEndTime.setOnTouchListener(this); } @Override public boolean onTouch(View v, MotionEvent event) { if (event.getAction() == MotionEvent.ACTION_DOWN) { AlertDialog.Builder builder = new AlertDialog.Builder(this); View view = View.inflate(this, R.layout.date_time_dialog, null); final DatePicker datePicker = (DatePicker) view.findViewById(R.id.date_picker); final TimePicker timePicker = (android.widget.TimePicker) view.findViewById(R.id.time_picker); builder.setView(view); Calendar cal = Calendar.getInstance(); cal.setTimeInMillis(System.currentTimeMillis()); datePicker.init(cal.get(Calendar.YEAR), cal.get(Calendar.MONTH), cal.get(Calendar.DAY_OF_MONTH), null); timePicker.setIs24HourView(true); timePicker.setCurrentHour(cal.get(Calendar.HOUR_OF_DAY)); timePicker.setCurrentMinute(Calendar.MINUTE); if (v.getId() == R.id.et_start_time) { final int inType = etStartTime.getInputType(); etStartTime.setInputType(InputType.TYPE_NULL); etStartTime.onTouchEvent(event); etStartTime.setInputType(inType); etStartTime.setSelection(etStartTime.getText().length()); builder.setTitle("选取起始时间"); builder.setPositiveButton("确 定", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { StringBuffer sb = new StringBuffer(); sb.append(String.format("%d-%02d-%02d", datePicker.getYear(), datePicker.getMonth() + 1, datePicker.getDayOfMonth())); sb.append(" "); sb.append(timePicker.getCurrentHour()) .append(":").append(timePicker.getCurrentMinute()); etStartTime.setText(sb); etEndTime.requestFocus(); dialog.cancel(); } }); } else if (v.getId() == R.id.et_end_time) { int inType = etEndTime.getInputType(); etEndTime.setInputType(InputType.TYPE_NULL); etEndTime.onTouchEvent(event); etEndTime.setInputType(inType); etEndTime.setSelection(etEndTime.getText().length()); builder.setTitle("选取结束时间"); builder.setPositiveButton("确 定", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { StringBuffer sb = new StringBuffer(); sb.append(String.format("%d-%02d-%02d", datePicker.getYear(), datePicker.getMonth() + 1, datePicker.getDayOfMonth())); sb.append(" "); sb.append(timePicker.getCurrentHour()) .append(":").append(timePicker.getCurrentMinute()); etEndTime.setText(sb); dialog.cancel(); } }); } Dialog dialog = builder.create(); dialog.show(); } return true; } }