TimePickerDialog和Jelly Bean,onTimeSet在取消时触发

时间:2012-08-20 02:29:13

标签: android timepicker android-4.2-jelly-bean

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

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

在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 个答案:

答案 0 :(得分:2)

bug修复之前我建议不要使用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();

答案 1 :(得分:2)

根据Ankur Chaudhary的精彩answer,请执行以下操作:

new TimePickerDialog.OnTimeSetListener() {
    @Override
    public void onTimeSet(TimePicker view, int setHour, int setMinute) {  
        if (view.isShown()) {
            // read the time here :)
        }
    }
};

答案 2 :(得分: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
                }
            });

答案 3 :(得分:0)

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