【问题标题】:Dialog Helper class how to callDialog Helper类如何调用
【发布时间】:2015-08-18 16:19:47
【问题描述】:

我想在这里寻求建议,我想为退出框创建一个对话框,因为几乎每个屏幕都可以调用此框,我想创建一个包含此方法的新类并调用它每次单击按钮时,但老实说我不知道​​该怎么做,我创建了一个名为 SignOutHelper 的类

public class SignOutHelper extends Activity {

public Dialog OnCreateDialog(Bundle savedInstanceState) {

    AlertDialog.Builder builder = new AlertDialog.Builder(this);
    builder.setMessage("do you wish to sign out?");
    builder.setTitle("Sign Out");

    builder.setPositiveButton("ok", new DialogInterface.OnClickListener() {
        public void onClick(DialogInterface dialog, int id) {

        }
    });

    builder.setNegativeButton("Cancel", new   DialogInterface.OnClickListener(){
        public void onClick(DialogInterface dialog, int id){

        }
    });
    AlertDialog dialog = builder.create();   
    return builder.create();
}
}

现在我想从正常活动中调用这个类,但我不知道该怎么做,感谢您的帮助,谢谢

【问题讨论】:

    标签: android android-activity dialog


    【解决方案1】:

    尝试制作静态方法来创建Dialog。

    public class SignOutHelper {
    
    public static Dialog CreateDialog(Context c) {
    
        AlertDialog.Builder builder = new AlertDialog.Builder(c);
        builder.setMessage("do you wish to sign out?");
        builder.setTitle("Sign Out");
    
        builder.setPositiveButton("ok", new DialogInterface.OnClickListener() {
            public void onClick(DialogInterface dialog, int id) {
    
            }
        });
    
        builder.setNegativeButton("Cancel", new   DialogInterface.OnClickListener(){
            public void onClick(DialogInterface dialog, int id){
    
            }
        });
        AlertDialog dialog = builder.create();   
        return builder.create();
       }
    }
    

    【讨论】:

    • 谢谢,如何从我的主要活动中调用该方法?
    • SignOutHelper.CreateDialog(this).show();SignOutHelper.CreateDialog(MainActivity.this).show();
    • 您将如何关闭/取消此对话框以防止设备旋转时窗口泄漏?
    【解决方案2】:

    你可以这样做。在您的助手类中创建一个静态方法,然后在您的按钮单击中调用该方法。

    SignOutHelper.java

    public class SignOutHelper {
    
        public static void showDialog(Context con){
            AlertDialog.Builder builder = new AlertDialog.Builder(con);
            builder.setMessage("do you wish to sign out?");
            builder.setTitle("Sign Out");
    
            builder.setPositiveButton("ok", new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog, int id) {
    
                }
            });
    
            builder.setNegativeButton("Cancel", new   DialogInterface.OnClickListener(){
                public void onClick(DialogInterface dialog, int id){
    
                }
            });
            AlertDialog dialog = builder.create();
            dialog.show();
        }
    }
    

    像这样调用按钮点击中的方法。

    在您的活动文件中,

    Button dialogButton = (Button) findViewById(R.id.button);
    dialogButton.setOnClickListener(new OnClickListener() {
        @Override
        public void onClick(View v) {
           SignOutHelper.showDialog(MainActivity.this); 
           }
    });
    

    就是这样......干杯!

    【讨论】:

    • 嗨,我收到一个异常 android.view.WindowManager$BadTokenException: Unable to add window -- token null 不适用于应用程序,知道为什么吗?
    • 我更改了上面代码中的上下文。这段代码现在可以工作了。 getApplicationContext() 更改为“MainActivity.this”。 @Juancadiyjohndiy
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2020-12-15
    • 1970-01-01
    • 1970-01-01
    • 2011-08-18
    • 1970-01-01
    • 1970-01-01
    • 2021-07-23
    相关资源
    最近更新 更多