android控制項開發之TimePicker和DatePicker

來源:互聯網
上載者:User

標籤:android控制項開發   dialog   picker   timerpicker   datepicker   

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出現如下:


android控制項開發之TimePicker和DatePicker

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.