如何在单独的类中编写一个函数来显示警告框

时间:2022-04-16 06:37:29
package test;
import test.DisplayAlert;


public class AlertCall
{     int j;

    public int dis2(String phoneNumber)
    {
      DisplayAlert ob = new DisplayAlert();     
      j=ob.dis1(AlertCall.this, phoneNumber);
      return j;
    }
  }

The class to display the dialog box

要显示对话框的类

package test;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.view.View;
import android.view.View.OnClickListener;

public class DisplayAlert extends Activity implements OnClickListener
{
    int t;

        public  int dis(Context activityContext, String destinationAddress)
        {
            new AlertDialog.Builder(activityContext).setTitle("SEND MESSAGE")
               .setMessage("Are you sure you want to send this msg to no ?   "+ destinationAddress)
        .setPositiveButton(android.R.string.yes, new DialogInterface.OnClickListener() {
            public void onClick(DialogInterface dialog, int which) 
                { 

                            t=0;
                  }
                })
            .setNegativeButton(android.R.string.no, new DialogInterface.OnClickListener() {
                 public void onClick(DialogInterface dialog, int which) { 

                       t=1;
               }
   })
  .setIcon(android.R.drawable.ic_dialog_alert)
   .show();
    return t;
}
    @Override
    public void onClick(View arg0) {
        // TODO Auto-generated method stub

    }
    public int dis1(AlertCall alertCall, String phoneNumber) {
        // TODO Auto-generated method stub
        new AlertDialog.Builder(alertCall).setTitle("SEND MESSAGE")
           .setMessage("Are you sure you want to send this msg to no ?   "+ phoneNumber)
 .setPositiveButton(android.R.string.yes, new DialogInterface.OnClickListener() {
     public void onClick(DialogInterface dialog, int which) 
         { 

                     t=0;
           }
         })
     .setNegativeButton(android.R.string.no, new DialogInterface.OnClickListener() {
          public void onClick(DialogInterface dialog, int which) { 

               t=1;
        }
})
.setIcon(android.R.drawable.ic_dialog_alert)
.show();
return t;
    }


}

I write a class to display Alert Box and call it through separate call but its not working.When I call the function from main_Activity then it is working but if I call from another class then there is an error can anyone please suggest.

我写了一个类来显示Alert Box并通过单独的调用来调用它但它不工作。当我从main_Activity调用该函数然后它正在工作但是如果我从另一个类调用然后有错误可以有人请建议。

4 个解决方案

#1


5  

Use callbacks

使用回调

public class AlertDialogHelper {

    public static void showAlert(Context context, final Callback callback) {
        new AlertDialog.Builder(context).setTitle("Some Title").setMessage("Some message")
                .setPositiveButton("Positive button", new DialogInterface.OnClickListener() {

                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        callback.onSucess(0);
                    }
                }).setNegativeButton("Negative button", new DialogInterface.OnClickListener() {

                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        callback.onSucess(-1);
                    }
                }).show();
    }

    public interface Callback {

        public void onSucess(int t);

    }

}


public class AlerCall extends Activity {

    private int j;

    public void dis() {
        AlertDialogHelper.showAlert(this, new AlertDialogHelper.Callback() {

            @Override
            public void onSucess(int t) {
                j = t;
            }
        });
    }

}

#2


0  

Try this it ll help ypu,if you want to create alert dialog not in mainActivity.

试试这个它会帮助ypu,如果你想在mainActivity中创建警告对话框。

Call this static method in your main activity like this..

在您的主要活动中调用此静态方法,如下所示。

        AlerBox.showNetworkAlert(MainActivity.this);

And in AlertBox Class

并在AlertBox类中

public class AlerBox {
public static void showNetworkAlert(final Context activity)
{


    AlertDialog.Builder alertDialog=new AlertDialog.Builder(activity);

    alertDialog.setTitle("KMP");
    alertDialog.setMessage("You don’t seem to have an active internet connection.");

    alertDialog.setPositiveButton("Settings", new DialogInterface.OnClickListener() {

        public void onClick(DialogInterface dialog, int which) {
            activity.startActivity(new Intent(Settings.ACTION_WIRELESS_SETTINGS));

        } 
    });



    alertDialog.setNegativeButton("Ok", new DialogInterface.OnClickListener() {

        public void onClick(DialogInterface dialog, int which) {


        } 
    });


    alertDialog.show();


}

}

}

#3


0  

Our Dialog Class

public class MyDialogBuilder 

