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;
}
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;
}
}
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();
}
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;
}
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;
}
}
Aggregations