3

如果我在 Jelly Bean 中使用 TimePickerDialog,对话框没有取消按钮,无论我做什么(比如尝试返回按钮),onTimeSet 都会触发,所以我无法知道用户是否没有'不想采取行动与否。

我尝试添加 onCancel 和 onDismiss 处理程序,但取消是在 onTimeSet 处理程序之后调用的。

在 ICS、GB 上,有一个可以正常工作的取消按钮。

创建默认应用程序,将 Button 添加到布局

<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" >

    <Button
        android:id="@+id/Button01"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Show Me" >
    </Button>

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true"
        android:layout_centerVertical="true"
        android:text="@string/hello_world"
        tools:context=".MainActivity" />

</RelativeLayout>

然后在主代码中有以下内容:

package com.example.example;

import android.os.Bundle;
import android.app.Activity;
import android.app.Dialog;
import android.app.TimePickerDialog;
import android.content.DialogInterface;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.TimePicker;

public class MainActivity extends Activity {

    @Override
    public void onCreate(Bundle savedInstanceState) {   
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Button button = (Button) findViewById(R.id.Button01);
        button.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
                Dialog d = new TimePickerDialog(MainActivity.this, new TimePickerDialog.OnTimeSetListener() {
                    @Override
                    public void onTimeSet(TimePicker view, int hourOfDay, int minute) {
                        Log.i("Hello", "onTimeSet called");
                    }
                }, 8, 0, false);
                d.setCancelable(true);
                d.setOnCancelListener(new TimePickerDialog.OnCancelListener() {

                    @Override
                    public void onCancel(DialogInterface dialog) {
                        Log.i("Hello", "onCancel called");

                    }
                });

                d.setOnDismissListener(new TimePickerDialog.OnDismissListener() {

                    @Override
                    public void onDismiss(DialogInterface dialog) {
                        Log.i("Hello", "onDismiss called"); 
                    }
                });

                d.show();
            }
        });   
    }
}
4

4 回答 4

2

修复错误之前,我建议不要使用 DatePickerDialog 或 TimePickerDialog 使用定制的 AlertDialog 和 TimePicker/DatePicker 小部件;

更改 TimePickerDialog 为;

    final TimePicker timePicker = new TimePicker(this);
    timePicker.setIs24HourView(true);
    timePicker.setCurrentHour(20);
    timePicker.setCurrentMinute(15);

    new AlertDialog.Builder(this)
            .setTitle("Test")
            .setPositiveButton(android.R.string.ok, new OnClickListener() {

                @Override
                public void onClick(DialogInterface dialog, int which) {
                    Log.d("Picker", timePicker.getCurrentHour() + ":"
                            + timePicker.getCurrentMinute());
                }
            })
            .setNegativeButton(android.R.string.cancel,
                    new OnClickListener() {

                        @Override
                        public void onClick(DialogInterface dialog,
                                int which) {
                            Log.d("Picker", "Cancelled!");
                        }
                    }).setView(timePicker).show();

更改 DatePickerDialog 为;

    final DatePicker datePicker = new DatePicker(this);
    datePicker.init(2012, 10, 5, null);
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {
        datePicker.setCalendarViewShown(false);
    }

    new AlertDialog.Builder(this)
            .setTitle("Test")
            .setPositiveButton(android.R.string.ok, new OnClickListener() {

                @Override
                public void onClick(DialogInterface dialog, int which) {
                    Log.d("Picker", datePicker.getYear() + " "
                            + (datePicker.getMonth() + 1) + " "
                            + datePicker.getDayOfMonth());
                }
            })
            .setNegativeButton(android.R.string.cancel,
                    new OnClickListener() {

                        @Override
                        public void onClick(DialogInterface dialog,
                                int which) {
                            Log.d("Picker", "Cancelled!");
                        }
                    }).setView(datePicker).show();
于 2013-03-03T21:35:29.953 回答
2

根据 Ankur Chaudhary 的精彩回答,只需执行以下操作:

new TimePickerDialog.OnTimeSetListener() {
    @Override
    public void onTimeSet(TimePicker view, int setHour, int setMinute) {  
        if (view.isShown()) {
            // read the time here :)
        }
    }
};
于 2015-03-16T17:42:48.400 回答
1

好的,看起来 DatePicker here有类似的错误,但我必须在调用 setButton() 之前通过将 Dialog 转换为 AlertDialog 来修改我的代码

((AlertDialog) d).setButton(DialogInterface.BUTTON_NEGATIVE, "Cancel", 
                    new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialog, int which) {
                    // Turn on your cancel flag here
                }
            });
于 2012-08-21T00:33:40.610 回答
0

尝试使用活动焦点(Activity.onWindowFocusChanged())。它应该在Dialog弹出时改变

于 2012-08-20T11:58:04.163 回答