Search in sources :

Example 6 with SafeListener

use of com.andframe.listener.SafeListener in project AndFrameWorks by scwang90.

the class AfDialogBuilder method showKeyDialog.

/**
 * 显示视图对话框(不再提示)
 *
 * @param key       不再显示KEY
 * @param defclick  不再显示之后默认执行index
 * @param theme     主题
 * @param iconres   对话框图标
 * @param title     显示标题
 * @param message   显示内容
 * @param positive  确认 按钮显示信息
 * @param lpositive 点击  确认 按钮 响应事件
 * @param neutral   详细 按钮显示信息
 * @param lneutral  点击  详细 按钮 响应事件
 * @param negative  按钮显示信息
 * @param lnegative 点击  拒绝 按钮 响应事件
 */
@Override
@TargetApi(Build.VERSION_CODES.HONEYCOMB)
public Dialog showKeyDialog(final String key, final int defclick, int theme, int iconres, CharSequence title, final CharSequence message, CharSequence negative, OnClickListener lnegative, CharSequence neutral, OnClickListener lneutral, CharSequence positive, OnClickListener lpositive) {
    if (!TextUtils.isEmpty(key) && defclick > -1) {
        int click = $.cache(key).getInt(key, -1);
        if (click == defclick) {
            OnClickListener[] clicks = { new SafeListener(lnegative), new SafeListener(lneutral), new SafeListener(lpositive) };
            for (int i = 0; i < clicks.length; i++) {
                if (i == defclick && clicks[i] != null) {
                    clicks[i].onClick(null, i);
                }
            }
            return null;
        }
    }
    final Dialog dialog = showDialog(theme, iconres, title, message, negative, lnegative, neutral, lneutral, positive, lpositive);
    if (dialog != null && !TextUtils.isEmpty(key)) {
        final View decor = dialog.getWindow().getDecorView();
        decor.postDelayed(() -> {
            CheckBox cbTip = null;
            FindTextViewWithText builderHelper = FindTextViewWithText.invoke((ViewGroup) decor, message);
            if (builderHelper != null) {
                int index = builderHelper.index;
                ViewGroup parent = builderHelper.parent;
                TextView textView = builderHelper.textView;
                parent.removeView(textView);
                LinearLayout ll = new LinearLayout(mContext);
                ll.setOrientation(LinearLayout.VERTICAL);
                LinearLayout.LayoutParams lp;
                LayoutParams olp = textView.getLayoutParams();
                if (olp instanceof ViewGroup.MarginLayoutParams) {
                    lp = new LinearLayout.LayoutParams((ViewGroup.MarginLayoutParams) olp);
                } else {
                    lp = new LinearLayout.LayoutParams(olp);
                }
                ll.setPadding(textView.getPaddingLeft(), textView.getPaddingTop(), textView.getPaddingRight(), textView.getPaddingBottom());
                textView.setPadding(0, 0, 0, 0);
                ll.addView(textView, lp);
                cbTip = new CheckBox(mContext);
                cbTip.setText(TXT_NOMORESHOW);
                if (olp instanceof ViewGroup.MarginLayoutParams) {
                    lp = new LinearLayout.LayoutParams((ViewGroup.MarginLayoutParams) olp);
                    Integer leftMargin = AfReflecter.getMemberNoException(lp, "leftMargin", int.class);
                    Integer topMargin = AfReflecter.getMemberNoException(lp, "topMargin", int.class);
                    Integer rightMargin = AfReflecter.getMemberNoException(lp, "rightMargin", int.class);
                    Integer bottomMargin = AfReflecter.getMemberNoException(lp, "bottomMargin", int.class);
                    if (leftMargin != null && topMargin != null && rightMargin != null && bottomMargin != null) {
                        lp.setMargins(leftMargin, topMargin + textView.getPaddingTop(), rightMargin, bottomMargin);
                    } else {
                        lp.setMargins(0, textView.getPaddingTop(), 0, 0);
                    }
                } else {
                    lp = new LinearLayout.LayoutParams(olp);
                    lp.setMargins(0, textView.getPaddingTop(), 0, 0);
                }
                ll.addView(cbTip, lp);
                parent.addView(ll, index, olp);
            }
            if (cbTip != null) {
                final CheckBox finalCbTip = cbTip;
                dialog.setOnDismissListener(dialog1 -> {
                    if (finalCbTip.isChecked()) {
                        $.cache(key).put(key, defclick);
                    }
                });
            }
        }, mBuildDelayed);
    }
    return dialog;
}
Also used : LayoutParams(android.view.ViewGroup.LayoutParams) ViewGroup(android.view.ViewGroup) View(android.view.View) TextView(android.widget.TextView) SuppressLint(android.annotation.SuppressLint) TimePickerDialog(android.app.TimePickerDialog) Dialog(android.app.Dialog) DatePickerDialog(android.app.DatePickerDialog) ProgressDialog(android.app.ProgressDialog) AlertDialog(android.app.AlertDialog) CheckBox(android.widget.CheckBox) OnClickListener(android.content.DialogInterface.OnClickListener) TextView(android.widget.TextView) SafeListener(com.andframe.listener.SafeListener) LinearLayout(android.widget.LinearLayout) TargetApi(android.annotation.TargetApi)

