天天看點

pickerview實作底部彈出選擇時間

首先看一下效果是否是您需要的呢

pickerview實作底部彈出選擇時間

首先導入依賴

compile 'com.bigkoo:pickerview:2.0.7'      

上面放選擇的資料時,這裡使用的是Button,是以看情況,你可以使用Textview

xml布局

<Button
        android:layout_width="match_parent"
        android:layout_height="50dp"
        android:id="@+id/tvTime"
        android:textSize="16sp"
        android:textColor="#fff"
        android:text="選擇時間"
        />
           

MainActivity.java

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.StaggeredGridLayoutManager;
import android.text.Editable;
import android.text.TextWatcher;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import com.bigkoo.pickerview.TimePickerView;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


public class MainActivity extends AppCompatActivity {
    private RecyclerView recycle;
    private TimePickerView pvTime;
    private int year;
    private int month;
    private int day;
    private String time;
    private Button tvTime;
  


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.fragment3);
        recycle = (RecyclerView) findViewById(R.id.recycle);
        quxiao = (Button) findViewById(R.id.quxiao);
        baocun = (Button) findViewById(R.id.baocun);
        tvTime = (Button) findViewById(R.id.tvTime);
      
        timeInit();//設定時間選擇器函數
        tvTime.setText(time);
        pvTime.setOnTimeSelectListener(new TimePickerView.OnTimeSelectListener() {
            @Override
            public void onTimeSelect(Date date) {
                tvTime.setText(getTime(date));
                time = getTime(date);
                Toast.makeText(MainActivity.this,time, Toast.LENGTH_SHORT).show();
            }
        });
        //彈出時間選擇器
        tvTime.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                pvTime.show();
            }
        });
    }

    public static String getTime(Date date) {
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
        return format.format(date);
    }

    private void timeInit() {
        tvTime = (Button) findViewById(R.id.tvTime);
        //時間選擇器
        pvTime = new TimePickerView(this, TimePickerView.Type.YEAR_MONTH_DAY);
        pvTime.setTime(new Date());
        //  pvTime.setCyclic(false);
        pvTime.setCancelable(true);
        //顯示目前時間
        final Calendar c = Calendar.getInstance();
        year = c.get(Calendar.YEAR);
        month = c.get(Calendar.MONTH);
        day = c.get(Calendar.DAY_OF_MONTH);
        if (month > 9) {
            if (day < 10) {
                time = year + "-" + (month + 1) + "-" + "0" + day;
            } else {
                time = year + "-" + (month + 1) + "-" + day;
            }
        } else {
            if (day < 10) {
                time = year + "-" + "0" + (month + 1) + "-" + "0" + day;
            } else {
                time = year + "-" + "0" + (month + 1) + "-" + day;
            }
        }

        tvTime.setText(time);
        pvTime.setOnTimeSelectListener(new TimePickerView.OnTimeSelectListener() {
            @Override
            public void onTimeSelect(Date date) {
                tvTime.setText(getTime(date));

                time = getTime(date);


            }
        });
        //彈出時間選擇器
        tvTime.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                pvTime.show();
            }
        });
    }}
           

這樣就可以選擇了,很簡單吧,使用起來吧,哈哈哈