2015-01-15 64 views
-1

這種方法使我的項目擁擠,因爲我有很多這樣的方法。這些可能僅在方法名稱和它所查看的佈局上有所不同。我的問題是如何減少這種事件?如何最小化編碼?

void showLevel1Quest1() { 
    AlertDialog.Builder builder = new AlertDialog.Builder(context); 
    builder.setTitle(context.getText(R.string.question)); 
    LayoutInflater inflater = (LayoutInflater) context 
      .getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
    View view = inflater.inflate(R.layout.add_question1, null); 
    builder.setView(view); 
    builder.setCancelable(true); 
    final AlertDialog finishDialog = builder.create(); 
    View closeButton = view.findViewById(R.id.close); 
    closeButton.setOnClickListener(new OnClickListener() { 
     @Override 
     public void onClick(View clicked) { 

      if (clicked.getId() == R.id.close) { 
       finishDialog.dismiss(); 
      } 
     } 
    }); 
    finishDialog.show(); 
} 

回答

4

您可以將一個通用的方法在一個單獨的類,只是傳遞要使用,如佈局ID:

/** 
* makeAlertBox 
* 
* Populates an Android OS alert dialog with the passed params. 
* Only for quick messages that require no imput, other than to 
* dismiss the dialog 
* 
* @param context - The application context 
* @param title - The dialog's title 
* @param message - The dialog's message 
* @param positiveText - The OK buttons text 
*/ 
public static void makeAlertBox(Context context, String title, String message, 
     String positiveText, int layoutId) 
{ 
    try 
    { 
     LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
     View view = inflater.inflate(layoutId, null); 
     new AlertDialog.Builder(context) 
     .setView(view) 
     .setTitle(title) 
     .setMessage(message) 
     .setPositiveButton(positiveText, new DialogInterface.OnClickListener() 
     {    
      @Override 
      public void onClick(DialogInterface dialog, int which) 
      { 
       dialog.cancel(); 
      } 
     }) 
     .setCancelable(false) 
     .show(); 
    } 
    catch(Exception ex) 
    { 
     Log.e("UTIL", "Caught exception while attempting to create alertdialog"); 
     ex.printStackTrace(); 
    } 
} 

在我的代碼我的抽象類被稱爲Utility所以我打電話:

Utility.makeAlertBox(getApplicationContext(), "Title", "Message", "Okay!", someLayoutId); 

編輯你可以明顯地擺脫你不需要額外的參數。作爲一個例子,我從工作區中複製粘貼。

編輯2如果您計劃在活動以外的任何地方使用此代碼,您將需要對上下文/應用程序的引用。最好的辦法是使用Singleton類,繼承從應用程序類,如下所示:

public class myApplication extends Application 
{ 
    private static myApplication instance; 

    @Override 
    public void onCreate() 
    { 
     instance = this; 
     super.onCreate(); 
    } 

    /** 
    * getInstance 
    * @return Returns the instance of this myApplication 
    */ 
    public static myApplication getInstance() 
    { 
     if(instance != null)  
      return instance; 

     return new myApplication(); 
    } 
} 

然後,當你需要訪問的背景下,你可以這樣做:myApplication.getInstance()或者myApplication.getInstance().getApplicationContext()

你也需要更新您的清單,以確保應用程序被拿起:

<application 
    android:name="com.YOURPACKAGE.myApplication" 
    <!-- everything else. Such as Activites etc...--> 
</application 

希望這會有所幫助。

+0

所以你的意思是先生我會創建一個新班級? – user3698267 2015-01-15 15:56:09

+0

你不需要。但從長遠來看,重構它自己的對象會更好,特別是如果它只是一個對話框而不是真的需要在一個活動中。創建一個新的抽象類並添加此方法(將其更改爲您認爲合適的方式),然後按照上面顯示的名稱(將Utility替換爲您自己的類名稱)調用它。 – LokiSinclair 2015-01-15 16:00:29

+0

sir ive getApplicationContext出錯。 – user3698267 2015-01-15 16:15:50

1

給你:

void showQuest(int layoutResId) { 
     AlertDialog.Builder builder = new AlertDialog.Builder(context); 
     builder.setTitle(context.getText(R.string.question)); 
     LayoutInflater inflater = (LayoutInflater) context 
       .getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
     View view = inflater.inflate(layoutResId, null); 
     builder.setView(view); 
     builder.setCancelable(true); 
     final AlertDialog finishDialog = builder.create(); 
     View closeButton = view.findViewById(R.id.close); 
     closeButton.setOnClickListener(new OnClickListener() { 
      @Override 
      public void onClick(View clicked) { 

       if (clicked.getId() == R.id.close) { 
        finishDialog.dismiss(); 
       } 
      } 
     }); 
     finishDialog.show(); 
    } 

附: OOP岩石:)

+0

我的意思是我有showLevel1Quest1然後showLevel1Quest2等。我怎樣才能使方法的名稱是唯一的? – user3698267 2015-01-15 15:32:10

+1

如果只有1個參數不同,爲什麼需要不同的方法? – localhost 2015-01-15 15:33:00

+0

你的權利。對不起。所以如果我的權利的方法會是這樣的? showQuest(int add_question1)? – user3698267 2015-01-15 15:35:31