Android中对话框的良好设计模式

时间:2012-04-07 01:09:22

标签: android dialog design-patterns

我想知道以下代码是否是在Android中使用Dialogs的良好模式,遵循dialogs (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

1 个答案:

答案 0 :(得分:0)

我找到了一种更简单的方法来显示用于选择选项的对话框:

用户代码为:

import android.app.Activity;
import android.app.Dialog;
import android.os.Bundle;
import android.widget.Toast;

// -----------------------------------------------------------------------------
// -----------------------------------------------------------------------------
public class MainActivity 
    extends Activity 
    implements Chooser.Listener
{
    // .....................................................................
    private Chooser levelChooser;

    // .....................................................................
    enum Options { one, two, tree, four};

    // .....................................................................
    // .....................................................................
    @Override
    public void onCreate(Bundle icicle) {
        super.onCreate(icicle);

        levelChooser = new Chooser (Options.class, "choose an option", this, this);

        showDialog(levelChooser.ID);
    } // ()

    // .....................................................................
    // .....................................................................
    @Override
    protected Dialog onCreateDialog(int dialogId) {
        Dialog aDialog = null;

        if (dialogId == levelChooser.ID) {
            aDialog = levelChooser.getTheDialog();
        }
        return aDialog;
    }

    // .....................................................................
    // .....................................................................
    public <T extends Enum<T>> void onChoiceMade(T theChoice) {
        Toast.makeText(this, "choice = " + theChoice, Toast.LENGTH_LONG).show();
    }
} // class

您只需要更改上面enum Options中的常量。 所有技巧都在Chooser类中完成,不需要修改:

import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import java.lang.reflect.Method;

public class Chooser {

    // .....................................................................
    // .....................................................................
    public static <T extends Enum<T>> String[] getNames(Class<T> c) {
        // returns the values of *any* enum in an array of String
        String[] values;
        try {
            Method m1 = c.getMethod("values");
            Object[] valuesObj;
            valuesObj = (Object[]) m1.invoke(c);
            values = new String[valuesObj.length];
            for (int i = 0; i < valuesObj.length; i++) {
                values[i] = valuesObj[i].toString();
            }
        } catch (Exception ex) {
            values = null;
        }
        return values;
    } // ()

    // .....................................................................
    // .....................................................................
    public interface Listener {
        public <T extends Enum<T>> void onChoiceMade(T theChoice);
    }

    // .....................................................................
    // .....................................................................
    public final int ID;
    private Listener theListener;
    private AlertDialog.Builder theBuilder;
    private AlertDialog theDialog = null;
    private final String[] names;

    // .....................................................................
    // .....................................................................
    public final AlertDialog getTheDialog() {
        if (this.theDialog == null) {
            this.theDialog = this.theBuilder.create();
        }
        return this.theDialog;
    }

    // .....................................................................
    // .....................................................................
    public <T extends Enum<T>> Chooser(final Class<T> theEnum, String theTitle, Context ctx, Listener listener) {

        this.ID = theEnum.hashCode();
        this.names = getNames(theEnum);
        this.theListener = listener;
        this.theBuilder = new AlertDialog.Builder(ctx);
        this.theBuilder.setTitle(theTitle);

        this.theBuilder.setSingleChoiceItems(names, -1, new DialogInterface.OnClickListener() {

            public void onClick(DialogInterface dialog, int electedNumber) {
                dialog.dismiss();
                Enum.valueOf(theEnum, names[electedNumber]);

                T theOption = (T) Enum.valueOf(theEnum, names[electedNumber]);
                if (theListener != null) {
                    theListener.onChoiceMade(theOption);
                }
            }
        });

    } // ()
} // class