今天主要完成记录页面的完善,添加了时间选择对话框
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@color/white">
<DatePicker
android:id="@+id/dialog_time_dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/please_input_time"
android:padding="10dp"/>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:padding="10dp">
<EditText
android:id="@+id/dialog_time_et_hour"
android:layout_width="60dp"
android:layout_height="wrap_content"
android:inputType="number"
android:maxLength="2"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text=":"
android:textSize="20dp"
android:textStyle="bold"/>
<EditText
android:id="@+id/dialog_time_et_minute"
android:layout_width="60dp"
android:layout_height="wrap_content"
android:inputType="number"
android:maxLength="2"/>
</LinearLayout>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="right"
android:orientation="horizontal">
<Button
android:id="@+id/dialog_time_btn_cancel"
android:layout_width="80dp"
android:layout_height="wrap_content"
android:text="@string/cancel"
android:textColor="@color/green_006400"
android:background="@null"
android:layout_marginRight="20dp"/>
<Button
android:id="@+id/dialog_time_btn_ensure"
android:layout_width="80dp"
android:layout_height="wrap_content"
android:text="@string/ensure"
android:textColor="@color/green_006400"
android:background="@null"
android:layout_marginRight="20dp"/>
</LinearLayout>
</LinearLayout>
package com.example.myapplication.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.example.myapplication.R;
/*
* 记录页面弹出时间的对话框*/
public class SelectTimeDialog extends Dialog implements View.OnClickListener {
EditText hourEt, minuteEt;
DatePicker datePicker;
Button ensureBtn, cancelBtn;
public interface OnEnsureListener{
public void OnEnsure(String time,int year,int month,int day);
}
OnEnsureListener onEnsureListener;
public void setOnEnsureListener(OnEnsureListener onEnsureListener) {
this.onEnsureListener = onEnsureListener;
}
public SelectTimeDialog(@NonNull Context context) {
super(context);
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.dialog_time);
hourEt = findViewById(R.id.dialog_time_et_hour);
minuteEt = findViewById(R.id.dialog_time_et_minute);
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 v) {
if (v.getId() == R.id.dialog_time_btn_cancel) {
cancel();
} else if (v.getId() == R.id.dialog_time_btn_ensure) {
int year = datePicker.getYear();
int month = datePicker.getMonth()+1;
int dayOfMonth = datePicker.getDayOfMonth();
String monthStr=String.valueOf(month);
if(month<10){
monthStr="0"+month;
}
String dayStr=String.valueOf(dayOfMonth);
if(dayOfMonth<10){
dayStr="0"+dayOfMonth;
}
//获取输入的小时和分钟
String hourStr = hourEt.getText().toString();
String minuteStr = minuteEt.getText().toString();
int hour=0;
if(!TextUtils.isEmpty(hourStr)){
hour=Integer.parseInt(hourStr);
hour=hour%24;
}
int minute=0;
if(!TextUtils.isEmpty(minuteStr)){
minute=Integer.parseInt(minuteStr);
minute=minute%60;
}
hourStr=String.valueOf(hour);
minuteStr=String.valueOf(minute);
if(hour<10){
hourStr="0"+hour;
}
if(minute<10){
minuteStr="0"+minute;
}
String timeFormat=year+"年"+monthStr+"月"+dayStr+"日 "+hourStr+":"+minuteStr;
if(onEnsureListener!=null){
onEnsureListener.OnEnsure(timeFormat,year,month,dayOfMonth);
}
cancel();
}
}
// //隐藏dataPicker的头布局
// private void hideDatePickerHeader() {
// ViewGroup rootView = (ViewGroup) datePicker.getChildAt(0);
// if (rootView == null) {
// return;
// }
// View headView = rootView.getChildAt(0);
// if (headView == null) {
// return;
// }
// int headerID = getContext().getResources().getIdentifier("day_picker_selector_layout", "id", "android");
// if(headerID==headView.getId()){
// headView.setVisibility(View.GONE);
// ViewGroup.LayoutParams layoutParamsRoot=rootView.getLayoutParams();
// layoutParamsRoot.width=ViewGroup.LayoutParams.WRAP_CONTENT;
// rootView.setLayoutParams(layoutParamsRoot);
//
// ViewGroup childAt = (ViewGroup) rootView.getChildAt(1);
// }
// }
}
标签:--,public,记账,dialog,time,andorid,import,android,id
From: https://www.cnblogs.com/gyg1222/p/18022224