我想在EditText
获取focus
时显示DatePicker:
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
。问题是当virtualkeyboard
获得EditText
时会显示focus
!那么当EditText
得到focus
时,如何让它不显示?
答案 0 :(得分:1)
确定使用此方法
public void hideSoftKeyboard(Activity activity) {
InputMethodManager inputMethodManager = (InputMethodManager) activity
.getSystemService(Activity.INPUT_METHOD_SERVICE);
if (activity.getCurrentFocus() != null)
inputMethodManager.hideSoftInputFromWindow(activity
.getCurrentFocus().getWindowToken(), 0);
}
答案 1 :(得分:0)
以编程方式试用
YourEditText=(EditText) findViewById(R.id.editText1);
YourEditText.setInputType(InputType.TYPE_NULL);
答案 2 :(得分:0)
基本上,您可以使用以下内容翻译任何日期:
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
String date = format.format(Date.parse("Your date string"));
然后,您可以检测用户何时选择日期,并显示您要使用的版本。
请点击此处了解详情:http://developer.android.com/reference/java/text/SimpleDateFormat.html
答案 3 :(得分:0)
将Edittext转换为TextView并为视图添加onclick侦听器,并显示相同的datepicker对话框,如下所示:
private DatePickerDialog mDatePicker;
static final int DATE_PICKER_ID = 1111;
private int mYear, mMonth, mDay, mHour, mMin;
mTvDate.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
final Calendar c = Calendar.getInstance();
mYear = c.get(Calendar.YEAR);
mMonth = c.get(Calendar.MONTH);
mDay = c.get(Calendar.DAY_OF_MONTH);
showDialog(DATE_PICKER_ID);
}
});
@Override
protected Dialog onCreateDialog(int id) {
switch (id) {
case DATE_PICKER_ID:
mDatePicker = new DatePickerDialog(this, pickerListener, mYear,
mMonth, mDay);
mDatePicker.setTitle("Select Date");
return mDatePicker;
}
return null;
}
private DatePickerDialog.OnDateSetListener pickerListener = new DatePickerDialog.OnDateSetListener() {
// when dialog box is closed, below method will be called.
@SuppressLint("SimpleDateFormat")
@Override
public void onDateSet(DatePicker view, int selectedYear,
int selectedMonth, int selectedDay) {
mDay = selectedDay;
mMonth = selectedMonth;
mYear = selectedYear;
Calendar c = Calendar.getInstance();
c.set(mYear, mMonth, mDay);
SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");
String selectedDate = df.format(c.getTime());
mTvDate.setText(selectedDate);
}
};
答案 4 :(得分:0)
在edittext onclicklistener方法中尝试这个。这会强制键盘隐藏。
getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_HIDDEN);