android控件开发之TimePicker和DatePicker
android控件开发之TimerPicker和DatePicker
java代码:
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文件如下:
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>
<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>
显示效果如下:
郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。