该文件中该代码的用途是什么?

时间:2011-12-12 06:52:08

标签: android custom-controls

我已经申请了。我想向我的应用添加最终用户许可协议。所以我创建了一个类来做... 首先,我曾经用内置的AlertDialog来展示我的EULA。 它工作得很好.. 然后我做了我自己的自定义AlertDialog,然后尝试在我的自定义对话框上显示ELUA。现在它工作正常......文件就像......

//my Eula.java file...
//Gets the Eula file from assests folder...



 class Eula {

        private static final String ASSET_EULA = "EULA";
        private static final String PREFERENCE_EULA_ACCEPTED = "eula.accepted";
        private static final String PREFERENCES_EULA = "eula";

        static interface OnEulaAgreedTo {
            void onEulaAgreedTo();
        }

        static boolean show(final Activity activity) 
    {
            final SharedPreferences preferences = activity.getSharedPreferences(PREFERENCES_EULA,
                    Activity.MODE_PRIVATE);
            if (!preferences.getBoolean(PREFERENCE_EULA_ACCEPTED, false)) 
            {
                final CustomDialog.Builder builder = new CustomDialog.Builder(activity);
                builder.setTitle(R.string.app_name1);
                //builder.setCancelable(true);
                builder.setPositiveButton(R.string.eula_accept, new DialogInterface.OnClickListener() 
                {
                    public void onClick(DialogInterface dialog, int which) 
                    {
                        accept(preferences);
                        /*if(activity instanceof OnEulaAgreedTo)
                        {
                            ((OnEulaAgreedTo) activity).onEulaAgreedTo();   
                        }*/
dialog.dismiss();
                    }
                });
                builder.setNegativeButton(R.string.eula_refuse, new DialogInterface.OnClickListener() 
                {

                    public void onClick(DialogInterface dialog, int which) {
                        refuse(activity);
                    }
                });
                CharSequence s = readEula(activity);
                builder.setMessage(s.toString());
                builder.create().show();
                return false;
            }
            return true;
        }

        private static void accept(SharedPreferences preferences) {
            preferences.edit().putBoolean(PREFERENCE_EULA_ACCEPTED, true).commit();
        }

        private static void refuse(Activity activity) {
            activity.finish();
        }

        private static CharSequence readEula(Activity activity) {
            BufferedReader in = null;
            try {
                in = new BufferedReader(new InputStreamReader(activity.getAssets().open(ASSET_EULA)));
                String line;
                StringBuilder buffer = new StringBuilder();
                while ((line = in.readLine()) != null) buffer.append(line).append('\n');
                return buffer;
            } catch (IOException e) {
                return "";
            } finally {
                closeStream(in);
            }
        }
        private static void closeStream(Closeable stream) {
            if (stream != null) {
                try {
                    stream.close();
                } catch (IOException e) {
                }
            }
        }
    }

然后我有我的CustomDialog文件

//my CustomDialog.java file...
public class CustomDialog extends Dialog {
    private static final String ASSET_EULA = "EULA";

    public CustomDialog(Context context, int theme) {
        super(context, theme);
    }

    public CustomDialog(Context context) {
        super(context);
    }
    public static class Builder {

        private Context context;
        private String title;
        private String message;
        private String positiveButtonText;
        private String negativeButtonText;
        //private String cancelButtonText;
        private View contentView;

        private DialogInterface.OnClickListener 
                        positiveButtonClickListener,
                        negativeButtonClickListener;

        public Builder(Context context) {
            this.context = context;
        }

        public Builder setMessage(String message) {
            this.message = message;
            return this;
        }

        public Builder setMessage(int message) {
            this.message = (String) context.getText(message);
            return this;
        }
        public Builder setTitle(int title) {
            this.title = (String) context.getText(title);
            return this;
        }

        public Builder setTitle(String title) {
            this.title = title;
            return this;
        }
        public Builder setContentView(View v) {
            this.contentView = v;
            return this;
        }

        public Builder setPositiveButton(int positiveButtonText,
                DialogInterface.OnClickListener listener) {
            this.positiveButtonText = (String) context
                    .getText(positiveButtonText);
            this.positiveButtonClickListener = listener;
            return this;
        }

