2015-02-23 55 views
1

我嘗試使用計劃用於不同項目的可重用方法創建類(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

回答

3

我認爲這是不可能的,因爲它依賴於事件。但我會給你一個替代方案:使用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