Android - 为AlertDialog创建可重用的方法

时间:2015-02-23 10:40:20

标签: android

我尝试使用可重用的方法创建一个类(classCommon),我计划将其用于不同的项目。

现在我遇到的问题是我想在该类中为YES \ NO AlertDialog创建一个可重用的方法。

所以基本上我需要编码 -an common / reusable AlertDialog返回" true" (如果点击是)或"假""如果没有被点击
- 主要应用程序必须等待用户实际做出选择

我的测试中也感受到了#34;主要代码"不会等到用户做出选择 - 不知怎的,AlertBox似乎运行asyncroniosly(?) - 所以无论用户在AlertDialog中选择什么,主代码都将被执行?

所以我的问题是: 我希望能够编写这样的代码:

main code:

//in my main code / Activity:
//call AlertDialog (MessageboxYESNO ) and *wait* til user made a selection, then contine
if ( classCommon._MessageboxYESNO ("Do you want to repeat the game ?","",myActivity.this) == true )
    { //do something if user select YES }
else
{   //do something if user select NO}
...

在classCommon中我创建了一个显示YES \ NO AlertDialog的方法 - 但我没有'知道如何将true / false返回给调用(主)代码。

classCommon :(我到目前为止)

public static void (boolean?) _MessageboxYESNO(String sTitle, String sMessage, final Context myContext)
    {
    AlertDialog.Builder builder = new AlertDialog.Builder(myContext);

        builder
                .setTitle(sTitle)
                .setMessage(sMessage)
                .setCancelable(false)                                
                .setPositiveButton("Yes", new DialogInterface.OnClickListener() {
                    public  void  (boolean?) onClick(DialogInterface dialog, int which) {
                        //Yes button clicked, do something

                        //return true ?
                    }
                })

                .setNegativeButton("No", new DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface dialog, int which) {
                        //no button clicked, do something

                        //return false ?
                     }
                })
                .show();

        //return  true/false ?

    }

1 个答案:

答案 0 :(得分:3)

我认为这是不可能的,因为它取决于事件。

但我会给你一个替代方案:用2种方法创建一个接口:OnYesClicked()和OnNoClicked()。然后你应该将这个接口实现到你调用MessageBox方法的类中,并传递"这个"作为参数。

自定义界面:

public interface MyInterface{

   public void onYesClicked();

   public void onNoClicked();
}

这样,当您单击是或否时,您可以执行某些操作。

public static void _MessageboxYESNO(String sTitle, String sMessage, final Context myContext, MyInterface myInterface)
{
AlertDialog.Builder builder = new AlertDialog.Builder(myContext);

    builder
            .setTitle(sTitle)
            .setMessage(sMessage)
            .setCancelable(false)                                
            .setPositiveButton("Yes", new DialogInterface.OnClickListener() {
                public  void onClick(DialogInterface dialog, int which) {
                    //Yes button clicked, do something

                    myInterface.onYesClicked();
                }
            })

            .setNegativeButton("No", new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog, int which) {
                    //no button clicked, do something

                     myInterface.onNoClicked();
                 }
            })
            .show();
}

这就是你的电话:

public class AClass implements MyInterface{

    @Override
    public void onYesClicked(){
       //Do Something
   }

   @Override
    public void onNoClicked(){
        //Do other thing
    }

    public void openDialog()
    {
     classCommon._MessageboxYESNO ("Do you want to repeat the game ?","",myActivity.this,this);
    }

}
相关问题