package com.jleo;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.SharedPreferences;
public abstract class Action {
public void setActivity(Activity activity) {
this.activity = activity;
}
public void apply(Activity activity) {
apply(activity, null);
}
public void apply(Activity activity, Action precedentAction) {
apply(activity,precedentAction,true);
}
public void apply(Activity activity, Action precedentAction,boolean setContentView4Me) {
this.setActivity(activity);
this.precedentAction = precedentAction;
if(setContentView4Me)
activity.setContentView(getViewId());
apply();
}
public SharedPreferences getSharedPreferences(String preferenceName){
return activity.getSharedPreferences(preferenceName, 0);
}
abstract void apply();
public abstract int getViewId();
public abstract void unapply(Activity activity);
public <T> T findViewById(int id, Class<T> c) {
return c.cast(activity.findViewById(id));
}
public Context getApplicationContext() {
return activity.getApplicationContext();
}
public <T> T getSystemService(String locationService, Class<T> clazz) {
return (T) activity.getSystemService(locationService);
}
public void showDialog(int dialogId) {
activity.showDialog(dialogId);
}
public <T extends Action> void forward(Class<T> clazz, Action previousAction) {
previousAction.unapply(activity);
try {
clazz.newInstance().apply(activity, previousAction);
} catch (IllegalAccessException e) {
e.printStackTrace();
} catch (InstantiationException e) {
e.printStackTrace();
}
}
protected Activity activity;
protected Action precedentAction;
public void alert(int titleId) {
new AlertDialog.Builder(activity).setIcon(R.drawable.alert_dialog_icon)
.setTitle(titleId).setSingleChoiceItems(R.array.ok, 0,
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog,
int whichButton) {
dialog.dismiss();
}
}).show();
}
public void alert(String title) {
new AlertDialog.Builder(activity).setIcon(R.drawable.alert_dialog_icon)
.setTitle(title).setSingleChoiceItems(R.array.ok, 0,
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog,
int whichButton) {
dialog.dismiss();
}
}).show();
}
}
sasa
最新推荐文章于 2023-02-07 16:39:26 发布