像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 9 import android.app.TimePickerDialog; 10 import android.app.TimePickerDialog.OnTimeSetListener; 11 import android.content.Context; 12 import android.content.Intent; 13 import android.os.Bundle; 14 import android.support.v7.app.AppCompatActivity; 15 import android.view.View; 16 import android.view.View.OnClickListener; 17 import android.widget.TimePicker; 18 import android.widget.TextView; 19 20 public class class_5_1_2d extends AppCompatActivity implements 21 OnClickListener, OnTimeSetListener { 22 23 private TextView tv_time; 24 25 @Override 26 protected void onCreate(Bundle savedInstanceState) { 27 super.onCreate(savedInstanceState); 28 setContentView(R.layout.code_5_1_2); 29 tv_time = (TextView) findViewById(R.id.tv_time); 30 findViewById(R.id.btn_time).setOnClickListener(this); 31 } 32 33 @Override 34 public void onClick(View v) { 35 if (v.getId() == R.id.btn_time) { 36 Calendar calendar = Calendar.getInstance(); 37 TimePickerDialog dialog = new TimePickerDialog(this, this, 38 calendar.get(Calendar.HOUR_OF_DAY), calendar.get(Calendar.MINUTE), true); 39 dialog.show(); 40 } 41 } 42 43 @Override 44 public void onTimeSet(TimePicker view, int hourOfDay, int minute) { 45 String desc = String.format("您选择的时间是%d时%d分", hourOfDay, minute); 46 tv_time.setText(desc); 47 } 48 49 public static void startHome(Context mContext) { 50 Intent intent = new Intent(mContext, class_5_1_2d.class); 51 mContext.startActivity(intent); 52 } 53 54 }
1 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 2 android:layout_width="match_parent" 3 android:layout_height="match_parent" 4 android:orientation="vertical" 5 android:padding="10dp" > 6 7 <Button 8 android:id="@+id/btn_time" 9 android:layout_width="match_parent" 10 android:layout_height="wrap_content" 11 android:text="请选择时间" 12 android:textColor="@color/black" 13 android:textSize="20sp" /> 14 15 <TextView 16 android:id="@+id/tv_time" 17 android:layout_width="match_parent" 18 android:layout_height="wrap_content" 19 android:textColor="@color/black" 20 android:textSize="17sp" /> 21 22 </LinearLayout>
时间: 2024-10-25 20:33:23