我想知道以下代码是否是在Android中使用对话框的好模式,遵循对话框(developer.android)的指导方针并促进封装。
该示例显示了一个用于选择选项的对话框。关键点是ChooseLevel
只需要在标记为HERE的点处修改的类,以便为呈现给用户的选项添加常量。
package org.dialogs;
import android.app.Activity;
import android.app.AlertDialog;
import android.app.Dialog;
import android.content.Context;
import android.content.DialogInterface;
import android.os.Bundle;
import android.widget.Toast;
import org.dialogs.ChooseLevel.Level; // see below
// -----------------------------------------------------------------------------
// -----------------------------------------------------------------------------
public class MainActivity
extends Activity
implements ChooseLevel.Listener
{
// ...........................................................................
@Override
public void onCreate(Bundle icicle) {
super.onCreate(icicle);
showDialog(ChooseLevel.ID);
} // ()
// ...........................................................................
@Override
protected Dialog onCreateDialog(int dialogId) {
Dialog dialog;
if (dialogId == ChooseLevel.ID) {
dialog = new ChooseLevel (this, this).getTheDialog();
}
return dialog;
}
// ...........................................................................
public void levelChosen(Level whatLevel) {
Toast.makeText(this, "level = " + whatLevel.toString(), Toast.LENGTH_LONG).show();
}
} //
// -----------------------------------------------------------------------------
// -----------------------------------------------------------------------------
class ChooseLevel
{
// ...........................................................................
public static final int ID = ChooseLevel.class.hashCode();
// ...........................................................................
public interface Listener {
void levelChosen(Level whatLevel);
}
// ...........................................................................
public enum Level {
Easy, Medium, Expert; // HERE: write constants names for the options
public static final String[] names;
static {
Level[] vals = Level.values();
names = new String[vals.length];
for (int i=0; i<vals.length; i++) {
names[i] = vals[i].toString();
}
} // static initializer
}
// ...........................................................................
private Listener theListener;
// ...........................................................................
private AlertDialog theDialog = null;
public AlertDialog getTheDialog () { return this.theDialog; }
// ...........................................................................
public ChooseLevel (Context ctx, Listener li) {
theListener = li;
AlertDialog.Builder builder = new AlertDialog.Builder(ctx);
builder.setTitle("choose a level");
builder.setSingleChoiceItems(Level.names, -1, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int numChosen) {
dialog.dismiss();
Level theLevel = Level.valueOf(Level.names[numChosen]);
if (theListener != null) {
theListener.levelChosen(theLevel);
}
}
});
theDialog = builder.create();
} // ()
} // class