View.OnClickListener mButtonHandler = new View.OnClickListener() { |
public void onClick(View v) { |
Message m = null ; |
if (v == mButtonPositive && mButtonPositiveMessage != null ) { |
m = Message.obtain(mButtonPositiveMessage); |
} else if (v == mButtonNegative && mButtonNegativeMessage != null ) { |
m = Message.obtain(mButtonNegativeMessage); |
} else if (v == mButtonNeutral && mButtonNeutralMessage != null ) { |
m = Message.obtain(mButtonNeutralMessage); |
} |
if (m != null ) { |
m.sendToTarget(); |
} |
// Post a message so we dismiss after the above handlers are executed |
mHandler.obtainMessage(ButtonHandler.MSG_DISMISS_DIALOG, mDialogInterface) |
.sendToTarget(); |
} |
}; //源代码片段来自云代码http://yuncode.net |
|
private static final class ButtonHandler extends Handler { |
// Button clicks have Message.what as the BUTTON{1,2,3} constant |
private static final int MSG_DISMISS_DIALOG = 1 ; |
private WeakReference<DialogInterface> mDialog; |
public ButtonHandler(DialogInterface dialog) { |
mDialog = new WeakReference<DialogInterface>(dialog); |
} |
@Override |
public void handleMessage(Message msg) { |
switch (msg.what) { |
case DialogInterface.BUTTON_POSITIVE: |
case DialogInterface.BUTTON_NEGATIVE: |
case DialogInterface.BUTTON_NEUTRAL: |
((DialogInterface.OnClickListener) msg.obj).onClick(mDialog.get(), msg.what); |
break ; |
case MSG_DISMISS_DIALOG: |
((DialogInterface) msg.obj).dismiss(); |
} |
} |
} //源代码片段来自云代码http://yuncode.net |
|