如何最小化编码?

How to minimize codings?

这种方法让我的项目很拥挤,因为我有很多这样的方法。这些可能仅在方法名称和它查看的布局上有所不同。我的问题是我怎样才能减少这种事件?

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

你在这里:

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

P.S。 OOP 棒极了 :)

您可以将通用方法放在单独的 class 中,只需传入您要使用的布局的 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();
    }
}

在我的代码中,我的摘要 class 被称为 Utility 所以我会调用:

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

edit 您显然可以摆脱不需要的额外参数。作为示例,我几乎从我的工作区复制并粘贴了。

编辑 2 如果您计划在 Activity 以外的任何地方使用此代码,您将需要对 Context/Application 的引用。最好的办法是使用继承自应用程序 class 的单例 class,如下所示:

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

希望对您有所帮助。