【问题标题】:How to create a textedit for date only programatically in android?如何在android中仅以编程方式为日期创建文本编辑?
【发布时间】:2013-07-27 21:38:02
【问题描述】:

我想创建一个文本编辑字段,用户只输入日期(没有时间)。日期将存储在MY SQL 中。用最少的验证来做到这一点的最好方法是什么?是否有一个内置的日期文本字段以保持正确的格式?

我有这个:

public static void AddEditTextDate(Context context, LinearLayout linearlayout, String text, int id) {
    EditText edittext = new EditText(context);
    edittext.setInputType(InputType.TYPE_DATETIME_VARIATION_DATE);
    edittext.setText(text);
    edittext.setId(id);
    LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LayoutParams.MATCH_PARENT,LayoutParams.WRAP_CONTENT);
    edittext.setLayoutParams(params);
    linearlayout.addView(edittext);
}

但是当我尝试输入它时,它看起来就像一个普通的键盘。我希望它默认进入数字键盘或其他东西......

编辑:它需要与 android 2.1+(即 v7)一起使用

有人知道吗?

谢谢

【问题讨论】:

  • 我真的建议你尝试在一些 xml 文件中定义它并在必要时加载它。并测试一些其他输入类型,如数字/电话等。
  • 我需要动态插入它们,因为用户可以更改他们的数据。
  • 使用InputType.TYPE_CLASS_DATETIME 而不是InputType.TYPE_DATETIME_VARIATION_DATE 来显示纯数字键盘。您当然需要在用户输入后验证日期和格式。你可以使用regex

标签: java android mysql date android-edittext


【解决方案1】:

你说Whats the best way to do this with the least amount of validation? Is there like a built in textfield for dates that keeps it in the proper format?

我想到了一种方法,使用它您可能不需要检查用户输入的日期格式的任何验证。点击EditText 框即可拨打DatePickerDialog。然后用户可以使用它来选择日期。用户选择日期后,您可以使用所选日期更新您的 EditText。通过这种方式,您可以减少验证输入日期格式的工作量,并且用户可以轻松直观地选择日期。你可能会这样:

Calendar myCalendar = Calendar.getInstance();
DatePickerDialog.OnDateSetListener date = new DatePickerDialog.OnDateSetListener() {

    @Override
    public void onDateSet(DatePicker view, int year, int monthOfYear,
            int dayOfMonth) {
        myCalendar.set(Calendar.YEAR, year);
        myCalendar.set(Calendar.MONTH, monthOfYear);
        myCalendar.set(Calendar.DAY_OF_MONTH, dayOfMonth);
        updateLabel();
    }

};
//When the editText is clicked then popup the DatePicker dialog to enable user choose the date       
edittext.setOnClickListener(new OnClickListener() {

     @Override
     public void onClick(View v) {
         // TODO Auto-generated method stub
         new DatePickerDialog(new_split.this, date, myCalendar
                 .get(Calendar.YEAR), myCalendar.get(Calendar.MONTH),
                 myCalendar.get(Calendar.DAY_OF_MONTH)).show();
     }
 });
// Call this whn the user has chosen the date and set the Date in the EditText in format that you wish
 private void updateLabel() {

    String myFormat = "MM/dd/yyyy"; //In which you need put here
    SimpleDateFormat sdf = new SimpleDateFormat(myFormat, Locale.US);   
    edittext.setText(sdf.format(myCalendar.getTime()));
 }

来源:This 回答 Datepicker: How to popup datepicker when click on edittext 问题。希望这可以帮助。

【讨论】:

  • DatePicker 类本身是从 API 级别 1 开始的。您可以使用 AlertDialog.Builder 创建一个对话框并将其内容视图设置为 DatePicker 实例。 (或者,使用 ActionBarSherlock 你可以使用 SherlockDialogFragment 并将 DatePicker 放在那里)
  • @Karakuri 是对的。您可以将其与 android 2.1 一起使用。正如他正确提到的,您也可以选择 ActionBarSherlock。
猜你喜欢
  • 1970-01-01
  • 2023-03-14
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-02-27
  • 2020-02-21
  • 2017-12-28
相关资源
最近更新 更多