android之timePicker

来源:互联网 发布:批量注册淘宝小号软件 编辑:程序博客网 时间:2024/06/08 06:01


MainActivity.java

package com.fzq.timepicker;import com.fzq.timepicker.R.id;import android.R.integer;import android.app.Activity;import android.app.Dialog;import android.app.TimePickerDialog;import android.os.Bundle;import android.view.Menu;import android.view.MenuItem;import android.view.View;import android.view.View.OnClickListener;import android.widget.Button;import android.widget.TimePicker;public class MainActivity extends Activity {private Button timePicker;private static final int  Time_Pick_ID=1;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);timePicker=(Button)findViewById(R.id.timePicker);timePicker.setOnClickListener(new timePickerListener());}class timePickerListener implements OnClickListener{@Overridepublic void onClick(View v) {// TODO Auto-generated method stubshowDialog(Time_Pick_ID);}}TimePickerDialog.OnTimeSetListener onTimeSetListener=new  TimePickerDialog.OnTimeSetListener(){@Overridepublic void onTimeSet(TimePicker view, int hourOfDay, int minute) {// TODO Auto-generated method stubSystem.out.print("时间:"+hourOfDay+" :"+minute);}};protected Dialog onCreateDialog(int Id){switch (Id) {case Time_Pick_ID://false 是表示不以24小时方式显示时间return new TimePickerDialog(MainActivity.this, onTimeSetListener, 11,26, false);}return null;}}

Main.xml

<LinearLayout 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:orientation="vertical"    tools:context="${relativePackage}.${activityClass}" >    <TextView        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:text="@string/hello_world" />    <Button         android:id="@+id/timePicker"        android:layout_width="fill_parent"        android:layout_height="wrap_content"        android:text="@string/timePicker"/></LinearLayout>


string.xml

<?xml version="1.0" encoding="utf-8"?><resources>    <string name="app_name">01_timePicker</string>    <string name="hello_world">Hello world! TimePicker</string>    <string name="timePicker">开启timePicker</string></resources>