简体   繁体   English

Android AlertDialog按钮单击

[英]Android AlertDialog button click

I got an AlertDialog that has one positive and negative button each. 我有一个AlertDialog,每个都有一个正面和负面的按钮。 Within the onClick(DialogInterface dialog, int item) event handler, which is a member function of Activity, how do I tell which button has been clicked? 在onClick(DialogInterface对话框,int item)事件处理程序中,它是Activity的成员函数,如何判断哪个按钮被单击? Base on my observation, the "item" parameter has -1 as value if positive button is clicked, or -2 if it's negative button. 根据我的观察,如果单击正按钮,则“item”参数的值为-1,如果是负按钮则为-2。 However, I couldn't find any information on such from API doc hence I feel that this could be broken at any time. 但是,我无法从API文档中找到任何关于此类信息的信息,因此我觉得这可以随时打破。

AlertDialog's positive and negative buttons use the DialogInterface.OnClickListener interface to respond to selection. AlertDialog的正面和负面按钮使用DialogInterface.OnClickListener接口来响应选择。 Like you said, the onClick method of this callback looks like this: 就像你说的,这个回调的onClick方法如下所示:

public void onClick(DialogInterface dialog, int which) {
  // ...
}

The which parameter indicates which button was clicked and can have, among others, the following values: 1) AlertDialog.BUTTON_POSITIVE (-1) 2) AlertDialog.BUTTON_NEGATIVE (-2) which参数指示单击了哪个按钮,并且可以具有以下值:1)AlertDialog.BUTTON_POSITIVE(-1)2)AlertDialog.BUTTON_NEGATIVE(-2)

So, you clicked the positive button if which is -1 and the negative if which is -2. 因此,如果为-1,则单击正按钮,如果为-2,则单击否定按钮。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM