打开一个对话框,当我点击一个按钮 [英] open a dialog when i click a button

查看:110
本文介绍了打开一个对话框,当我点击一个按钮的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个按钮,我想开一个对话框时,pressed。这是我的code:

 按钮更多=(按钮)findViewById(R.id.more);
more.setOnClickListener(新View.OnClickListener(){
    公共无效的onClick(视图查看){
        //意图myIntent =新的意向书(view.getContext(),agones.class);
        // startActivityForResult(myIntent,0);

        AlertDialog alertDialog =新AlertDialog.Builder(本).create();
        alertDialog.setTitle(HI);
        alertDialog.setMessage(这是我的应用程序);

        alertDialog.setButton(继续。,新DialogInterface.OnClickListener(){
        公共无效的onClick(DialogInterface对话,诠释它){
            //在这里你可以添加功能
        }
        });
    }
});
 

解决方案

由于@Roflcoptr说,你不叫 alertDialog.show()方法。因此你的对话不会出现。

这是你的编辑code:

 按钮更多=(按钮)findViewById(R.id.more);
more.setOnClickListener(新View.OnClickListener(){
    公共无效的onClick(视图查看){
        //意图myIntent =新的意向书(view.getContext(),agones.class);
        // startActivityForResult(myIntent,0);


        AlertDialog alertDialog =新AlertDialog.Builder(小于YourActivityName>这).create(); //读取更新
        alertDialog.setTitle(HI);
        alertDialog.setMessage(这是我的应用程序);

        alertDialog.setButton(继续。,新DialogInterface.OnClickListener(){
           公共无效的onClick(DialogInterface对话,诠释它){
              //在这里你可以添加功能
           }
        });

        alertDialog.show(); //<  - 看到这个!
    }

});
 

如果你写而不是< ActivityName>。这,那么它是要采取参考对 View.OnClickListener ,因为正在里面进行访问。你需要给你活动的名字在那里。

I have a button and I would like to open a dialog when pressed. This is my code:

Button more = (Button) findViewById(R.id.more);
more.setOnClickListener(new View.OnClickListener() {
    public void onClick(View view) {
        //Intent myIntent = new Intent(view.getContext(), agones.class);
        //startActivityForResult(myIntent, 0);

        AlertDialog alertDialog = new AlertDialog.Builder(this).create();
        alertDialog.setTitle("hi");
        alertDialog.setMessage("this is my app");

        alertDialog.setButton("Continue..", new DialogInterface.OnClickListener() {
        public void onClick(DialogInterface dialog, int which) {
            // here you can add functions
        }
        });
    }
});

解决方案

As @Roflcoptr has said, you haven't called alertDialog.show() method. thus your dialog doesn't appear.

Here's your edited code:

Button more = (Button) findViewById(R.id.more);
more.setOnClickListener(new View.OnClickListener() {
    public void onClick(View view) {
        //Intent myIntent = new Intent(view.getContext(), agones.class);
        //startActivityForResult(myIntent, 0);


        AlertDialog alertDialog = new AlertDialog.Builder(<YourActivityName>this).create(); //Read Update
        alertDialog.setTitle("hi");
        alertDialog.setMessage("this is my app");

        alertDialog.setButton("Continue..", new DialogInterface.OnClickListener() {
           public void onClick(DialogInterface dialog, int which) {
              // here you can add functions
           }
        });

        alertDialog.show();  //<-- See This!
    }

});

if you write this instead of <ActivityName>.this, then it is going to take the reference of View.OnClickListener since this is currently being accessed inside it. You need to give your Activity's name there.

这篇关于打开一个对话框,当我点击一个按钮的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