Android 开发笔记___时间选择器---timePicker

【Android 开发笔记___时间选择器---timePicker】盛年不重来,一日难再晨,及时当勉励,岁月不待人。这篇文章主要讲述Android 开发笔记___时间选择器---timePicker相关的知识,希望能为你提供帮助。
像datepicker一样,也有timepicker。
同样有timepickerdialog
所用到的方法还是一样,监听时间选择器的变化。

1 package com.example.alimjan.hello_world; 2 3 import java.util.Calendar; 4 5 /** 6* Created by alimjan on 7/15/2017. 7*/ 8 9import android.app.TimePickerDialog; 10import android.app.TimePickerDialog.OnTimeSetListener; 11 import android.content.Context; 12 import android.content.Intent; 13 import android.os.Bundle; 14import android.support.v7.app.AppCompatActivity; 15import android.view.View; 16import android.view.View.OnClickListener; 17import android.widget.TimePicker; 18import android.widget.TextView; 19 20 public class class_5_1_2d extends AppCompatActivity implements 21OnClickListener, OnTimeSetListener { 22 23private TextView tv_time; 24 25@Override 26protected void onCreate(Bundle savedInstanceState) { 27super.onCreate(savedInstanceState); 28setContentView(R.layout.code_5_1_2); 29tv_time = (TextView) findViewById(R.id.tv_time); 30findViewById(R.id.btn_time).setOnClickListener(this); 31} 32 33@Override 34public void onClick(View v) { 35if (v.getId() == R.id.btn_time) { 36Calendar calendar = Calendar.getInstance(); 37TimePickerDialog dialog = new TimePickerDialog(this, this, 38calendar.get(Calendar.HOUR_OF_DAY), calendar.get(Calendar.MINUTE), true); 39dialog.show(); 40} 41} 42 43@Override 44public void onTimeSet(TimePicker view, int hourOfDay, int minute) { 45String desc = String.format("您选择的时间是%d时%d分", hourOfDay, minute); 46tv_time.setText(desc); 47} 48 49public static void startHome(Context mContext) { 50Intent intent = new Intent(mContext, class_5_1_2d.class); 51mContext.startActivity(intent); 52} 53 54 }

1 < LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 2android:layout_width="match_parent" 3android:layout_height="match_parent" 4android:orientation="vertical" 5android:padding="10dp" > 6 7< Button 8android:id="@+id/btn_time" 9android:layout_width="match_parent" 10android:layout_height="wrap_content" 11android:text="请选择时间" 12android:textColor="@color/black" 13android:textSize="20sp" /> 14 15< TextView 16android:id="@+id/tv_time" 17android:layout_width="match_parent" 18android:layout_height="wrap_content" 19android:textColor="@color/black" 20android:textSize="17sp" /> 21 22 < /LinearLayout>

 

    推荐阅读