Example 7 with SafeListener

use of com.andframe.listener.SafeListener in project AndFrameWorks by scwang90.

the class AfDialogBuilder method inputText.

/**
 * 弹出一个文本输入框
 *
 * @param title    标题
 * @param defaul   默认值
 * @param type     android.text.InputType
 * @param listener 监听器
 */
@Override
public Dialog inputText(CharSequence title, final CharSequence defaul, int type, final InputTextListener listener) {
    final EditText input = new EditText(mContext);
    final int defaullength = defaul != null ? defaul.length() : 0;
    input.setText(defaul);
    input.clearFocus();
    input.setInputType(type);
    final CharSequence oKey = "确定";
    final CharSequence msgKey = "$inputText$";
    OnClickListener cancleListener = (dialog, which) -> {
        AfSoftKeyboard.hideSoftKeyboard(input);
        if (listener instanceof InputTextCancelable) {
            ((InputTextCancelable) listener).onInputTextCancel(input);
        }
        if (dialog != null) {
            dialog.dismiss();
        }
    };
    final OnClickListener okListener = (dialog, which) -> {
        if (listener.onInputTextComfirm(input, input.getText().toString())) {
            AfSoftKeyboard.hideSoftKeyboard(input);
            if (dialog != null) {
                dialog.dismiss();
            }
        }
    };
    final OnShowListener showListener = dialog -> {
        AfSoftKeyboard.showSoftkeyboard(input);
        if (defaullength > 3 && defaul.toString().matches("[^.]+\\.[a-zA-Z]\\w{1,3}")) {
            input.setSelection(0, defaul.toString().lastIndexOf('.'));
        } else {
            input.setSelection(0, defaullength);
        }
    };
    if (mBuildNative) {
        Builder builder = new AlertDialog.Builder(mContext);
        builder.setView(input);
        builder.setCancelable(false);
        builder.setTitle(title);
        builder.setPositiveButton("确定", new SafeListener());
        builder.setNegativeButton("取消", cancleListener);
        final AlertDialog dialog = builder.create();
        dialog.setOnShowListener(showListener);
        dialog.show();
        dialog.getButton(AlertDialog.BUTTON_POSITIVE).setOnClickListener(v -> new SafeListener(okListener).onClick(dialog, 0));
        return dialog;
    } else {
        final Dialog dialog = showDialog(title, msgKey, oKey, okListener, "取消", cancleListener);
        new Handler(Looper.getMainLooper()).postDelayed(() -> {
            FindTextViewWithText builderHelper = FindTextViewWithText.invoke((ViewGroup) dialog.getWindow().getDecorView(), msgKey);
            if (builderHelper != null) {
                builderHelper.parent.removeViewAt(builderHelper.index);
                builderHelper.parent.addView(input, builderHelper.index, builderHelper.textView.getLayoutParams());
                showListener.onShow(dialog);
                builderHelper = FindTextViewWithText.invoke((ViewGroup) dialog.getWindow().getDecorView(), oKey);
                if (builderHelper != null) {
                    builderHelper.textView.setOnClickListener(v -> new SafeListener(okListener).onClick(dialog, 0));
                }
            }
        }, mBuildDelayed);
        return dialog;
    }
}
Also used : EditText(android.widget.EditText) Builder(android.app.AlertDialog.Builder) Context(android.content.Context) TimePickerDialog(android.app.TimePickerDialog) LinearLayout(android.widget.LinearLayout) OnClickListener(android.content.DialogInterface.OnClickListener) OnMultiChoiceClickListener(android.content.DialogInterface.OnMultiChoiceClickListener) Date(java.util.Date) Dialog(android.app.Dialog) AfDensity(com.andframe.util.android.AfDensity) DatePicker(android.widget.DatePicker) Stack(java.util.Stack) SuppressLint(android.annotation.SuppressLint) Calendar(java.util.Calendar) DatePickerDialog(android.app.DatePickerDialog) CheckBox(android.widget.CheckBox) AfActivity(com.andframe.activity.AfActivity) Handler(android.os.Handler) Looper(android.os.Looper) View(android.view.View) OnCancelListener(android.content.DialogInterface.OnCancelListener) OnShowListener(android.content.DialogInterface.OnShowListener) Build(android.os.Build) OnDateSetListener(android.app.DatePickerDialog.OnDateSetListener) TargetApi(android.annotation.TargetApi) DialogInterface(android.content.DialogInterface) OnTimeSetListener(android.app.TimePickerDialog.OnTimeSetListener) SafeListener(com.andframe.listener.SafeListener) ProgressDialog(android.app.ProgressDialog) InputType(android.text.InputType) TextUtils(android.text.TextUtils) ViewGroup(android.view.ViewGroup) AfExceptionHandler(com.andframe.exception.AfExceptionHandler) AlertDialog(android.app.AlertDialog) DialogBuilder(com.andframe.api.DialogBuilder) Gravity(android.view.Gravity) LayoutParams(android.view.ViewGroup.LayoutParams) TextView(android.widget.TextView) TypedValue(android.util.TypedValue) com.andframe.$(com.andframe.$) RelativeLayout(android.widget.RelativeLayout) TimePicker(android.widget.TimePicker) AfReflecter(com.andframe.util.java.AfReflecter) Window(android.view.Window) EditText(android.widget.EditText) AlertDialog(android.app.AlertDialog) ViewGroup(android.view.ViewGroup) Builder(android.app.AlertDialog.Builder) DialogBuilder(com.andframe.api.DialogBuilder) Handler(android.os.Handler) AfExceptionHandler(com.andframe.exception.AfExceptionHandler) SuppressLint(android.annotation.SuppressLint) OnShowListener(android.content.DialogInterface.OnShowListener) TimePickerDialog(android.app.TimePickerDialog) Dialog(android.app.Dialog) DatePickerDialog(android.app.DatePickerDialog) ProgressDialog(android.app.ProgressDialog) AlertDialog(android.app.AlertDialog) OnClickListener(android.content.DialogInterface.OnClickListener) SafeListener(com.andframe.listener.SafeListener)

