6.17 4

package com.zhen.accountbook.utils;

import android.app.Dialog;
import android.content.Context;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.DatePicker;
import android.widget.EditText;
import androidx.annotation.NonNull;
import com.zhen.accountbook.R;

//在记录页面弹出时间对话框
public class SelectTimeDialog extends Dialog implements View.OnClickListener {
    EditText hourEt, minEt;
    DatePicker datePicker;
    Button ensureBtn, cancelBtn;

    public interface OnEnsureListener {
        public void onEnsure(String time, int year, int month, int day);
    }

    public void setOnEnsureListener(OnEnsureListener onEnsureListener) {
        this.onEnsureListener = onEnsureListener;
    }

    OnEnsureListener onEnsureListener;

    public SelectTimeDialog(@NonNull Context context) {
        super(context);
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.dialog_calendar);
        hourEt = findViewById(R.id.dialog_time_et_hour);
        minEt = findViewById(R.id.dialog_time_et_min);
        datePicker = findViewById(R.id.dialog_time_dp);
        ensureBtn = findViewById(R.id.dialog_time_btn_ensure);
        cancelBtn = findViewById(R.id.dialog_time_btn_cancel);
        ensureBtn.setOnClickListener(this);
        cancelBtn.setOnClickListener(this);

    }

    @Override
    public void onClick(View view) {
        if (view.getId() == R.id.dialog_time_btn_ensure) {
            if (onEnsureListener != null) {
                int year = datePicker.getYear();
                int month = datePicker.getMonth() + 1;
                int day = datePicker.getDayOfMonth();
                String monthStr = String.valueOf(month);
                if (month < 10) {
                    monthStr = "0" + month;
                }
                String dayStr = String.valueOf(day);
                if (day < 10) {
                    dayStr = "0" + day;
                }
                String hourStr=hourEt.getText().toString();
                String minStr=minEt.getText().toString();
                int hour=0;
                if (!TextUtils.isEmpty(hourStr)) {
                    hour=Integer.parseInt(hourStr);
                    hour=hour%24;
                }
                int min=0;
                if (!TextUtils.isEmpty(minStr)) {
                    min=Integer.parseInt(minStr);
                    min=min%60;
                }
                hourStr=String.valueOf(hour);
                minStr=String.valueOf(min);
                if (hour<10) {
                    hourStr="0"+hourStr;
                }
                if (min<10) {
                    minStr="0"+minStr;
                }
                String time=year+"-"+monthStr+"-"+dayStr+" "+hourStr+":"+minStr;
                onEnsureListener.onEnsure(time,year,month,day);
            }
        } else if (view.getId() == R.id.dialog_time_btn_cancel) {
            cancel();
        }
    }
    //隐藏 DatePicker头布局
//    private void hideDatePickerHeader(){
//        ViewGroup rootView= (ViewGroup) datePicker.getChildAt(0);
//   if (rootView==null){
//       return ;
//   }View headView=rootView.getChildAt(0);
//   if(headView==null){
//       return ;
//   }
//    }
}

 

posted @ 2024-06-17 23:47  七安。  阅读(4)  评论(0编辑  收藏  举报