1. 声明EditText变量,并关联到相应控件上
1 private EditText sellStartTime; 2 private EditText sellEndTime;
1 sellStartTime = (EditText)findViewById(R.id.editSellStartTime); 2 sellEndTime = (EditText)findViewById(R.id.editSellEndTime);
2. 日期选择器函数
1 private DatePickerDialog.OnDateSetListener mDateSetListener = 2 new DatePickerDialog.OnDateSetListener() { 3 public void onDateSet(DatePicker view, int year, int monthOfYear, int dayOfMonth) { 4 mYear = year; 5 String mm; 6 String dd; 7 if(monthOfYear<=9) 8 { 9 mMonth = monthOfYear+1;10 mm="0"+mMonth;11 }12 else{13 mMonth = monthOfYear+1;14 mm=String.valueOf(mMonth);15 }16 if(dayOfMonth<=9)17 {18 mDay = dayOfMonth;19 dd="0"+mDay;20 }21 else{22 mDay = dayOfMonth;23 dd=String.valueOf(mDay);24 }25 mDay = dayOfMonth;26 if(timeFlag == 0) {27 sellStartTime.setText(String.valueOf(mYear)+"-"+mm+"-"+dd);28 }else {29 sellEndTime.setText(String.valueOf(mYear)+"-"+mm+"-"+dd);30 }31 } 32 };33 34 protected Dialog onCreateDialog(int id) {35 switch (id) { 36 case 0:37 return new DatePickerDialog(this,38 mDateSetListener,39 mYear, mMonth, mDay);40 case 1:41 return new DatePickerDialog(this,42 mDateSetListener,43 mYear, mMonth, mDay);44 }45 return null;46 }47 48 // 隐藏手机键盘49 private void hideIM(View edt){50 try {51 InputMethodManager im = (InputMethodManager) getSystemService(Activity.INPUT_METHOD_SERVICE);52 IBinder windowToken = edt.getWindowToken();53 if(windowToken != null) {54 im.hideSoftInputFromWindow(windowToken, 0);55 }56 } catch (Exception e) {57 58 }59 }
3. 注册监听器,当EditText获得焦点时,或者被点击时,打开日期选择器
1 sellStartTime.setOnClickListener(new OnClickListener(){ 2 public void onClick(View v) { 3 timeFlag = 0; 4 showDialog(0); 5 } 6 }); 7 sellEndTime.setOnClickListener(new OnClickListener() { 8 public void onClick(View v) { 9 timeFlag = 1;10 showDialog(1);11 } 12 });13 sellStartTime.setOnFocusChangeListener(new OnFocusChangeListener() {14 public void onFocusChange(View v, boolean hasFocus) {15 if (hasFocus == true) {16 timeFlag = 0;17 hideIM(v);18 showDialog(0); 19 }20 }21 });22 23 sellEndTime.setOnFocusChangeListener(new OnFocusChangeListener() {24 public void onFocusChange(View v, boolean hasFocus) {25 if (hasFocus == true) {26 timeFlag = 1;27 hideIM(v);28 showDialog(1); 29 }30 }31 });
4. 在包含EditText的LinearLayout中,添加语句
1 android:focusable="true" android:focusableInTouchMode="true"
这句话,是为了防止EditText自动获取焦点,触发日期选择器。
EditText如何关闭软键盘
方法一:在AndroidMainfest.xml中选择哪个activity,设置windowSoftInputMode属性为adjustUnspecified|stateHidden例如:<activity android:name=".Main" android:label="@string/app_name" android:windowSoftInputMode="adjustUnspecified|stateHidden" android:configChanges="orientation|keyboardHidden"> <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity>方法二:强制隐藏Android输入法窗口例如:EditText edit=(EditText)findViewById(R.id.edit); InputMethodManager imm = (InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE); imm.hideSoftInputFromWindow(edit.getWindowToken(),0);2.EditText始终不弹出软件键盘例:EditText edit=(EditText)findViewById(R.id.edit); edit.setInputType(InputType.TYPE_NULL);
还有一个问题是怎样才能不让实体键盘也输入呢,解决方法
Android用代码实现EditText不可编辑
editText.setFilters(new InputFilter[] { new InputFilter() {
@Override
public CharSequence filter(CharSequence source, int start,
int end, Spanned dest, int dstart, int dend) {
return source.length() < 1 ? dest.subSequence(dstart, dend) : "";
}
}
}); 但是这样连DatePicker的设置也不能显示到EditText里了,改一改
//实现EditText不可编辑
birthEdit.setFilters(new InputFilter[]{new InputFilter(){ @Override public CharSequence filter(CharSequence source, int start, int end, Spanned dest, int dstart, int dend) { showDialog(HmConst.DIALOG_DATEPICKER);//不管按什么键都让DatePicker出现 return source.length() == 1 ? "" : source; //DatePicker的设置还是要让他显示滴 } }});
我的实验代码,可行
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub super.onCreate(savedInstanceState); setContentView(R.layout.register);birthEdit =(EditText)findViewById(R.id.birthEdit);
makeDateEdit();
}
/**
* 将EditText变成一个日期选择器 */ public void makeDateEdit(){ //隐藏EditText键盘 birthEdit.setInputType(InputType.TYPE_NULL); //点击显示DatePickerDialog birthEdit.setOnClickListener(new OnClickListener(){ @Override public void onClick(View v) { // TODO Auto-generated method stub showDialog(HmConst.DIALOG_DATEPICKER); } }); //实现EditText不可编辑 birthEdit.setFilters(new InputFilter[]{new InputFilter(){ @Override public CharSequence filter(CharSequence source, int start, int end, Spanned dest, int dstart, int dend) { showDialog(HmConst.DIALOG_DATEPICKER);//不管按什么键都让DatePicker出现 return source.length() == 1 ? "" : source; } }}); //屏蔽实体键盘delete键 birthEdit.setOnKeyListener(new OnKeyListener(){ @Override public boolean onKey(View v, int keyCode, KeyEvent event) { // TODO Auto-generated method stub if(keyCode == KeyEvent.KEYCODE_DEL) { showDialog(HmConst.DIALOG_DATEPICKER);//delete键都让DatePicker出现 return true;//返回真就会屏蔽delete键的原效果 } return false; } }); //设置时间选择器的监听器 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; mMonth = monthOfYear+1; //因为month是从0开始计数的 if(mMonth<=9) { mm="0"+mMonth; } else{ mm=String.valueOf(mMonth); } mDay = dayOfMonth; if(mDay<=9) { dd="0"+mDay; } else{ dd=String.valueOf(mDay); } mDay = dayOfMonth; //将选择的时间显示在EditText上 birthEdit.setText(String.valueOf(mYear)+"-"+mm+"-"+dd); } }; } @Override protected Dialog onCreateDialog(int id) { // TODO Auto-generated method stub switch(id){ case HmConst.DIALOG_DATEPICKER: return new DatePickerDialog(this,mDateSetListener,2010,11,25); default: return null; } }