Example 8 with SafeListener

use of com.andframe.listener.SafeListener in project AndFrameWorks by scwang90.

the class AfDialogBuilder method showViewDialog.

/**
 * 显示视图对话框
 *
 * @param theme     主题
 * @param iconres   对话框图标
 * @param title     显示标题
 * @param view      显示内容
 * @param positive  确认 按钮显示信息
 * @param lpositive 点击  确认 按钮 响应事件
 * @param neutral   详细 按钮显示信息
 * @param lneutral  点击  详细 按钮 响应事件
 * @param negative  按钮显示信息
 * @param lnegative 点击  拒绝 按钮 响应事件
 */
@Override
@SuppressLint("NewApi")
public Dialog showViewDialog(int theme, int iconres, CharSequence title, View view, CharSequence negative, OnClickListener lnegative, CharSequence neutral, OnClickListener lneutral, CharSequence positive, OnClickListener lpositive) {
    Builder builder = null;
    if (theme > 0) {
        try {
            builder = new Builder(mContext, theme);
        } catch (Throwable ignored) {
        }
    }
    if (builder == null) {
        try {
            builder = new Builder(mContext);
        } catch (Throwable ex) {
            return null;
        }
    }
    builder.setTitle(title);
    RelativeLayout.LayoutParams lp;
    lp = new RelativeLayout.LayoutParams(AfActivity.LP_WC, AfActivity.LP_WC);
    lp.addRule(RelativeLayout.CENTER_IN_PARENT, RelativeLayout.TRUE);
    RelativeLayout layout = new RelativeLayout(mContext);
    layout.addView(view, lp);
    builder.setView(layout);
    if (iconres > 0) {
        builder.setIcon(iconres);
    }
    if (positive != null && positive.length() > 0) {
        builder.setPositiveButton(positive, new SafeListener(lpositive));
    }
    if (negative != null && negative.length() > 0) {
        builder.setNegativeButton(negative, new SafeListener(lnegative));
    }
    if (neutral != null && neutral.length() > 0) {
        builder.setNeutralButton(neutral, new SafeListener(lneutral));
    }
    builder.setCancelable(false);
    builder.create();
    return builder.show();
}
Also used : Builder(android.app.AlertDialog.Builder) DialogBuilder(com.andframe.api.DialogBuilder) RelativeLayout(android.widget.RelativeLayout) SafeListener(com.andframe.listener.SafeListener) SuppressLint(android.annotation.SuppressLint)

Example 9 with SafeListener

use of com.andframe.listener.SafeListener in project AndFrameWorks by scwang90.

