Android學習筆記---使用TimePickerDialog打造時間選擇器

CrackgmKey發表於2017-11-29


今天看到一個新的控制元件,覺得不錯 日後肯定會有大用,於是先記錄下來

一個時間選擇器,上張圖吧






那具體是怎麼實現的呢? 

首先在bulie.gradle中依賴compile 'com.jzxiang.pickerview:TimePickerDialog:0.9.2'

我們下來看程式碼

先來看下佈局程式碼



<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools" android:id="@+id/activity_main"
    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"
    android:orientation="vertical"
    tools:context="baidutu.ht.sks.timepicde.MainActivity">


    <Button
        android:id="@+id/button1"
        android:text="顯示年月日時分"
        android:layout_gravity="center"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />

    <Button
        android:id="@+id/button2"
        android:text="顯示年月日"
        android:layout_gravity="center"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />

    <Button
        android:id="@+id/button3"
        android:text="顯示時分"
        android:layout_gravity="center"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />

    <Button
        android:id="@+id/button4"
        android:text="顯示月日時分"
        android:layout_gravity="center"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />

    <Button
        android:id="@+id/button5"
        android:text="顯示年月"
        android:layout_gravity="center"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />


</LinearLayout>


Mainactivity.java

package baidutu.ht.sks.timepicde;

import android.net.Uri;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;

import com.google.android.gms.appindexing.Action;
import com.google.android.gms.appindexing.AppIndex;
import com.google.android.gms.appindexing.Thing;
import com.google.android.gms.common.api.GoogleApiClient;
import com.jzxiang.pickerview.TimePickerDialog;
import com.jzxiang.pickerview.data.Type;
import com.jzxiang.pickerview.listener.OnDateSetListener;

import java.text.SimpleDateFormat;
import java.util.Date;

import static android.icu.lang.UCharacter.GraphemeClusterBreak.T;

public class MainActivity extends AppCompatActivity implements OnDateSetListener, View.OnClickListener {
    private Button button1, button2, button3, button4, button5;
    private SimpleDateFormat sf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
    private TimePickerDialog pickerdialog;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        initview();
    }

    /**
     * 設定樣式
     */
    private void intiTimeDialog(Type claa) {
        pickerdialog = new TimePickerDialog.Builder()
                //設定型別
                .setType(claa)
                //設定選擇時間監聽回撥
                .setCallBack(this)
                //設定標題
                .setTitleStringId("請選擇時間")
                //設定時間
                .setSelectorMillseconds(System.currentTimeMillis())
                //設定顏色
                .setThemeColor(getResources().getColor(R.color.colorAccent))
                //設定 字型大小
                .setWheelItemTextSize(15)
                //完畢
                .build();
        pickerdialog.show(getSupportFragmentManager(),"abc");
    }

    /*
    初始化元件
     */
    private void initview() {
        button1 = (Button) findViewById(R.id.button1);
        button2 = (Button) findViewById(R.id.button2);
        button3 = (Button) findViewById(R.id.button3);
        button4 = (Button) findViewById(R.id.button4);
        button5 = (Button) findViewById(R.id.button5);
        button5.setOnClickListener(this);
        button4.setOnClickListener(this);
        button3.setOnClickListener(this);
        button2.setOnClickListener(this);
        button1.setOnClickListener(this);
    }

    @Override
    public void onDateSet(TimePickerDialog timePickerView, long millseconds) {
        Toast.makeText(this, "你選擇的時間:"+getDateToString(millseconds), Toast.LENGTH_SHORT).show();
    }

    //Android時間選擇器,支援年月日時分,年月日,年月,月日時分,時分格式,可以設定最小時間(精確到分)
    public String getDateToString(long time) {
        Date d = new Date(time);
        return sf.format(d);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.button1:
                intiTimeDialog(Type.ALL);
                break;
            case R.id.button2:
                intiTimeDialog(Type.YEAR_MONTH_DAY);
                break;
            case R.id.button3:
                intiTimeDialog(Type.HOURS_MINS);
                break;
            case R.id.button4:
                intiTimeDialog(Type.MONTH_DAY_HOUR_MIN);
                break;
            case R.id.button5:
                intiTimeDialog(Type.YEAR_MONTH);
                break;
        }
    }
}

相關文章