如果我在Jelly Bean中使用TimePickerDialog,则对话框没有“取消”按钮,并且无论我做什么(如尝试“上一步”按钮),OnTimeSet都将始终启动,因此我无法知道用户是否不想采取行动。
我试过添加oncancel和ondismiss处理程序,但是取消是在ontimeset处理程序之后调用的。
在ics、gb上,有一个cancel按钮,工作正常。
创建默认应用程序,将按钮添加到布局
<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();
}
});
}
}
最佳答案
在bug修复之前,我建议不要使用datepickerdialog或timepickerdialog使用带有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();
关于android - TimePickerDialog和Jelly Bean,onTimeSet在取消时触发,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/12031663/