androidactivity传值到dialog,自定义Dialog(二)之Dialog与。。。

更新时间:2023-06-20 08:14:07 阅读: 评论:0

androidactivity传值到dialog,⾃定义Dialog(⼆)之Dialog与。
。。
之前发的那篇⾃定义Dialog似乎有点太含糊,Dialog的关系图甚⾄都没写,以及Dialog显⽰的时候Activity的⽣命周期是处于什么状
态,Dialog如何向Activity传递值
1,Dialog的关系图
2,Dialog显⽰的时候Activity处于什么状态 ? Dialog显⽰的时候不会调⽤Activity的任何⽣命周期⽅法,上代码:
public class MainActivity extends Activity {
private CustomDialog mDialog;
private final String TAG = "MainActivity";
private Button mButton;
@Override
protected void onCreate(Bundle savedInstanceState) {
Log.e(TAG, "onCreate");
tRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
tContentView(R.layout.activity_main);
mButton = (Button) findViewById(R.id.btn_start);
mDialog = new CustomDialog.Builder(this).tTitle("标题")
.tMessage("这是内容")
.tNegativeButton("取消", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
dissmissDialog();
}
})
.tPositiveButton("确认", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
dissmissDialog();
阿司匹林的功效
}
}).tCallBackListener(new DialogCallBackListener() {
@Override
public void callBack(String msg) {
if (!TextUtils.isEmpty(msg))
mButton.tText(msg);
}
}).build();
public void showDialog(View view) {
if (mDialog != null && !mDialog.isShowing())
mDialog.show();
}
public void dissmissDialog() {
if (mDialog != null && mDialog.isShowing())
冬天的形容词
mDialog.dismiss();
}
@Override
protected void onStart() {
Log.e(TAG, "onStart");
}
@Override
protected void onResume() {
Log.e(TAG, "onResume");
}
@Override
protected void onPau() {
Log.e(TAG, "onPau");
}
@Override法令纹怎么去除小妙招
protected void onStop() {
Log.e(TAG, "onStop");
}
@Override
protected void onRestart() {
Log.e(TAG, "onRestart");
}
@Override
protected void onDestroy() {
Log.e(TAG, "onDestory");
}
}
当Dialog显⽰的时候看logcat截图
此时⼿机的屏幕:白萝卜鸡汤
Dialog显⽰不会调⽤Activity的任何⽣命周期
3,Dialog如何向Activity传值:
我是通过接⼝,在操作Dialog的时候回调给调⽤者
演⽰流程:
在Dialog的内容区域放⼀个EdiitText,动态输⼊然后修改MainActivity,Button的值
流程:
(1),在我们Dialog⾥⾯定义⼀个接⼝
(2),当点击Dialog的确定按钮之后触发接⼝⾥⾯的⽅法
(3),MainActivity调⽤Dialog时,实现接⼝
public class CustomDialog extends Dialog {
public interface DialogCallBackListener{//通过该接⼝回调Dialog需要传递的值
public void callBack(String msg);//具体⽅法
}
public CustomDialog(Context context) {
super(context);
}
public CustomDialog(Context context, int themeResId) {
super(context, themeResId);
}
protected CustomDialog(Context context, boolean cancelable, OnCancelListener cancelListener) {椴木
super(context, cancelable, cancelListener);
}
@Override
public void dismiss() {
班主任工作总结
super.dismiss();
}
//⽤Builder模式来构造Dialog
public static class Builder {
应急演练总结报告家庭介绍private Context mContext;
private View contentView;
private String title;
private String message;
private String positiveText;
private String negativeText;
private DialogInterface.OnClickListener positiviOnclickListener;
private DialogInterface.OnClickListener negativeOnclickListener;
private DialogCallBackListener mDialogCallBackListener;
public Builder(Context mContext) {
this.mContext = mContext;
}
public Builder tContentView(View contentView) {//设置dialog的主view
return this;
}
public Builder tTitle(String title) {//设置dialog的标题
this.title = title;
return this;
}
public Builder tMessage(String msg) {//设置dialig的内容
return this;
}
public Builder tPositiveButton(String text, DialogInterface.OnClickListener positiviOnclickListener) {//dialog的确认按钮this.positiveText = text;
this.positiviOnclickListener = positiviOnclickListener;
return this;
}
public Builder tNegativeButton(String text, DialogInterface.OnClickListener negativeOnclickListener) {//dialog的取消按钮
return this;
}
public Builder tCallBackListener(DialogCallBackListener mDialogCallBackListener){//设置回调
this.mDialogCallBackListener = mDialogCallBackListener;
return this;
}
/** * 1,加载要显⽰的布局 * 2,通过dialog的addContentView将布局添加到window中 * 3,基本逻辑处理 * 4,显⽰dialog的布局 */
public CustomDialog build() {
LayoutInflater mInflater = (LayoutInflater) SystemService(Context.LAYOUT_INFLATER_SERVICE);
final CustomDialog mCustomDialog = new CustomDialog(mContext, R.style.CustomDialog);//默认调⽤带style的构造
mCustomDialog.tCanceledOnTouchOutside(fal);//默认点击布局外不能取消dialog
View view = mInflater.inflate(R.layout.custom_dialog, null);
mCustomDialog.addContentView(view, new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT));
final TextView messageView = (TextView) view.findViewById(R.id.tv_message);;
if (!TextUtils.isEmpty(title)) {
TextView titleView = (TextView) view.findViewById(R.id.tv_title);
titleView.tText(title);
}
if (!TextUtils.isEmpty(message)) {
messageView.tHint(message);//显⽰的内容
} el if (contentView != null) {//如果内容区域要显⽰其他的View的话
LinearLayout mContentLayout = (LinearLayout) view.findViewById(t);
mContentLayout.addView(contentView);
}
if (!TextUtils.isEmpty(positiveText)) {//这是确认按钮
Button btn_cofirm = (Button) view.findViewById(R.id.btn_confirm);

本文发布于:2023-06-20 08:14:07,感谢您对本站的认可!

本文链接:https://www.wtabcd.cn/fanwen/fan/82/997039.html

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。

标签:时候   按钮   回调   点击
相关文章
留言与评论(共有 0 条评论)
   
验证码:
推荐文章
排行榜
Copyright ©2019-2022 Comsenz Inc.Powered by © 专利检索| 网站地图