这个方法让我的项目拥挤,因为我有很多像这样的方法。这些可能仅在方法名称和它所查看的布局上有所不同。我的问题是我怎么能减少这种事件?
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();
}
答案 0 :(得分: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 如果您打算在活动以外的任何地方使用此代码,您将需要对Context / Application的引用。最好的办法是使用一个继承自Application类的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
希望这有帮助。
答案 1 :(得分: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();
}
P.S。 OOP岩石:)