如何在单击按钮时获得简单的警报?

发布于 2024-12-27 15:10:08 字数 1478 浏览 2 评论 0原文

我正在玩一个简单的应用程序,我想在单击按钮时显示警报。

到目前为止我的代码是:

package max.helloworld.firstapp;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.DialogInterface.OnClickListener;

public class HelloWorldActivity extends Activity {
/** Called when the activity is first created. */

private Button closeButton;


@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    this.setContentView(R.layout.main);
    this.closeButton = (Button)this.findViewById(R.id.close);
    this.closeButton.setKeyListener(new onClickListener() {
        @Override
        public void onClick(View v) { 
            AlertDialog alertDialog = new AlertDialog.Builder(this).create();               
            alertDialog.setTitle("Transformers");
            alertDialog.setMessage("Optimus Prime");
            alertDialog.setButton("OK", new OnClickListener(){
                public void onClick(DialogInterface dialog, int which){
                }
            });
            //alertDialog.setIcon(R.drawable.icon);
            alertDialog.show();
            finish();
        }
    }); 
}
}

问题在于它说 AlertDialogalertDialog = new AlertDialog.Builder(this).create();

它说构造函数未定义。请问我该如何解决这个问题?

I was playing around with a simple app and I want to show an alert when a button is clicked.

The code I have so far is:

package max.helloworld.firstapp;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.DialogInterface.OnClickListener;

public class HelloWorldActivity extends Activity {
/** Called when the activity is first created. */

private Button closeButton;


@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    this.setContentView(R.layout.main);
    this.closeButton = (Button)this.findViewById(R.id.close);
    this.closeButton.setKeyListener(new onClickListener() {
        @Override
        public void onClick(View v) { 
            AlertDialog alertDialog = new AlertDialog.Builder(this).create();               
            alertDialog.setTitle("Transformers");
            alertDialog.setMessage("Optimus Prime");
            alertDialog.setButton("OK", new OnClickListener(){
                public void onClick(DialogInterface dialog, int which){
                }
            });
            //alertDialog.setIcon(R.drawable.icon);
            alertDialog.show();
            finish();
        }
    }); 
}
}

The issue is where it says AlertDialog alertDialog = new AlertDialog.Builder(this).create();

It says the constructor is undefined. How do I fix that please?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

剩余の解释 2025-01-03 15:10:08

将此行更改

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

为:

`AlertDialog alertDialog = new AlertDialog.Builder(yourActivityName.this);`

Change this line:

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

To:

`AlertDialog alertDialog = new AlertDialog.Builder(yourActivityName.this);`
妄想挽回 2025-01-03 15:10:08

关键字 this 解析为该范围内的 OnClickListener 实例。要引用回您的活动实例,请将 this 替换为 HelloWorldActivity.this

The keyword this resolves to the instance of OnClickListener at that scope. To refer back to your activity instance replace this with HelloWorldActivity.this

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文