the class AfDialogBuilder method selectTime.

/**
 * 选择时间
 * @param title 标题
 * @param value 默认时间
 * @param listener 监听器
 */
@Override
public Dialog selectTime(CharSequence title, Date value, final OnTimeSetListener listener) {
    Calendar calender = Calendar.getInstance();
    calender.setTime(value);
    int hour = calender.get(Calendar.HOUR_OF_DAY);
    int minute = calender.get(Calendar.MINUTE);
    AlertDialog tDialog = new TimePickerDialog(mContext, new SafeListener(listener), hour, minute, true) {

        @Override
        public void show() {
            super.show();
            if (listener instanceof OnTimeSetVerifyListener) {
                getButton(BUTTON_POSITIVE).setOnClickListener(v -> {
                    try {
                        TimePicker picker = AfReflecter.getMemberByType(this, TimePicker.class);
                        if (picker == null) {
                            this.dismiss();
                            super.onClick(this, BUTTON_POSITIVE);
                        } else if (((OnTimeSetVerifyListener) listener).onPreTimeSet(this, picker, picker.getCurrentHour(), picker.getCurrentMinute())) {
                            this.dismiss();
                            super.onClick(this, BUTTON_POSITIVE);
                        }
                    } catch (IllegalAccessException e) {
                        e.printStackTrace();
                    }
                });
            }
        }
    };
    if (title != null && title.length() > 0) {
        tDialog.setTitle(title);
    }
    tDialog.show();
    tDialog.setCancelable(true);
    return tDialog;
}
Also used : AlertDialog(android.app.AlertDialog) TimePicker(android.widget.TimePicker) Calendar(java.util.Calendar) TimePickerDialog(android.app.TimePickerDialog) SafeListener(com.andframe.listener.SafeListener) SuppressLint(android.annotation.SuppressLint)

Example 10 with SafeListener

use of com.andframe.listener.SafeListener in project AndFrameWorks by scwang90.

the class AfDialogBuilder method inputLines.

/**
 * 弹出一个文本输入框
 *
 * @param title    标题
 * @param defaul   默认值
 * @param type     android.text.InputType
 * @param listener 监听器
 */
