温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

datepicterDialog和timepicterDialog取消按钮

发布时间:2020-08-06 11:39:43 来源:网络 阅读:872 作者:lg491733638 栏目:移动开发

这是日期弹出框的第一种弹出方式:分开弹出datepicterdialog和timepicterdialog

 

final DatePickerDialog datePickerDialog = new DatePickerDialog(
       TableChart.this, null, mYear, mMonth,
       mDay);
     datePickerDialog.setButton(DialogInterface.BUTTON_POSITIVE,
       "完成", new DialogInterface.OnClickListener() {
        @Override
        public void onClick(DialogInterface arg0,
          int arg1) {
         DateTimeUtils.showDatePositive(datePickerDialog,starttime);
        }
       });
     datePickerDialog.setButton(DialogInterface.BUTTON_NEGATIVE,
       "取消", new DialogInterface.OnClickListener() {
        @Override
        public void onClick(DialogInterface arg0,
          int arg1) {
         DateTimeUtils.showDateNegative(starttime);
        }
       });
     datePickerDialog.setCanceledOnTouchOutside(false);
     datePickerDialog.show();
 
public static void showDatePositive(DatePickerDialog datePickerDialog,
   EditText yearEdit) {
  DatePicker datePicker = datePickerDialog.getDatePicker();//获取DatePicter对象
  int year = datePicker.getYear();
  int month = datePicker.getMonth() + 1;
  int day = datePicker.getDayOfMonth();
  yearEdit.setText(year+"-"month+"-"+day);
  yearEdit.clearFocus();
  }
public static void showDateNegative(EditText yearEdit) {
  yearEdit.clearFocus();
  }
由于timepicterDialog无法获取到timepicter,所以重写:MyTimePickerDialog.java
package com.rts.dzdc.utils;
import com.rts.dzdc.activity.R;
import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.DialogInterface.OnClickListener;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.TimePicker;
import android.widget.TimePicker.OnTimeChangedListener;
/**
* 基本上把源码套用了一下
* @author Lee
*
*/
public class MyTimePickerDialog extends AlertDialog
        implements OnClickListener, OnTimeChangedListener {
public interface OnTimeSetListener {
        void onTimeSet(TimePicker view, int hourOfDay, int minute);
    }
    private static final String HOUR = "hour";
    private static final String MINUTE = "minute";
    private static final String IS_24_HOUR = "is24hour";
    private final TimePicker mTimePicker;
    private final OnTimeSetListener mCallback;
    int mInitialHourOfDay;
    int mInitialMinute;
    boolean mIs24HourView;
  
    public MyTimePickerDialog(Context context,
            OnTimeSetListener callBack,
            int hourOfDay, int minute, boolean is24HourView) {
        this(context, 0, callBack, hourOfDay, minute, is24HourView);
    }
  
    public MyTimePickerDialog(Context context,
            int theme,
            OnTimeSetListener callBack,
            int hourOfDay, int minute, boolean is24HourView) {
        super(context, theme);
        mCallback = callBack;
        mInitialHourOfDay = hourOfDay;
        mInitialMinute = minute;
        mIs24HourView = is24HourView;
        setIcon(0);
        setTitle("设置时间");
        Context themeContext = getContext();
        setButton(BUTTON_POSITIVE, "完成", this);
        LayoutInflater inflater =
                (LayoutInflater) themeContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        View view = inflater.inflate(R.layout.time_picker_dialog, null);
        setView(view);
        mTimePicker = (TimePicker) view.findViewById(R.id.timePicker);
        // initialize state
        mTimePicker.setIs24HourView(mIs24HourView);
        mTimePicker.setCurrentHour(mInitialHourOfDay);
        mTimePicker.setCurrentMinute(mInitialMinute);
        mTimePicker.setOnTimeChangedListener(this);
    }
    public void onClick(DialogInterface dialog, int which) {
        tryNotifyTimeSet();
    }
    public void updateTime(int hourOfDay, int minutOfHour) {
        mTimePicker.setCurrentHour(hourOfDay);
        mTimePicker.setCurrentMinute(minutOfHour);
    }
    public void onTimeChanged(TimePicker view, int hourOfDay, int minute) {
        /* do nothing */
    }
    private void tryNotifyTimeSet() {
        if (mCallback != null) {
            mTimePicker.clearFocus();
            mCallback.onTimeSet(mTimePicker, mTimePicker.getCurrentHour(),
                    mTimePicker.getCurrentMinute());
        }
    }
    @Override
    protected void onStop() {
        tryNotifyTimeSet();
        super.onStop();
    }
    @Override
    public Bundle onSaveInstanceState() {
        Bundle state = super.onSaveInstanceState();
        state.putInt(HOUR, mTimePicker.getCurrentHour());
        state.putInt(MINUTE, mTimePicker.getCurrentMinute());
        state.putBoolean(IS_24_HOUR, mTimePicker.is24HourView());
        return state;
    }
    @Override
    public void onRestoreInstanceState(Bundle savedInstanceState) {
        super.onRestoreInstanceState(savedInstanceState);
        int hour = savedInstanceState.getInt(HOUR);
        int minute = savedInstanceState.getInt(MINUTE);
        mTimePicker.setIs24HourView(savedInstanceState.getBoolean(IS_24_HOUR));
        mTimePicker.setCurrentHour(hour);
        mTimePicker.setCurrentMinute(minute);
    }
   
    /**
     * 自己加的获取TimePicker方法
     * @return
     */
    public TimePicker getTimePicker()
    {
     return mTimePicker;
    }
}
使用方法:
final MyTimePickerDialog timePickerDialog = new MyTimePickerDialog(
         TableSlideZaiHaiActivity.this, null,
         mhour, mmintue, true);
       timePickerDialog.setButton(DialogInterface.BUTTON_POSITIVE,
         "完成", new DialogInterface.OnClickListener() {
          @Override
          public void onClick(DialogInterface arg0,
            int arg1) {
           DateTimeUtils.showTimePositive(timePickerDialog,hour);
          }
         });
       timePickerDialog.setButton(DialogInterface.BUTTON_NEGATIVE,
         "取消", new DialogInterface.OnClickListener() {
          @Override
          public void onClick(DialogInterface arg0,
            int arg1) {
           DateTimeUtils.showTimeNegative(slidedown_huadongtimehour);
          }
         });
       timePickerDialog.setCanceledOnTouchOutside(false);
       timePickerDialog.show();
      
       public static void showTimePositive(MyTimePickerDialog timePickerDialog,EditText hourEdit) {
  TimePicker timePicker = timePickerDialog.getTimePicker();
  int hour = timePicker.getCurrentHour();
  int mintue = timePicker.getCurrentMinute();
  hourEdit.setText(hour+":"+mintue);
   hourEdit.clearFocus();
   }
public static void showTimeNegative(EditText hourEdit, EditText ) {
  hourEdit.clearFocus();
   }
  

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI