对话框打开时检测后退按钮事件
Detect Back Button Event when Dialog is Open
我正在构建一个应用程序,它有 2 个打开的对话框,如果用户在某些对话框打开时按下后退按钮,我希望发生一些事情。但是,由于某种原因,当对话框打开时,后退按钮事件没有注册。我通过在 onBackPressed() 中放置一个日志来测试它,每当对话未打开并且我只是在主 activity 上时,日志就会出现在 logcat 上。然而,如果对话是开放的,我只是得到这个:
W/InputEventReceiver: Attempted to finish an input event but the input event receiver has already been disposed.
下面我放置了对话代码:
public void pair() {
final Set<BluetoothDevice> pairedDevices = mBluetoothAdapter.getBondedDevices();
AlertDialog.Builder pairedList = new AlertDialog.Builder(this);
pairedList.setTitle("Paired Devices");
final ArrayAdapter<String> arrayAdapter = new ArrayAdapter<String>(this, android.R.layout.select_dialog_singlechoice);
if (pairedDevices.size() > 0) {
for (BluetoothDevice device : pairedDevices) {
arrayAdapter.add(device.getName());
}
}
pairedList.setNegativeButton("cancel", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
dialog.dismiss();
mBluetoothAdapter.disable();
// pair_dialog = false;
}
});
pairedList.setPositiveButton("Pair New", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
startActivityForResult(new Intent(android.provider.Settings.ACTION_BLUETOOTH_SETTINGS), 0);
}
});
pairedList.setAdapter(arrayAdapter, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
// connect_dialog = true;
String strName = arrayAdapter.getItem(which);
AlertDialog.Builder builderInner = new AlertDialog.Builder(MainActivity.this);
builderInner.setMessage(strName);
builderInner.setTitle("Connect To:");
builderInner.setPositiveButton("Ok", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
for (BluetoothDevice device : pairedDevices) {
if(device.getName().equals(strName)){
paired = device;
dialog.dismiss();
}
}
}
});
builderInner.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
// connect_dialog = false;
pairedList.show();
}
});
builderInner.show();
}
});
pairedList.show();
// pair_dialog = true;
}
下面是我的 onBackPressed() 方法,紧接在上述方法之后。没什么不正常的,我不觉得。
@Override
public void onBackPressed() {
Log.e(TAG, "Back Button Pressed");
super.onBackPressed();
}
就像我说的,如果对话没有打开,日志在 logcat 中显示得很好,但如果对话打开,就好像后退按钮没有注册。
如果你有添加,
dialog.setCancelable(false);
改成,
dialog.setCancelable(true);
实际上,setCancelable(false) 取消了对话框外的触摸事件,也取消了按下事件。
这对我有用...
yuordialog.setOnKeyListener(new Dialog.OnKeyListener() {
@Override
public boolean onKey(DialogInterface arg0, int keyCode,
KeyEvent event) {
if (keyCode == KeyEvent.KEYCODE_BACK) {
//your stuff....
}
return true;
}
});
您也可以使用
builder.setOnDismissListener(new DialogInterface.OnDismissListener() {
@Override
public void onDismiss(DialogInterface dialogInterface) {
//your dismiss code here
}
});
这会监听后压事件和触摸关闭。
我正在构建一个应用程序,它有 2 个打开的对话框,如果用户在某些对话框打开时按下后退按钮,我希望发生一些事情。但是,由于某种原因,当对话框打开时,后退按钮事件没有注册。我通过在 onBackPressed() 中放置一个日志来测试它,每当对话未打开并且我只是在主 activity 上时,日志就会出现在 logcat 上。然而,如果对话是开放的,我只是得到这个:
W/InputEventReceiver: Attempted to finish an input event but the input event receiver has already been disposed.
下面我放置了对话代码:
public void pair() {
final Set<BluetoothDevice> pairedDevices = mBluetoothAdapter.getBondedDevices();
AlertDialog.Builder pairedList = new AlertDialog.Builder(this);
pairedList.setTitle("Paired Devices");
final ArrayAdapter<String> arrayAdapter = new ArrayAdapter<String>(this, android.R.layout.select_dialog_singlechoice);
if (pairedDevices.size() > 0) {
for (BluetoothDevice device : pairedDevices) {
arrayAdapter.add(device.getName());
}
}
pairedList.setNegativeButton("cancel", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
dialog.dismiss();
mBluetoothAdapter.disable();
// pair_dialog = false;
}
});
pairedList.setPositiveButton("Pair New", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
startActivityForResult(new Intent(android.provider.Settings.ACTION_BLUETOOTH_SETTINGS), 0);
}
});
pairedList.setAdapter(arrayAdapter, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
// connect_dialog = true;
String strName = arrayAdapter.getItem(which);
AlertDialog.Builder builderInner = new AlertDialog.Builder(MainActivity.this);
builderInner.setMessage(strName);
builderInner.setTitle("Connect To:");
builderInner.setPositiveButton("Ok", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
for (BluetoothDevice device : pairedDevices) {
if(device.getName().equals(strName)){
paired = device;
dialog.dismiss();
}
}
}
});
builderInner.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
// connect_dialog = false;
pairedList.show();
}
});
builderInner.show();
}
});
pairedList.show();
// pair_dialog = true;
}
下面是我的 onBackPressed() 方法,紧接在上述方法之后。没什么不正常的,我不觉得。
@Override
public void onBackPressed() {
Log.e(TAG, "Back Button Pressed");
super.onBackPressed();
}
就像我说的,如果对话没有打开,日志在 logcat 中显示得很好,但如果对话打开,就好像后退按钮没有注册。
如果你有添加,
dialog.setCancelable(false);
改成,
dialog.setCancelable(true);
实际上,setCancelable(false) 取消了对话框外的触摸事件,也取消了按下事件。
这对我有用...
yuordialog.setOnKeyListener(new Dialog.OnKeyListener() {
@Override
public boolean onKey(DialogInterface arg0, int keyCode,
KeyEvent event) {
if (keyCode == KeyEvent.KEYCODE_BACK) {
//your stuff....
}
return true;
}
});
您也可以使用
builder.setOnDismissListener(new DialogInterface.OnDismissListener() {
@Override
public void onDismiss(DialogInterface dialogInterface) {
//your dismiss code here
}
});
这会监听后压事件和触摸关闭。