Android手机开发:EditText+DatePicker带日期选择器的编辑框

标签: android手机exceptionstringdialognull
13858人阅读 评论(5) 收藏 举报
分类:

1. 声明EditText变量,并关联到相应控件上

private EditText sellStartTime;
private EditText sellEndTime;
sellStartTime = (EditText)findViewById(R.id.editSellStartTime);
sellEndTime = (EditText)findViewById(R.id.editSellEndTime);

2. 日期选择器函数

private DatePickerDialog.OnDateSetListener mDateSetListener =
            new DatePickerDialog.OnDateSetListener() {
				public void onDateSet(DatePicker view, int year, int monthOfYear, int dayOfMonth) {
                    mYear = year;
                    String mm;
                    String dd;
                    if(monthOfYear<=9)
                    {
                    	mMonth = monthOfYear+1;
                    	mm="0"+mMonth;
                    }
                    else{
                    	mMonth = monthOfYear+1;
                    	mm=String.valueOf(mMonth);
                    	}
                    if(dayOfMonth<=9)
                    {
                    	mDay = dayOfMonth;
                    	dd="0"+mDay;
                    }
                    else{
                    	mDay = dayOfMonth;
                    	dd=String.valueOf(mDay);
                    	}
                    mDay = dayOfMonth;
                    if(timeFlag == 0) {
                    	sellStartTime.setText(String.valueOf(mYear)+"-"+mm+"-"+dd);
                    }else {
                    	sellEndTime.setText(String.valueOf(mYear)+"-"+mm+"-"+dd);
                    }
                }			
            };
   
    protected Dialog onCreateDialog(int id) {
    	switch (id) {	
        case 0:
        	return new DatePickerDialog(this,
                    mDateSetListener,
                    mYear, mMonth, mDay);
        case 1:
        	return new DatePickerDialog(this,
                    mDateSetListener,
                    mYear, mMonth, mDay);
        }
	    return null;
    }
    
    // 隐藏手机键盘
    private void hideIM(View edt){
        try {
             InputMethodManager im = (InputMethodManager) getSystemService(Activity.INPUT_METHOD_SERVICE);
             IBinder  windowToken = edt.getWindowToken();
             if(windowToken != null) {
                 im.hideSoftInputFromWindow(windowToken, 0);
             }
         } catch (Exception e) {
           
         }
     }

3. 注册监听器,当EditText获得焦点时,或者被点击时,打开日期选择器

                sellStartTime.setOnClickListener(new OnClickListener(){
	                public void onClick(View v) {
	        	        timeFlag = 0;
	        	        showDialog(0);
			}
	        });
		sellEndTime.setOnClickListener(new OnClickListener() {
			public void onClick(View v) {
				timeFlag = 1;
				showDialog(1);
			}			
		});
		sellStartTime.setOnFocusChangeListener(new OnFocusChangeListener() {
			public void onFocusChange(View v, boolean hasFocus) {
				if (hasFocus == true) {
					timeFlag = 0;
					hideIM(v);
					showDialog(0);  
				}
			}
		});

		sellEndTime.setOnFocusChangeListener(new OnFocusChangeListener() {
			public void onFocusChange(View v, boolean hasFocus) {
				if (hasFocus == true) {
					timeFlag = 1;
					hideIM(v);
					showDialog(1);  
				}
			}
		});

4. 在包含EditText的LinearLayout中,添加语句

android:focusable="true"  android:focusableInTouchMode="true"

这句话,是为了防止EditText自动获取焦点,触发日期选择器。


1
1

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:580416次
    • 积分:6952
    • 等级:
    • 排名:第3424名
    • 原创:163篇
    • 转载:6篇
    • 译文:0篇
    • 评论:93条
    我的推荐
    最新评论