2011-05-15 103 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(); 
}