展会信息港展会大全

android之TimePicker
来源:互联网   发布日期:2016-01-06 19:18:54   浏览:2710次  

导读:TimePicker:时间选择器。这个和DatePicker的设置基本差不多,下面就不多说了,基本注释都在代码中。效果:程序开始画面点击按钮后就会显示时间选择器对话框在对话框上...

TimePicker:时间选择器。这个和DatePicker的设置基本差不多,下面就不多说了,基本注释都在代码中。

效果:

程序开始画面

点击按钮后就会显示时间选择器对话框

在对话框上面设置时间:

当用户设置好时间后,将时间显示到TextView上面:

layout中的xml文件:

<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/textview"

android:layout_width="fill_parent"

android:layout_height="wrap_content"

android:text="设置的时间:"

/>

<Button

android:id="@+id/timepickerBut"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:text="改变时间"

/>

</LinearLayout>

Activaty.java文件:

package com.cheng.timepickerproject;

import java.util.Calendar;

import android.os.Bundle;

import android.app.Activity;

import android.app.Dialog;

import android.app.TimePickerDialog;

import android.app.TimePickerDialog.OnTimeSetListener;

import android.view.Menu;

import android.view.View;

import android.widget.Button;

import android.widget.TextView;

import android.widget.TimePicker;

public class TimePickerActivity extends Activity {

private Button mButton;//定义按钮

private TextView mTextView;//定义文本框

private int mHour;//存放时间的小时

private int mMinute;//存放时间的分钟

private static final int TIMEPICKER_DIALOG_1 = 0;//设置Dialog的id

//时间监听器,当用户改变时间的时候将会调用它

OnTimeSetListener otsl = new OnTimeSetListener() {

@Override

public void onTimeSet(TimePicker view, int hourOfDay, int minute) {

// TODO Auto-generated method stub

//设置小时、分钟,并把时间显示在文本框上

mHour = hourOfDay;

mMinute = minute;

mTextView.setText("当前时间:" + mHour +":"+mMinute);

}

};

//从写onCreateDialog,创建一个对话框

protected Dialog onCreateDialog(int id) {

switch (id) {

case TIMEPICKER_DIALOG_1:

//新建一个TimePickerDialog

return new TimePickerDialog(TimePickerActivity.this, otsl, mHour, mMinute, true);

default:

break;

}

return null;

}

@Override

protected void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

super.setContentView(R.layout.main);

//给按钮和文本框赋值

mButton = (Button)findViewById(R.id.timepickerBut);

mTextView = (TextView)findViewById(R.id.textview);

//点击监听器

View.OnClickListener ocl = new View.OnClickListener() {

@Override

public void onClick(View v) {

// TODO Auto-generated method stub

//获得当前的时间的小时和分钟

Calendar calendar = Calendar.getInstance();

mHour = calendar.get(Calendar.HOUR);

mMinute = calendar.get(Calendar.MINUTE);

//显示对话框

showDialog(TIMEPICKER_DIALOG_1);

}

};

//将点击时间绑定到按钮上面

mButton.setOnClickListener(ocl);

}

@Override

public boolean onCreateOptionsMenu(Menu menu) {

// Inflate the menu; this adds items to the action bar if it is present.

getMenuInflater().inflate(R.menu.main, menu);

return true;

}

}

赞助本站

人工智能实验室

相关热词: android开发 教程

AiLab云推荐
展开

热门栏目HotCates

Copyright © 2010-2024 AiLab Team. 人工智能实验室 版权所有    关于我们 | 联系我们 | 广告服务 | 公司动态 | 免责声明 | 隐私条款 | 工作机会 | 展会港