DatePicker 对话框无法在 Android 中打开

DatePicker dialog can not opening in Android

我正在尝试在我的 Android 应用程序中实现 DatePicker 对话框。但是,不幸的是,对话没有打开。 我的代码如下:

    public class HomeActivity extends Activity implements View.OnClickListener {
    private EditText medWorkTitle;
    private EditText medWorkDescription;
    private Button mbtnSetTime;
    private Button mbtnSetDate;
    private Button mbtnSave;
    private TextView mtxtDateTime;


    private Calendar mCalendar;
    private int mInt_year, mInt_month, mInt_day;
    static final int DATE_PICKER_ID = 1111;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_home);
        init();
        // FOR DATE-PICKER
        mInt_year = mCalendar.get(Calendar.YEAR);
        mInt_month = mCalendar.get(Calendar.MONTH);
        mInt_day = mCalendar.get(Calendar.DAY_OF_MONTH);
        showDate(mInt_year, mInt_month + 1, mInt_day);

    }

    public void init() {
        mCalendar = Calendar.getInstance();
        medWorkDescription = (EditText) findViewById(R.id.edWorkDescription);
        medWorkTitle = (EditText) findViewById(R.id.edWorkTitle);

        mtxtDateTime = (TextView) findViewById(R.id.txtDateTime);

        mbtnSave = (Button) findViewById(R.id.btnSave);
        mbtnSave.setOnClickListener(this);
        mbtnSetDate = (Button) findViewById(R.id.btnSetDate);
        mbtnSetDate.setOnClickListener(this);
        mbtnSetTime = (Button) findViewById(R.id.btnSetTime);
        mbtnSetTime.setOnClickListener(this);
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.btnSetDate:


                break;
            case R.id.btnSetTime:
                break;
            case R.id.btnSave:
                break;
        }
    }
    // /////FOR DATE-PICKER
    public void setDate(View view) {
        showDialog(DATE_PICKER_ID);
        Toast.makeText(getApplicationContext(), "ca", Toast.LENGTH_SHORT)
                .show();
    }

    @Override
    protected Dialog onCreateDialog(int id) {
        // TODO Auto-generated method stub
        switch (id) {
            case DATE_PICKER_ID:
                return new DatePickerDialog(this, myDateListener, mInt_year,
                        mInt_month, mInt_day);
        }
        return null;
    }

    private DatePickerDialog.OnDateSetListener myDateListener = new DatePickerDialog.OnDateSetListener() {
        @Override
        public void onDateSet(DatePicker arg0, int arg1, int arg2, int arg3) {

            showDate(arg1, arg2 + 1, arg3);
        }
    };

    private void showDate(int year, int month, int day) {

        if (day < 10) {

            if (month < 10) {
                mtxtDateTime.setText(new StringBuilder().append("0" + day)
                        .append("/").append("0" + month).append("/")
                        .append(year));
            } else {
                mtxtDateTime.setText(new StringBuilder().append("0" + day)
                        .append("/").append(month).append("/").append(year));
            }

        } else {

            if (month < 10) {
                mtxtDateTime.setText(new StringBuilder().append(day)
                        .append("/").append("0" + month).append("/")
                        .append(year));

            } else {
                mtxtDateTime.setText(new StringBuilder().append(day)
                        .append("/").append(month).append("/").append(year));
            }
        }

    }


}

我的布局文件如下:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:paddingBottom="@dimen/activity_vertical_margin"
tools:context=".HomeActivity">

<RelativeLayout
    android:id="@+id/rel_homeContent"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_centerInParent="true">

    <EditText
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:hint="Work Title"
        android:id="@+id/edWorkTitle" />

    <EditText
        android:id="@+id/edWorkDescription"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_below="@+id/edWorkTitle"
        android:hint="Work Description" />

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/lin_homeDateTime"
        android:orientation="horizontal"
        android:layout_below="@+id/edWorkDescription">

        <Button
            android:id="@+id/btnSetDate"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Set Date"
            android:onClick="setDate"
            android:layout_weight="1" />

        <Button
            android:layout_weight="1"
            android:id="@+id/btnSetTime"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:onClick="setDate"
            android:text="Set Time" />
    </LinearLayout>

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/txtDateTime"
        android:layout_below="@+id/lin_homeDateTime"
        android:text="Please set date and time of your work" />

    <RadioGroup
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_below="@+id/txtDateTime"
        android:id="@+id/rgWorkValue"
        android:orientation="horizontal">

        <RadioButton
            android:id="@+id/rdNormal"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Normal"
            android:checked="true"
            android:layout_weight="1" />

        <RadioButton
            android:id="@+id/rdImportant"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Important"
            android:layout_weight="1" />

        <RadioButton
            android:id="@+id/rdVeryImportant"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Very Important"
            android:layout_weight="1" />
    </RadioGroup>

    <Button
        android:id="@+id/btnSave"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="SAVE"
        android:layout_below="@+id/rgWorkValue" />
</RelativeLayout>

请让我知道我的错误。谢谢。

据我了解,您需要在单击 setDate 按钮时显示日期选择器。因此,由于您的 OnClickListener 什么都不做,所以什么也没有发生。 我想你想要:

@Override
public void onClick(View view) {
    switch (view.getId()) {
        case R.id.btnSetDate:
            //Add there
            setDate(View view);
            break;
        case R.id.btnSetTime:
            break;
        case R.id.btnSave:
            break;
    }
}