何时使用此警告对话框或上下文

时间:2015-08-17 20:13:44

标签: android alertdialog android-context

我正在尝试创建一个警告对话框,但在声明的括号中, 在它所说的行:

AlertDialog.Builder alertDialog=new AlertDialog.Builder(this).create();

(this)导致错误。我尝试将其更改为上下文,还有更多错误。救命?

import android.app.Activity;
import android.app.AlertDialog.Builder;
import android.content.DialogInterface;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.app.AlertDialog;

public class userPreferences extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.userpreferences);
    EditText editText=(EditText)findViewById(R.id.editText);
    Button save=(Button)findViewById(R.id.button);


  save.setOnClickListener(new View.OnClickListener() {
      @Override
      public void onClick(View v) {
          AlertDialog.Builder alertDialog=new AlertDialog.Builder(this).create();
                  alertDialog.setTitle("Alert Dialog");
                  alertDialog.setMessage("Are you sure you want to save?");
                  alertDialog.setPositiveButton("Yes", new DialogInterface.OnClickListener() {
                      @Override
                      public void onClick(DialogInterface dialog, int which) {

                      }
                  });
                  alertDialog.setNegativeButton("No", null);
                  alertDialog.show();


      }
  });

}
}

4 个答案:

答案 0 :(得分:2)

alertDialog应声明为final OnClickListener之外:

final AlertDialog.Builder alertDialog = new AlertDialog.Builder(this);

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

        alertDialog.setTitle("Alert Dialog");
        alertDialog.setMessage("Are you sure you want to save?");
        alertDialog.setPositiveButton("Yes", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                ...
            }
        });
        alertDialog.setNegativeButton("No", null);
        alertDialog.show();
    }
});

希望这有帮助。

答案 1 :(得分:1)

this是对当前正在运行的实例的引用。您可以使用this传递您的活动(继承自Context,从而为什么我们可以将Activity参数传递给Context参数)到AlertDialog.Builder ,如果它在听众之外。

定义匿名内部类时,如:

  

new View.OnClickListener() { ... }

this引用更改为OnClickListener

的实例

AlertDialog.Builder()预计会Context!由于它是一个匿名的内部类,我们可以简单地使用userPreferences.this(因为userPreferences仍然在范围内)来获得正确的引用。

答案 2 :(得分:1)

点击监听器的内部将引用点击监听器

    save.setOnClickListener(new View.OnClickListener() {
      @Override
      public void onClick(View v) {
                  AlertDialog.Builder alertDialog=new AlertDialog.Builder(userPrefereces.this).create();
                  alertDialog.setTitle("Alert Dialog");
                  alertDialog.setMessage("Are you sure you want to save?");
                  alertDialog.setPositiveButton("Yes", new DialogInterface.OnClickListener() {
                      @Override
                      public void onClick(DialogInterface dialog, int which) {

                      }
                  });
                  alertDialog.setNegativeButton("No", null);
                  alertDialog.show();


      }
  });

但您可以通过

获取活动的背景信息
userPreferences.this

答案 3 :(得分:0)

在onClick方法之外创建构建器

@Override
protected void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  setContentView(R.layout.userpreferences);
  EditText editText=(EditText)findViewById(R.id.editText);
  Button save=(Button)findViewById(R.id.button);

  final AlertDialog.Builder alertDialog=new AlertDialog.Builder(this).create();

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

                alertDialog.setTitle("Alert Dialog");
                alertDialog.setMessage("Are you sure you want to save?");
                alertDialog.setPositiveButton("Yes", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {

                    }
                });
                alertDialog.setNegativeButton("No", null);
                alertDialog.show();
    }
  });
}