第一次单击时不显示日期选择器

Datepicker not showing when clicked the first time

我有一个编辑字段,当我单击它时,会打开一个日期选择器,但我无法在第一次单击时弹出日期选择器。如果我第一次单击编辑字段,则不会出现日期选择器。当我点击第二次或第三次时,它正在显示。这是我的代码:

public class MainActivity extends Activity implements OnClickListener {

    private Calendar calendar;
    private int day;
    private int month;
    private int year;

    @Override  
    protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);

            setContentView(R.layout.activity);    

            date = (EditText) findViewById(R.id.date);
            calendar = Calendar.getInstance();
            day = calendar.get(Calendar.DAY_OF_MONTH);
            month = calendar.get(Calendar.MONTH);
            year = calendar.get(Calendar.YEAR);
            date.setOnClickListener(this);
    }

    @Override
    protected Dialog onCreateDialog(int id) {
           // TODO Auto-generated method stub
           return new DatePickerDialog(this, datePickerListener, year, month, day);     
    }

    private DatePickerDialog.OnDateSetListener datePickerListener = new DatePickerDialog.OnDateSetListener() {               
        public void onDateSet(DatePicker view, int selectedYear, int selectedMonth, int selectedDay) {

        date.setText(selectedYear + " - " + (selectedMonth + 1) + " - " + selectedDay);
        }
    };

    @Override
    public void onClick(View v) {
        // TODO Auto-generated method stub
        showDialog(0);
    }
}

您真的不需要 Activity 来创建对话。只需创建一个 XML 并使用 AlertDialog。

final View dialogView = View.inflate(this, R.layout.dialog_date_picker, null);
final AlertDialog alertDialog = new AlertDialog.Builder(this).create();

DatePicker datePicker = (DatePicker) dialogView.findViewById(R.id.date_picker);
      datePicker.init(mCalendar.get(Calendar.YEAR),mCalendar.get(Calendar.MONTH),mCalendar.get(Calendar.DAY_OF_MONTH),null);

dialogView.findViewById(R.id.date_time_set).setOnClickListener(new View.OnClickListener()
{
  @Override
  public void onClick(View view)
  {
    alertDialog.dismiss();
  }
});

alertDialog.setView(dialogView);
alertDialog.show();

这里是 XML (dialog_date_picker.xml):

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:padding="8dp"
    android:layout_height="match_parent">

    <DatePicker
        android:id="@+id/date_picker"
        android:layout_width="match_parent"
        android:calendarViewShown="true"
        android:spinnersShown="false"
        android:layout_weight="4"
        android:layout_height="0dp" />

    <Button
        android:id="@+id/date_time_set"
        android:layout_weight="1"
        android:layout_width="match_parent"
        android:text="Set"
        android:layout_height="0dp" />

</LinearLayout>

在您的布局 xml 中,在您的编辑文本中执行此操作-

android:focusableInTouchMode="false"

这会起作用

如果您以编程方式创建编辑文本,则可以使用动态设置。

...
EditText editText = new EditText();
editText.setFocusableInTouchMode(false);
...