datepicterDialog和timepicterDialog取消按钮

这是日期弹出框的第一种弹出方式:分开弹出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();
   }

时间: 2024-12-21 12:11:10

datepicterDialog和timepicterDialog取消按钮的相关文章

自定义UISearchBar的“取消按钮”

需求: UISearchBar的取消按钮,没有公共方法可以修改它的属性.但我们仍可以通过遍历UISearchBar的子控件来设置该"取消按钮". 效果: 将UISearchBar的取消按钮中的"取消"文字,更改为"搜索". 代码如下: - (void)setSearchBar:(UISearchBar *)searchBar{ UISearchBar *searchBar = [[UISearchBar alloc] initWithFrame:

[android] 解决DatePickerDialog和TimePickerDialog控件取消按钮问题

一. 问题提出 在Android程序中,我们通常需要使用DatePicker来设置日期,TimePicker来设置时间.其基本步骤是: 1.先定义DatePicker和TimePicker布局 2.然后通过Calendar类获得系统时间 3.接着通过init方法将日期传递给DatePicker初始化日期控件 4.在响应点击事件中可以通过DatePicker的getYear().getDayOfMonth().getMonth()函数获取具体日期 同时可以在OnDateChangedListene

EasyUI 中点击取消按钮关闭Dialog(对话框窗口)

1.在取消按钮里的function中通过id找到这个对话框所在的层, 2.点出这个对话框,然后在括号了写入关闭的方法 <div id="dl1" class="easyui-dialog" title="窗口" style="width:400px;height: 200px" data-options=" toolbar:[ { text:'添加', iconCls:'icon-add', handler:f

IOS第五天(1:取消按钮的监听和设置代理textField字数限制)

***********取消按钮的监听和设置代理textField字数限制 UITextFieldDelegate #import "HMViewController.h" @interface HMViewController () <UITextFieldDelegate> @end @implementation HMViewController /** 1> UIButton -> UIControl -> UIView 1.1 设置控件的状态 启用

GridView编辑、取消按钮自定义控件

这个需求来自于论坛一位坛友提出的问题,他希望能够自定义编辑.取消按钮,而不是用GridView自带的编辑和取消.这里只当抛砖引玉,提出一些解决方案. 首先在页面前台设置一个GridView. <div> <asp:GridView ID="GridView1" runat="server"> <Columns> <asp:TemplateField HeaderText="操作"> <Item

IOS开发UISearchBar失去第一响应者身份后,取消按钮不执行点击事件的问题

在iOS开发中,使用UISearchBar的时候,当搜索框失去焦点的时候,取消按钮是默认不能点击的,如图按钮的颜色是灰色的:  这是因为此时取消按钮的enabled属性被设置为NO了,那么当我们需要让取消按钮一直可以点击的时候,就需要我们自己来改变该属性的赋值,这里举例当点击search按钮的时候,回收键盘的同时让取消按钮的可以点击,在代理方法里面添加代码如下: -(void)searchBarSearchButtonClicked:(UISearchBar *)searchBar { [sea

修改IOS中UISearchBar的取消按钮背景、搜索内容输入文本框背景和UISearchBar的背景

转载请标明出处:http://blog.csdn.net/android_ls/article/details/39993433 测试的手机IOS系统版本号为:6.1.3,实现步骤如下: 1.添加UISearchBar到父View _searchBar = [[UISearchBar alloc]init]; _searchBar.frame = CGRectMake(0, 0, self.view.frame.size.width, kSeachBarH); _searchBar.autore

在UISearchDisplayController里定义取消按钮和去掉No Result

- (void)searchDisplayControllerWillBeginSearch:(UISearchDisplayController *)controller { searchDisplayController.searchBar.backgroundColor = [UIColor whiteColor]; searchDisplayController.searchBar.showsCancelButton = YES; //定义取消按钮 /* *ios7与ios6方法不同 *

搜索框在失去焦点时,取消按钮不可点击问题

当对searchbar进行如下操作时 [_searchBar resignFirstResponder]; 会出现 此时的取消按钮是置灰的,原因在于searchbar失去焦点后,取消按钮的enabled属性变为NO.知道原因后,若想让取消按钮一直不变色,并且可以点击.那么需要设置其enabled属性 for(UIView *view in [[[_searchBar subviews] objectAtIndex:0] subviews]) { if([view isKindOfClass:[N