最近接触Android编程,感触很深。。。书到用时方恨少;事非经过不知难。。。感觉后一句有点不贴切至少在某些地方是。。。
下面我贴一下源码,给我做给记录,给需要的一个参考
需求是单击EditText弹出
先发个样图证明一下
日期的xml
<EditText这里注意
android:id="@+id/xjrq"
style="@style/EditText_blueContent"
android:layout_height="36dp"
android:digits="0123456789-"
android:focusable="false"
android:focusableInTouchMode="false" />
android:focusable="false"android:focusableInTouchMode="false"解决界面加载获得自动焦点,给弹出框来
下面是java源码
xianjiaoqixian = (EditText) findViewById(R.id.xjrq);
xianjiaoqixian.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
timeFlag = 0;
showDialog(0);
}
});
xianjiaoqixian.setOnFocusChangeListener(new OnFocusChangeListener() {
public void onFocusChange(View v, boolean hasFocus) {
if (hasFocus == true) {
timeFlag = 0;
hideIM(v);
showDialog(0);
}
}
});
private DatePickerDialog.OnDateSetListener mDateSetListener = new DatePickerDialog.OnDateSetListener() {
@Override
public void onDateSet(DatePicker view, int year, int monthOfYear,
int dayOfMonth) {
// TODO Auto-generated method stub
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) {
xianjiaoqixian.setText(String.valueOf(mYear) + "-" + mm + "-"
+ dd);
} else {
jiaonariqi.setText(String.valueOf(mYear) + "-" + mm + "-" + dd);
}
}
};
protected Dialog onCreateDialog(int id) {
<span style="white-space:pre"></span>//这里需要注意一下,如果不得到当前的日期默认显示的日期是1900年1月01日Calendar dateAndTime = Calendar.getInstance(Locale.CHINA);mYear = dateAndTime.get(Calendar.YEAR);mMonth = dateAndTime.get(Calendar.MONTH);mDay = dateAndTime.get(Calendar.DAY_OF_MONTH);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) {e.printStackTrace();}}