Android 覆盖AlertDialog里的按钮事件并显示Toast
网上找到的多数是:
控制其不消失,和消失。
1
// /** 假设对话框已经关闭,欺骗系统,以保持输入窗口**/
2
try {
3
Field field = this.getClass().getSuperclass().getDeclaredField( "mShowing" );
4
field.setAccessible( true );
5
field.set(this, bSucceed);
6
} catch (Exception e){
7
e.printStackTrace();
8
}
还可以
用OnClicklistener覆盖DialogInte易做图ce.OnClicklistener
01
//可以使用AlerDialog.Builder,并显示一个Toast。//只要你覆盖按钮的OnClickListener,就可以触发Toast,显示在对话框上。//并决定对话框是否消失 public void showToastOnDialog(final Context context) {
02
AlertDialog.Builder builder = new AlertDialog.Builder(context);
03
builder.setTitle("Dialog title");
04
builder.setMessage("Dialog message");
05
builder.setPositiveButton(android.R.string.ok, new DialogInte易做图ce.OnClickListener() {
06
@Override
07
public void onClick(DialogInte易做图ce dialog, int which) {
08
// Do nothing, you will be overriding this anyway
09
}
10
});
11
builder.setNegativeButton(android.R.string.cancel,
12
new DialogInte易做图ce.OnClickListener() {
13
@Override
14
public void onClick(DialogInte易做图ce dialog, int which) {
15
// You can implement code here, because you wont be
16
// overriding this
17
}
18
});
19
final AlertDialog dialog = builder.create();
20
// Make sure you show the dialog first before overriding the
21
// OnClickListener
22
dialog.show();
23
// Notice that I`m not using DialogInte易做图ce.OnClicklistener but the
24
// View.OnClickListener
25
dialog.getButton(Dialog.BUTTON_POSITIVE).setOnClickListener(
26
new View.OnClickListener() {
27
28
@Override
29
public void onClick(View v) {
30
31
Toast toast = Toast.makeText(context,
32
"I`m a toast on top of a dialog.",
33
Toast.LENGTH_LONG);
34
toast.show();
35
// Because you are overriding the OnClicklistener, the
36
// dialog will not auto dismiss after clicking
37
////otherwise //dialog.dismiss();
38
dialog.dismiss();
39
}
40
});
41
}
补充:移动开发 , Android ,