編輯:Android開發實例
利用Android應用框架提供的DatePicker(日期選擇器)和TimePicker(時間選擇器),實現日期時間選擇器。
Dialog的Content布局文件(date_time_dialog.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="fill_parent"
- android:layout_height="wrap_content"
- android:singleLine="true"
- android:text="請選擇日期"
- android:textColor="#FFFFFF"
- android:textSize="16sp" />
- <DatePicker
- android:id="@+id/date_picker"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:layout_marginTop="5dip" />
- <TextView
- android:layout_width="fill_parent"
- android:layout_height="wrap_content"
- android:layout_marginTop="10dip"
- android:singleLine="true"
- android:text="請選擇時間"
- android:textColor="#FFFFFF"
- android:textSize="16sp" />
- <TimePicker
- android:id="@+id/time_picker"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:layout_marginTop="5dip" />
- </LinearLayout>
代碼中的實現:
- package com.easipass.test;
- 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 DateTimeActivity extends Activity implements View.OnTouchListener {
- private EditText etStartTime;
- private EditText etEndTime;
- @Override
- public void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.main);
- 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;
- }
- }
運行後的效果圖:
點擊EditTet之後
點擊確定之後
轉自:http://blog.csdn.net/android_ls/article/details/8644067
本文實例講述了Android編程使用Fragment界面向下跳轉並一級級返回的實現方法。分享給大家供大家參考,具體如下: 1.首先貼上項目結構圖: 2.先添加一
本文實例講述了Android編程實現QQ表情的發送和接收。分享給大家供大家參考,具體如下: 在自己做一個聊天應用練習的時候,需要用到表情,於是就想著模仿一下QQ表
可以顯示在的Android任務,通過加載進度條的進展。進度條有兩種形狀。加載欄和加載微調(spinner)。在本章中,我們將討論微調(spinner)。Spinner 用
登錄應用程序的屏幕,詢問憑據登錄到一些特定的應用。可能需要登錄到Facebook,微博等本章介紹了,如何創建一個登錄界面,以及如何管理安全問題和錯誤嘗試。首先,必須定義兩