android控件开发之TimePicker和DatePicker

来源:互联网 发布:知乎越来越恶心 编辑:程序博客网 时间:2024/05/01 12:50

android控件开发之TimerPicker和DatePicker




java代码:

package com.example.timepicker;


import android.os.Bundle;
import android.app.Activity;
import android.app.DatePickerDialog;
import android.app.Dialog;
import android.app.TimePickerDialog;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.DatePicker;
import android.widget.TimePicker;


public class MainActivity extends Activity {


private final int TIMER = 1;
private final int DATE = 2;

private Button showTimerButton = null;
private Button showDateButton = null;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);

showTimerButton = (Button)findViewById(R.id.showTimePicker);

showTimerButton.setOnClickListener(new showTimerDialogListener());

showDateButton = (Button)findViewById(R.id.showDatePicker);

showDateButton.setOnClickListener(new showDateDialogListener());

}

//Date button的监听器
class showDateDialogListener implements OnClickListener{


@Override
public void onClick(View v) {
// TODO Auto-generated method stub
//显示Date Diaolog
showDialog(DATE);
}

}



//Timer button 监听器
class showTimerDialogListener implements OnClickListener{


@Override
public void onClick(View v) {
// TODO Auto-generated method stub

//显示ID为TIMER的dialog
showDialog(TIMER);

//TimePickerDialog timerDialog = new TimePickerDialog(this, onTimeSetListener, 22, 54, true);
//timerDialog.show();
}

}




//此方法由showDialog()调用并且传入Dialog ID
protected Dialog onCreateDialog(int id){
Dialog dialog = null;
switch(id){
//当ID为TIMER时,创建一个TimePickerDialog对象并且返回
case TIMER:
dialog =  new TimePickerDialog(this, onTimeSetListener, 22, 54, true);
break;
//当ID为DATE时,创建一个DatePickerDialog对象并且返回
case DATE:
dialog = new DatePickerDialog(this, onDateSetListener, 2014, 12, 10);
break; 
}
return dialog;
}




main.xml文件如下:


<RelativeLayout 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:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context=".MainActivity" >


    <TextView
        android:id="@+id/myText"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/hello_world" />
    
    <Button 
        android:id="@+id/showTimePicker"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_below="@id/myText"
        android:text="@string/show_timer"/>
    
     <Button 
         android:id="@+id/showDatePicker"
         android:layout_height="wrap_content"
         android:layout_width="fill_parent"
         android:layout_below="@id/showTimePicker"
         android:text="@string/show_date"/>


</RelativeLayout>


Strings.xml代码如下:
<?xml version="1.0" encoding="utf-8"?>
<resources>


    <string name="app_name">PickerDialog</string>
    <string name="action_settings">Settings</string>
    <string name="hello_world">Hello world!</string>
<string name="show_timer">Show Time Dialog</string>
<string name="show_date">Show Date Dialog</string>
</resources>


显示效果如下:

主界面:



点击Show Timer Dialog 按钮出现如下:



点击Show Date Dialog出现如下:



0 0
原创粉丝点击