2011-05-15 108 views
0

这可能看起来像一个简单的问题来解决,但我是Android的新手,请耐心等待。我有以下代码片段,显示警告框:关闭AlertDialog框

Builder pwBox = new AlertDialog.Builder(this); 
    AlertDialog pwDialog; 
    LayoutInflater mInflater = (LayoutInflater) this 
      .getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
    View pwView = mInflater.inflate(R.layout.passworddialog, null); 

    Button btnSetPassword = (Button) pwView 
      .findViewById(R.id.btnSetPassword); 

    pwBox.setView(pwView); 
    pwBox.setCancelable(false); 
    pwBox.setTitle("New Password"); 
    pwDialog = pwBox.create(); 

    btnSetPassword.setOnClickListener(new OnClickListener() { 
     @Override 
     public void onClick(View v) { 

      //pwDialog.dismiss(); <------ Problem Line 
     } 
    }); 

    pwDialog.show(); 

一切正常。问题是,我无法访问“pwDialog”变量,所以如何关闭对话框?

回答

1

它看起来好像你应该能够访问你的pwDialog变量。您可能需要将其宣布为final。

final AlertDialog pwDialog = pwBox.create(); 
+0

这是我的问题。我需要添加最后的关键字,现在它可以工作。谢谢! – Icemanind 2011-05-15 20:41:40

0

你想要的那种东西:

private static final CommandWrapper DISMISS = new CommandWrapper(Command.NO_OP); 

public static AlertDialog createDeletionDialog(final Context context, 
    final String message, final String positiveLabel, final Command positiveCommand) { 

    AlertDialog.Builder builder = new AlertDialog.Builder(context); 
    builder.setCancelable(true); 
    builder.setMessage(message); 

    builder.setInverseBackgroundForced(true); 
    builder.setPositiveButton(positiveLabel, new CommandWrapper(positiveCommand)); 
    builder.setNeutralButton("Cancel", DISMISS); 
    return builder.create(); 
}