本文主要是介绍TimePickerDialog控件的使用,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
这个例子主要就是:
显示当前的系统时间,并且可以改变自己想要的时间,显示在TextView中
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<TextView
android:id="@+id/tvTimePicker"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="30sp"
android:textColor="#00aa00"
/>
<Button
android:id="@+id/showTime"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="@string/showTime"
/>
</LinearLayout>
package com.example.datepickerdialogtest;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import android.app.Activity;
import android.app.Dialog;
import android.app.TimePickerDialog;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;
import android.widget.TimePicker;
public class TimePickerActivity extends Activity {
private Button timePicker;
private TextView timeShow;
private static final int TIME_PICKER_ID=1;
private Calendar cal=Calendar.getInstance();
private SimpleDateFormat sdf=null;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
timePicker=(Button)findViewById(R.id.showTime);
timeShow=(TextView)findViewById(R.id.showTime);
timePicker.setOnClickListener(new TimePickerButtonListener());
}
class TimePickerButtonListener implements OnClickListener{
@Override
public void onClick(View view) {
showDialog(TIME_PICKER_ID);
}
}
@Override
@Deprecated
protected Dialog onCreateDialog(int id) {
switch(id){
case TIME_PICKER_ID:
return new TimePickerDialog(this, mTimeSetListener, cal.get(Calendar.HOUR),cal.get(Calendar.MINUTE), true);
}
return null;
}
TimePickerDialog.OnTimeSetListener mTimeSetListener=new TimePickerDialog.OnTimeSetListener(){
@Override
public void onTimeSet(TimePicker view, int hourOfDay, int minute) {
cal.set(Calendar.HOUR, hourOfDay);
cal.set(Calendar.MINUTE, minute);
updateTime();
}
};
public void updateTime(){
sdf=new SimpleDateFormat("HH:mm:ss");
timeShow.setText(sdf.format(cal.getTime()));
}
}
这篇关于TimePickerDialog控件的使用的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!