{
    private Context context;

public AlertDialog getMyDialog(Context c,String message)
{
    this.context=c;
    AlertDialog.Builder builder = new AlertDialog.Builder((Activity)context);
    builder.setMessage(message).setTitle("Download");                               

    builder.setPositiveButton("POSITIVE_BUTTON", new OnClickListener() {

        @Override
        public void onClick(DialogInterface dialog, int which) 
        {
        //  do work on positive button press                
        }
    });

    builder.setNegativeButton("NEGATIVE _BUTTON", new OnClickListener() {

        @Override
        public void onClick(DialogInterface dialog, int which) 
        {
        //  do work on negative button press
        }
    });
    AlertDialog dialog = builder.create();
    return dialog;

}

}

calling part

AlertDialog dialog=new MyDialogBuilder().getMyDialog(Activity_Context,"msg_to_show");
            dialog.show();

#4


0  

You must pass context from Activity when call method

调用方法时,必须从Activity传递上下文

Simple solution is

简单的解决方案

DisplayAlert.java

DisplayAlert.java

public class AlertDisplay 
{

Context c;
public AlertDisplay(Context context) 
{
    c=context;
}
 public void DiplayAlert(String s)
{
    //Here Code to display alert
}

}

}

Call in main method like

像主要方法一样调用

AlertDispley ad = new AlertDispley(getApplicationContext())
ad.DiplayAlert("message");

#1


5  

Use callbacks

使用回调

public class AlertDialogHelper {

    public static void showAlert(Context context, final Callback callback) {
        new AlertDialog.Builder(context).setTitle("Some Title").setMessage("Some message")
                .setPositiveButton("Positive button", new DialogInterface.OnClickListener() {

                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        callback.onSucess(0);
                    }
                }).setNegativeButton("Negative button", new DialogInterface.OnClickListener() {

                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        callback.onSucess(-1);
                    }
                }).show();
    }

    public interface Callback {

        public void onSucess(int t);

    }

}


public class AlerCall extends Activity {

    private int j;

    public void dis() {
        AlertDialogHelper.showAlert(this, new AlertDialogHelper.Callback() {

            @Override
            public void onSucess(int t) {
                j = t;
            }
        });
    }

}

#2


0  

Try this it ll help ypu,if you want to create alert dialog not in mainActivity.

试试这个它会帮助ypu,如果你想在mainActivity中创建警告对话框。

Call this static method in your main activity like this..

在您的主要活动中调用此静态方法,如下所示。

        AlerBox.showNetworkAlert(MainActivity.this);

And in AlertBox Class

并在AlertBox类中

public class AlerBox {
public static void showNetworkAlert(final Context activity)
{


    AlertDialog.Builder alertDialog=new AlertDialog.Builder(activity);

    alertDialog.setTitle("KMP");
    alertDialog.setMessage("You don’t seem to have an active internet connection.");

    alertDialog.setPositiveButton("Settings", new DialogInterface.OnClickListener() {

        public void onClick(DialogInterface dialog, int which) {
            activity.startActivity(new Intent(Settings.ACTION_WIRELESS_SETTINGS));

        } 
    });



    alertDialog.setNegativeButton("Ok", new DialogInterface.OnClickListener() {

        public void onClick(DialogInterface dialog, int which) {


        } 
    });


    alertDialog.show();


}

}

}

#3


0  

Our Dialog Class

public class MyDialogBuilder 

{
    private Context context;

public AlertDialog getMyDialog(Context c,String message)
{
    this.context=c;
    AlertDialog.Builder builder = new AlertDialog.Builder((Activity)context);
    builder.setMessage(message).setTitle("Download");                               

    builder.setPositiveButton("POSITIVE_BUTTON", new OnClickListener() {

        @Override
        public void onClick(DialogInterface dialog, int which) 
        {
        //  do work on positive button press                
        }
    });

    builder.setNegativeButton("NEGATIVE _BUTTON", new OnClickListener() {

        @Override
        public void onClick(DialogInterface dialog, int which) 
        {
        //  do work on negative button press
        }
    });
    AlertDialog dialog = builder.create();
    return dialog;

}

}

calling part

AlertDialog dialog=new MyDialogBuilder().getMyDialog(Activity_Context,"msg_to_show");
            dialog.show();

#4


0  

You must pass context from Activity when call method

调用方法时,必须从Activity传递上下文

Simple solution is

简单的解决方案

DisplayAlert.java

DisplayAlert.java

public class AlertDisplay 
{

Context c;
public AlertDisplay(Context context) 
{
    c=context;
}
 public void DiplayAlert(String s)
{
    //Here Code to display alert
}

}

}

Call in main method like

像主要方法一样调用

AlertDispley ad = new AlertDispley(getApplicationContext())
ad.DiplayAlert("message");