来自警报对话框的android日期选择器

标签 android datepicker android-datepicker

我正在尝试从警告对话框中启动日期选择器。但由于某种原因,showDialog(DATE_DIALOG_ID) 根本没有打开。有人可以告诉我出了什么问题吗?

final AlertDialog.Builder alt_bld = new AlertDialog.Builder(this);
        alt_bld.setIcon(R.drawable.icon);
        alt_bld.setTitle("Categories");
        alt_bld.setSingleChoiceItems(categoryType, -1, new DialogInterface.OnClickListener() {
            public void onClick(DialogInterface dialog, int item) {
                System.out.println("Category selected= "+categoryType[item]);
                Toast.makeText(getApplicationContext(), "Category selected= "+categoryType[item], Toast.LENGTH_SHORT).show();
                category=categoryType[item];


                if(category.equalsIgnoreCase("Time")){
                    showDialog(DATE_DIALOG_ID);
                    System.out.println("selected date here:"+selectedDate);
                    Intent intent = new Intent(MainActivity.this, ListViewActivity.class);
                    bundle.putString("category", category);
                    bundle.putString("attribute", selectedDate);
                    intent.putExtras(bundle);
                    startActivity(intent);

                }

其他日期选择器相关代码

static final int DATE_DIALOG_ID = 0;



@Override
    protected Dialog onCreateDialog(int id) {
        switch (id) {
        case DATE_DIALOG_ID:
            return new DatePickerDialog(this,
                        mDateSetListener,
                        mYear, mMonth, mDay);
        }
        return null;
    }   


    private DatePickerDialog.OnDateSetListener mDateSetListener =
        new DatePickerDialog.OnDateSetListener() {


        public void onDateSet(DatePicker view, int year,
                              int monthOfYear, int dayOfMonth) {
            mYear = year;
            mMonth = monthOfYear;
            mDay = dayOfMonth;
            selectedDate = Integer.toString(mMonth)+"-"+Integer.toString(mDay)+"-"+Integer.toString(mYear);
            System.out.println("selected date is:"+selectedDate);
        }


    };

public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.homescreen);

        final Calendar cal = Calendar.getInstance();
        mYear = cal.get(Calendar.YEAR);
        mMonth = cal.get(Calendar.MONTH);
        mDay = cal.get(Calendar.DAY_OF_MONTH);

最佳答案

在您的代码中,您正在调用 showDialog(int);并立即启动新的 Intent 。您如何期望看到对话。实际上会因为对话框没有关闭而导致内存泄漏。

关于来自警报对话框的android日期选择器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4359696/

相关文章:

android - Firebase Crashlytics 在没有 Fabric API key 的情况下无法工作

jquery-ui - 将 jquery ui datepicker 上的 maxDate 设置为特定日期

android - android timePickerDialog 的时间验证

java - 如何在Android中设置DatePicker和TimePicker的限制

android - 与 Activity 风格不同的日期选择器

java - 可扩展抽屉导航不动

android - 在 getjar.com 上推送 Android 应用程序更新

android - 恢复期间备份代理签名不匹配

jquery - 在更改选择框时更改 BootStrap Datepicker 的 startDate 和 endDate 参数

java - 打开带有建议日期的 DatePicker