简体   繁体   English

何时在“警报对话框”中使用此内容或上下文

[英]when to use this or context with Alert Dialog

I am trying to create an alert dialog but in the bracket in the declaration, on the line where it says : 我正在尝试创建一个警报对话框,但在声明的括号中,该行显示:

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

the (this) causes errors. (这)会导致错误。 I tried changing it to context, still more errors. 我尝试将其更改为上下文,仍然有更多错误。 HELP? 救命?

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();


      }
  });

}
}

alertDialog should be declared as final outside OnClickListener : 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();
    }
});

Hope this helps. 希望这可以帮助。

this is a reference to the currently running instance. this是对当前正在运行的实例的引用。 You could use this to pass your Activity (which inherits from Context , thus why we can pass an Activity reference to a Context parameter) to the AlertDialog.Builder , if it's outside of the listener. 你可以使用this来传递您的活动(从继承Context ,因此为什么我们可以通过一个Activity参照Context参数)到AlertDialog.Builder ,如果它的听众之外。

When you define an anonymous inner class, like: 当您定义匿名内部类时,例如:

new View.OnClickListener() { ... }

The this reference changes to the instance of OnClickListener . this参考更改为OnClickListener的实例。

AlertDialog.Builder() expects a Context though! AlertDialog.Builder()期望一个Context Since it's an anonymous inner class we can simply use userPreferences.this (because userPreferences is still in scope) to get the correct reference. 由于它是一个匿名内部类,因此我们可以简单地使用userPreferences.this (因为userPreferences仍在范围内)来获取正确的引用。

This inside of the click listener is going to reference the click listener 点击监听器的内部将引用点击监听器

    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();


      }
  });

but you can get the context of the activity by 但是您可以通过获取活动的上下文

userPreferences.this

Create the builder outside onClick method 在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();
    }
  });
}

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

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