如何获得与按下按钮相关的数据?

时间:2012-04-11 06:21:55

标签: android android-alertdialog android-button

当用户选择要删除的项目时,我打开一个alertDialog以确保他要删除它。当我获得用于BUTTON_POSITIVE的onClick时,我需要获取要删除的文件名。这在打开alertDialog之前就已知道了。我尝试使用setButton(BUTTON_POSITIVE,“是”,msg)并在邮件中设置文件名,但我不清楚我在哪里收到邮件?有人可以澄清或提出更好的实施吗?

这是单击删除按钮后运行的代码:

public void OnClick(View v)
{
    Button b = (Button)v;
    CharSequence cs = b.getText();
    int id = Integer.parseInt(cs.toString());

    AlertDialog alertDialog;
    Message msg = new Message();
    msg.arg1 = id;

    AlertDialog deleteAlert = new AlertDialog.Builder(this).create();
    deleteAlert.setTitle("Trigger Remove Alert");
    deleteAlert.setMessage("Are you sure you want to remove this trigger?");
    deleteAlert.setButton(AlertDialog.BUTTON_POSITIVE, "Yes", msg);
    deleteAlert.setButton(AlertDialog.BUTTON_POSITIVE, "Yes", new OnClickListener(){

    @Override
    public void onClick(DialogInterface dialog, int which) {               
    //remove Trigger
        //boolean b = RemoveTriggerPOI(aid, id);
    }
    });
    deleteAlert.setButton2("No", new OnClickListener(){

    @Override
    public void onClick(DialogInterface dialog, int which) {
    //...
    }
    });
    deleteAlert.show();



}

1 个答案:

答案 0 :(得分:0)

首先,将您的idcs定义为:

final int id = Integer.parseInt(cs.toString());
final CharSequence cs = b.getText();

现在只需在对话框按钮的 onClick 监听器中使用csid

 deleteAlert.setButton(AlertDialog.BUTTON_POSITIVE, "Yes", new OnClickListener(){

    @Override
    public void onClick(DialogInterface dialog, int which) {               
       //remove Trigger
       //boolean b = RemoveTriggerPOI(aid, id);

       //SIMPLY USE id OR cs HERE. IT SHOULD WORK
    }
    });