像datepicker一样,也有timepicker。
同样有timepickerdialog
所用到的方法还是一样,监听时间选择器的变化。
package com.example.alimjan.hello_world; import java.util.Calendar; /**
* Created by alimjan on 7/15/2017.
*/ import android.app.TimePickerDialog;
import android.app.TimePickerDialog.OnTimeSetListener;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.TimePicker;
import android.widget.TextView; public class class_5_1_2d extends AppCompatActivity implements
OnClickListener, OnTimeSetListener { private TextView tv_time; @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.code_5_1_2);
tv_time = (TextView) findViewById(R.id.tv_time);
findViewById(R.id.btn_time).setOnClickListener(this);
} @Override
public void onClick(View v) {
if (v.getId() == R.id.btn_time) {
Calendar calendar = Calendar.getInstance();
TimePickerDialog dialog = new TimePickerDialog(this, this,
calendar.get(Calendar.HOUR_OF_DAY), calendar.get(Calendar.MINUTE), true);
dialog.show();
}
} @Override
public void onTimeSet(TimePicker view, int hourOfDay, int minute) {
String desc = String.format("您选择的时间是%d时%d分", hourOfDay, minute);
tv_time.setText(desc);
} public static void startHome(Context mContext) {
Intent intent = new Intent(mContext, class_5_1_2d.class);
mContext.startActivity(intent);
} }
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:padding="10dp" > <Button
android:id="@+id/btn_time"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="请选择时间"
android:textColor="@color/black"
android:textSize="20sp" /> <TextView
android:id="@+id/tv_time"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textColor="@color/black"
android:textSize="17sp" /> </LinearLayout>