我想說明一個datepicker時EditText
得到focus
:如何讓EditText獲取焦點時不顯示虛擬鍵盤?
public class DatePickerUtil {
private Context ctxt;
private Calendar calendrier = Calendar.getInstance();
private EditText chp;
private KeyListener keyListener;
private OnTouchListener touchListener;
private boolean canceled;
private DatePickerDialog picker;
public DatePickerUtil(Context context, EditText field) {
ctxt = context;
chp = field;
keyListener = new KeyListener() {
@Override
public int getInputType() {
// TODO Auto-generated method stub
return 0;
}
@Override
public boolean onKeyDown(View view, Editable text, int keyCode,
KeyEvent event) {
// TODO Auto-generated method stub
return false;
}
@Override
public boolean onKeyUp(View view, Editable text, int keyCode,
KeyEvent event) {
// TODO Auto-generated method stub
return false;
}
@Override
public boolean onKeyOther(View view, Editable text, KeyEvent event) {
// TODO Auto-generated method stub
return false;
}
@Override
public void clearMetaKeyState(View view, Editable content,
int states) {
// TODO Auto-generated method stub
}
};
touchListener = new OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
if (event.getAction() == MotionEvent.ACTION_UP) {
canceled = false;
show();
}
return false;
}
};
}
private void show() {
OnDateSetListener evt = new OnDateSetListener() {
@Override
public void onDateSet(DatePicker view, int annee, int mois, int jour) {
if (!canceled) {
calendrier.set(annee, mois, jour);
chp.setText(DateFormat.format("yyyy-MM-dd", calendrier));
}
}
};
picker = new DatePickerDialog(ctxt, evt, calendrier.get(Calendar.YEAR),
calendrier.get(Calendar.MONTH),
calendrier.get(Calendar.DAY_OF_MONTH));
picker.setButton(DialogInterface.BUTTON_NEGATIVE, ctxt.getResources().getString(R.string.button_cancel), new OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
canceled = true;
dialog.dismiss();
}
});
picker.show();
}
public void attache() {
chp.setOnTouchListener(touchListener);
chp.setKeyListener(keyListener);
}
}
在運行時,焦點不是EditText
裏面只是圍繞現場:
正如你可以看到用戶無法編輯EditText
的內容,所以virtualkeyboard
是沒有必要的。問題是當EditText
得到focus
時顯示virtualkeyboard
!那麼如何在EditText
得到focus
時不顯示?
你有沒有試着用'安卓的inputType = 「日期」'在XML? – NSimon 2015-03-31 09:13:37
是的,但Android的默認格式爲「DD/MM/YYYY」,儘管我的格式爲「YYYY-MM-DD」 – pheromix 2015-03-31 09:24:51