Android - 为AlertDialog创建可重用方法

问题描述:

我尝试使用计划用于不同项目的可重用方法创建类(classCommon)。Android - 为AlertDialog创建可重用方法

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

所以基本上我需要编写
-an共同\可重复使用AlertDialog返回“真”(如果是被点击)或“假””如果NO被点击
-the主应用程序必须等待,直到用户实际已经作出了选择

我也有我的测试,“主代码”不会等待直到用户做出选择的感觉 - 不知何故AlertBox似乎运行异步(?) - 所以主代码将无论用户在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我已经创建了一个显示是\ 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 ? 

    } 
+0

我前段时间做了同样的事情。你需要创建一个接口。当你打电话给警报时,每个班级都会向你的班级打电话。 – rahultheOne 2015-02-23 10:45:58

+0

我们不能创建一个方法用于不同的项目,除非你创建自己的android库。我们只能创建一个方法用于同一个项目。 – 2015-02-23 10:46:54

我认为这是不可能的,因为它依赖于事件。但我会给你一个替代方案:使用2种方法创建一个接口:OnYesClicked()和OnNoClicked()。然后,您应该将此接口实现到您调用MessageBox方法的类中,并将“this”作为参数传递。

自定义接口:

public interface MyInterface{ 

    public void onYesClicked(); 

    public void onNoClicked(); 
} 

这一点,你可以做一些事情,当你点击Yes或No.

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); 
    } 

} 
+0

谢谢你的回答,我会试一试。我*新的Java和界面的使用...所以我有一个问题:是myInterface.onYesClicked();正确或应该是AClass.onYesClicked(); ? – Spacewalker 2015-02-23 19:40:28

+0

界面是否正确,因为它允许您在任何类中重用。你只需要在你想要的类中实现接口(实现一个接口有点类似于扩展一个类)。 – programmer23 2015-02-24 08:28:38

+0

我只是很惊讶myInterface.onYesClicked();将工作,因为myInterface.onYesClicked()中没有代码; 我知道inferfaces没有代码,只是方法签名...所以我不知道myInterface.onYesClicked();将会运行。 第二我不明白:类“AClass” - 这有实际的代码 - 但这个电话是从来没有使用..?? – Spacewalker 2015-02-24 09:43:20