4

I have a editfield and when I click on it a datepicker will open but I am not able to get the datepicker to popup when clicked for first time. If I click the editfield the first time, the datepicker does not appear. When I click the second or third time, it is showing. Here is my code:

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);
    }
}
4

3 回答 3

5

在您的布局 xml 中,在您的 edittext- 中执行此操作

android:focusableInTouchMode="false"

这会起作用

于 2015-05-12T05:44:57.040 回答
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>
于 2015-05-12T03:31:40.697 回答
0

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

...
EditText editText = new EditText();
editText.setFocusableInTouchMode(false);
...
于 2017-02-21T12:12:51.597 回答