首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Android-AlertDialog -不工作

Android-AlertDialog -不工作
EN

Stack Overflow用户
提问于 2015-08-02 09:38:52
回答 1查看 2K关注 0票数 0

昨天,我通过助手编程,用我的安卓手机编写了一个小AlertDialog。在互联网上我找到了源代码

代码语言:javascript
复制
AlertDialog alertDialog = new AlertDialog.Builder(AlertDialogActivity.this).create();
// Setting Dialog Title
alertDialog.setTitle("Alert Dialog");
// Setting Dialog Message
alertDialog.setMessage("Welcome to AndroidHive.info");
// Setting Icon to Dialog
alertDialog.setIcon(R.drawable.tick);
// Setting OK Button
alertDialog.setButton("OK", new DialogInterface.OnClickListener() {
    public void onClick(DialogInterface dialog, int which) {
        // Write your code here to execute after dialog closed
        Toast.makeText(getApplicationContext(), "You clicked on OK", Toast.LENGTH_SHORT).show();
    }
});
// Showing Alert Message
alertDialog.show();

我已经在AIDE中测试过它,它正在工作,然后我在AndroidStudio中测试了它,但是它没有工作。为什么它在AIDE而不是Android演播室工作呢?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2015-08-02 09:54:36

代码语言:javascript
复制
AlertDialog.Builder alertDialog = new AlertDialog.Builder(
                AlertDialogActivity.this);

按照上面的方式编辑第一行,因为您需要在创建alertDialog之前设置生成器

完整代码如下:

代码语言:javascript
复制
AlertDialog.Builder alertDialog = new AlertDialog.Builder(
                AlertDialogActivity.this);
// Setting Dialog Title
alertDialog.setTitle("Alert Dialog");

// Setting Dialog Message
alertDialog.setMessage("Welcome to AndroidHive.info");

// Setting Icon to Dialog
alertDialog.setIcon(R.drawable.tick);

// Setting OK Button
alertDialog.setButton("OK", new DialogInterface.OnClickListener() {
        public void onClick(DialogInterface dialog, int which) {
        // Write your code here to execute after dialog closed
        Toast.makeText(getApplicationContext(), "You clicked on OK", Toast.LENGTH_SHORT).show();
        }
});
AlertDialog alertDialogMain = alertDialog.create();

// Showing Alert Message
alertDialogMain.show();
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/31770560

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档