需要在 Android 中创建 AlertDialog 的建议

发布于 2024-12-07 17:17:59 字数 1246 浏览 0 评论 0原文

当在我的活动中按下按钮时,我会调用以下代码。

AlertDialog.Builder alertDialog = new AlertDialog.Builder(this).create();  
            alertDialog.setTitle("Alert 1");  
            alertDialog.setMessage("This is an alert");  
            alertDialog.setButton("OK", new DialogInterface.OnClickListener() {  
              public void onClick(DialogInterface dialog, int which) {  
                return;  
            } });  

我已经添加了这两个导入:

import android.app.AlertDialog;
import android.app.AlertDialog.Builder;

但仍然出现以下错误:

Description Resource    Path    Location    Type
The constructor AlertDialog.Builder(new View.OnClickListener(){}) is undefined  MobileTrackerActivity.java  /MobileTracker/src/com/example/mobiletracker    line 77 Java Problem
The method setButton(String, new DialogInterface.OnClickListener(){}) is undefined for the type AlertDialog.Builder MobileTrackerActivity.java  /MobileTracker/src/com/example/mobiletracker    line 80 Java Problem
Type mismatch: cannot convert from AlertDialog to AlertDialog.Builder   MobileTrackerActivity.java  /MobileTracker/src/com/example/mobiletracker    line 77 Java Problem

任何人都可以给我任何解决方案吗?我对 Java 很陌生。

I have the following code called when a button is pressed in my activity.

AlertDialog.Builder alertDialog = new AlertDialog.Builder(this).create();  
            alertDialog.setTitle("Alert 1");  
            alertDialog.setMessage("This is an alert");  
            alertDialog.setButton("OK", new DialogInterface.OnClickListener() {  
              public void onClick(DialogInterface dialog, int which) {  
                return;  
            } });  

I have added imports these two :

import android.app.AlertDialog;
import android.app.AlertDialog.Builder;

But still get following errors :

Description Resource    Path    Location    Type
The constructor AlertDialog.Builder(new View.OnClickListener(){}) is undefined  MobileTrackerActivity.java  /MobileTracker/src/com/example/mobiletracker    line 77 Java Problem
The method setButton(String, new DialogInterface.OnClickListener(){}) is undefined for the type AlertDialog.Builder MobileTrackerActivity.java  /MobileTracker/src/com/example/mobiletracker    line 80 Java Problem
Type mismatch: cannot convert from AlertDialog to AlertDialog.Builder   MobileTrackerActivity.java  /MobileTracker/src/com/example/mobiletracker    line 77 Java Problem

Can anybody give any solution to me please? I am very new to Java.

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

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

发布评论

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

评论(2

两相知 2024-12-14 17:17:59

这样创建 AlertDilog :

   AlertDialog.Builder alertDialog = new AlertDialog.Builder(this);  
   alertDialog.setTitle("Alert 1");  
   alertDialog.setMessage("This is an alert");  
   alertDialog.setButton("OK", new DialogInterface.OnClickListener() {  
   public void onClick(DialogInterface dialog, int which) {  
        return;  
   } });  
   AlertDialog alert = alertDialog.create();
   alert.show();

Create the AlertDilog this way :

   AlertDialog.Builder alertDialog = new AlertDialog.Builder(this);  
   alertDialog.setTitle("Alert 1");  
   alertDialog.setMessage("This is an alert");  
   alertDialog.setButton("OK", new DialogInterface.OnClickListener() {  
   public void onClick(DialogInterface dialog, int which) {  
        return;  
   } });  
   AlertDialog alert = alertDialog.create();
   alert.show();
素手挽清风 2024-12-14 17:17:59

如果您查看此页面:

http://developer.android.com/guide/ topic/ui/dialogs.html

和您收到的错误消息您只需要进行以下更改:

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

If you look at this page:

http://developer.android.com/guide/topics/ui/dialogs.html

and the error message you get you just need to make this change:

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