        public Builder setPositiveButton(String positiveButtonText,
                DialogInterface.OnClickListener listener) {
            this.positiveButtonText = positiveButtonText;
            this.positiveButtonClickListener = listener;
            return this;
        }

        public Builder setNegativeButton(int negativeButtonText,
                DialogInterface.OnClickListener listener) {
            this.negativeButtonText = (String) context
                    .getText(negativeButtonText);
            this.negativeButtonClickListener = listener;
            return this;
        }

        public Builder setNegativeButton(String negativeButtonText,
                DialogInterface.OnClickListener listener) {
            this.negativeButtonText = negativeButtonText;
            this.negativeButtonClickListener = listener;
            return this;
        }

        public CustomDialog create() {
            LayoutInflater inflater = (LayoutInflater) context
                    .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            // instantiate the dialog with the custom Theme
            final CustomDialog dialog = new CustomDialog(context, 
                    R.style.Dialog);
            View layout = inflater.inflate(R.layout.dialog, null);
            dialog.addContentView(layout, new LayoutParams(
                    LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT));
            // set the dialog title
            ((TextView) layout.findViewById(R.id.title)).setText(title);
            // set the confirm button
            if (positiveButtonText != null) 
            {
                ((Button) layout.findViewById(R.id.positiveButton)).setText(positiveButtonText);
                if (positiveButtonClickListener != null) 
                {
                    ((Button) layout.findViewById(R.id.positiveButton)).setOnClickListener(new View.OnClickListener() 
                    {
                        public void onClick(View v) 
                        {
                            positiveButtonClickListener.onClick(dialog, DialogInterface.BUTTON_POSITIVE);
                        }
                            });
                }
            } else {
                // if no confirm button just set the visibility to GONE
                layout.findViewById(R.id.positiveButton).setVisibility(
                        View.GONE);
            }
            // set the cancel button
            if (negativeButtonText != null) {
                ((Button) layout.findViewById(R.id.negativeButton))
                        .setText(negativeButtonText);
                if (negativeButtonClickListener != null) {
                    ((Button) layout.findViewById(R.id.negativeButton))
                            .setOnClickListener(new View.OnClickListener() {
                                public void onClick(View v) 
                                {
                                    negativeButtonClickListener.onClick(dialog, DialogInterface.BUTTON_NEGATIVE);
                                }
                            });
                }
            } else {
                // if no confirm button just set the visibility to GONE
                layout.findViewById(R.id.negativeButton).setVisibility(
                        View.GONE);
            }
            // set the content message
            if (message != null) {
                ((TextView) layout.findViewById(
                        R.id.message)).setText(message);
            } else if (contentView != null) {
                // if no message set
                // add the contentView to the dialog body
                ((LinearLayout) layout.findViewById(R.id.content))
                        .removeAllViews();
                ((LinearLayout) layout.findViewById(R.id.content))
                        .addView(contentView, 
                                new LayoutParams(
                                        LayoutParams.WRAP_CONTENT, 
                                        LayoutParams.WRAP_CONTENT));
            }
            dialog.setContentView(layout);
            return dialog;
        }

        public void dismiss() 
        {
            this.dismiss();
        }

        public void setCancelable(boolean b) {
            // TODO Auto-generated method stub
            this.setCancelable(true);
        }
    }
}

Atfirst,eula.java文件的onClickfor setPositive按钮就像

public void onClick(DialogInterface dialog, int which) 
                {
                    accept(preferences);
                    if(activity instanceof OnEulaAgreedTo)
                    {
                        ((OnEulaAgreedTo) activity).onEulaAgreedTo();
                    }
                }

它适用于内置的AlertDialog。但是当我用我的自定义对话框更改它时,该编码总是会导致错误...

有谁能告诉我这段代码的用途是什么?

2 个答案:

答案 0 :(得分:1)

问题可能出在以下情况。请检查活动实例是否符合条件?

if(activity instanceof OnEulaAgreedTo)
                    {
                        ((OnEulaAgreedTo) activity).onEulaAgreedTo();   
                    }

答案 1 :(得分:1)

对于消失的对话框,您应该使用Dialog.dismiss()。您可以在正按钮行为结束时关闭对话框。

当您点击拒绝按钮时,您就完成了活动,这就是您对话解除的原因。