@Override
public Dialog inputLines(CharSequence title, final CharSequence defaul, int type, final InputTextListener listener) {
    final EditText input = new EditText(mContext);
    final int defaullength = defaul != null ? defaul.length() : 0;
    input.setText(defaul);
    input.clearFocus();
    input.setInputType(type | InputType.TYPE_TEXT_FLAG_MULTI_LINE);
    input.setGravity(Gravity.TOP);
    input.setSingleLine(false);
    input.setHorizontallyScrolling(false);
    input.setMinHeight(AfDensity.dp2px(100));
    final CharSequence oKey = "确定";
    final CharSequence msgKey = "$inputLines$";
    OnClickListener cancleListener = (dialog, which) -> {
        AfSoftKeyboard.hideSoftKeyboard(input);
        if (listener instanceof InputTextCancelable) {
            ((InputTextCancelable) listener).onInputTextCancel(input);
        }
        if (dialog != null) {
            dialog.dismiss();
        }
    };
    final OnClickListener okListener = (dialog, which) -> {
        if (listener.onInputTextComfirm(input, input.getText().toString())) {
            AfSoftKeyboard.hideSoftKeyboard(input);
            if (dialog != null) {
                dialog.dismiss();
            }
        }
    };
    final OnShowListener showListener = dialog -> {
        AfSoftKeyboard.showSoftkeyboard(input);
        if (defaullength > 3 && defaul.toString().matches("[^.]+\\.[a-zA-Z]\\w{1,3}")) {
            input.setSelection(0, defaul.toString().lastIndexOf('.'));
        } else {
            input.setSelection(0, defaullength);
        }
    };
    if (mBuildNative) {
        Builder builder = new AlertDialog.Builder(mContext);
        builder.setView(input);
        builder.setCancelable(false);
        builder.setTitle(title);
        builder.setPositiveButton("确定", new SafeListener());
        builder.setNegativeButton("取消", cancleListener);
        final AlertDialog dialog = builder.create();
        dialog.setOnShowListener(showListener);
        dialog.show();
        dialog.getButton(AlertDialog.BUTTON_POSITIVE).setOnClickListener(v -> new SafeListener(okListener).onClick(dialog, 0));
        return dialog;
    } else {
        final Dialog dialog = showDialog(title, msgKey, oKey, okListener, "取消", cancleListener);
        new Handler(Looper.getMainLooper()).postDelayed(() -> {
            FindTextViewWithText builderHelper = FindTextViewWithText.invoke((ViewGroup) dialog.getWindow().getDecorView(), msgKey);
            if (builderHelper != null) {
                builderHelper.parent.removeViewAt(builderHelper.index);
                builderHelper.parent.addView(input, builderHelper.index, builderHelper.textView.getLayoutParams());
                showListener.onShow(dialog);
                builderHelper = FindTextViewWithText.invoke((ViewGroup) dialog.getWindow().getDecorView(), oKey);
                if (builderHelper != null) {
                    builderHelper.textView.setOnClickListener(v -> new SafeListener(okListener).onClick(dialog, 0));
                }
            }
        }, mBuildDelayed);
        return dialog;
    }
}
Also used : EditText(android.widget.EditText) Builder(android.app.AlertDialog.Builder) Context(android.content.Context) TimePickerDialog(android.app.TimePickerDialog) LinearLayout(android.widget.LinearLayout) OnClickListener(android.content.DialogInterface.OnClickListener) OnMultiChoiceClickListener(android.content.DialogInterface.OnMultiChoiceClickListener) Date(java.util.Date) Dialog(android.app.Dialog) AfDensity(com.andframe.util.android.AfDensity) DatePicker(android.widget.DatePicker) Stack(java.util.Stack) SuppressLint(android.annotation.SuppressLint) Calendar(java.util.Calendar) DatePickerDialog(android.app.DatePickerDialog) CheckBox(android.widget.CheckBox) AfActivity(com.andframe.activity.AfActivity) Handler(android.os.Handler) Looper(android.os.Looper) View(android.view.View) OnCancelListener(android.content.DialogInterface.OnCancelListener) OnShowListener(android.content.DialogInterface.OnShowListener) Build(android.os.Build) OnDateSetListener(android.app.DatePickerDialog.OnDateSetListener) TargetApi(android.annotation.TargetApi) DialogInterface(android.content.DialogInterface) OnTimeSetListener(android.app.TimePickerDialog.OnTimeSetListener) SafeListener(com.andframe.listener.SafeListener) ProgressDialog(android.app.ProgressDialog) InputType(android.text.InputType) TextUtils(android.text.TextUtils) ViewGroup(android.view.ViewGroup) AfExceptionHandler(com.andframe.exception.AfExceptionHandler) AlertDialog(android.app.AlertDialog) DialogBuilder(com.andframe.api.DialogBuilder) Gravity(android.view.Gravity) LayoutParams(android.view.ViewGroup.LayoutParams) TextView(android.widget.TextView) TypedValue(android.util.TypedValue) com.andframe.$(com.andframe.$) RelativeLayout(android.widget.RelativeLayout) TimePicker(android.widget.TimePicker) AfReflecter(com.andframe.util.java.AfReflecter) Window(android.view.Window) EditText(android.widget.EditText) AlertDialog(android.app.AlertDialog) ViewGroup(android.view.ViewGroup) Builder(android.app.AlertDialog.Builder) DialogBuilder(com.andframe.api.DialogBuilder) Handler(android.os.Handler) AfExceptionHandler(com.andframe.exception.AfExceptionHandler) SuppressLint(android.annotation.SuppressLint) OnShowListener(android.content.DialogInterface.OnShowListener) TimePickerDialog(android.app.TimePickerDialog) Dialog(android.app.Dialog) DatePickerDialog(android.app.DatePickerDialog) ProgressDialog(android.app.ProgressDialog) AlertDialog(android.app.AlertDialog) OnClickListener(android.content.DialogInterface.OnClickListener) SafeListener(com.andframe.listener.SafeListener)

Aggregations

SafeListener (com.andframe.listener.SafeListener)11 SuppressLint (android.annotation.SuppressLint)8 Builder (android.app.AlertDialog.Builder)7 DialogBuilder (com.andframe.api.DialogBuilder)7 AlertDialog (android.app.AlertDialog)6 TargetApi (android.annotation.TargetApi)5 DatePickerDialog (android.app.DatePickerDialog)5 TimePickerDialog (android.app.TimePickerDialog)5 Calendar (java.util.Calendar)5 Dialog (android.app.Dialog)4 ProgressDialog (android.app.ProgressDialog)4 DialogInterface (android.content.DialogInterface)4 OnCancelListener (android.content.DialogInterface.OnCancelListener)4 OnClickListener (android.content.DialogInterface.OnClickListener)4 View (android.view.View)4 ViewGroup (android.view.ViewGroup)4 LayoutParams (android.view.ViewGroup.LayoutParams)4 CheckBox (android.widget.CheckBox)4 DatePicker (android.widget.DatePicker)4 LinearLayout (android.widget.LinearLayout)4