android中有没有类似ShowMessage那种模态对话框
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了android中有没有类似ShowMessage那种模态对话框相关的知识,希望对你有一定的参考价值。
给个例子好了
import android.app.Activity;import android.app.Dialog;
import android.os.Handler;
import android.os.Looper;
import android.os.Message;
import android.text.method.PasswordTransformationMethod;
import android.text.method.TransformationMethod;
import android.view.View;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.TextView;
public class PasswordDialog extends Dialog
int dialogResult;
Handler mHandler ;
public PasswordDialog(Activity context, String mailName, boolean retry)
super(context);
setOwnerActivity(context);
onCreate();
TextView promptLbl = (TextView) findViewById(R.id.promptLbl);
promptLbl.setText("请输入密码/n" + mailName);
public int getDialogResult()
return dialogResult;
public void setDialogResult(int dialogResult)
this.dialogResult = dialogResult;
/** Called when the activity is first created. */
public void onCreate()
setContentView(R.layout.password_dialog);
findViewById(R.id.cancelBtn).setOnClickListener(new android.view.View.OnClickListener()
@Override
public void onClick(View paramView)
endDialog(DialogResult.CANCEL);
);
findViewById(R.id.okBtn).setOnClickListener(new android.view.View.OnClickListener()
@Override
public void onClick(View paramView)
endDialog(DialogResult.OK);
);
public void endDialog(int result)
dismiss();
setDialogResult(result);
Message m = mHandler.obtainMessage();
mHandler.sendMessage(m);
public int showDialog()
mHandler = new Handler()
@Override
public void handleMessage(Message mesg)
// process incoming messages here
//super.handleMessage(msg);
throw new RuntimeException();
;
super.show();
try
Looper.getMainLooper().loop();
catch(RuntimeException e2)
return dialogResult;
参考技术A
有两种方式实现模态对话框:
第一种:通过dialog的方式。
第二种:通过popupWindow的方式。
示例:
第一种方式:protected void dialog()
AlertDialog.Builder builder = new Builder(Main.this);//创建对话框
builder.setMessage("确认退出吗?");//设置显示的消息
builder.setTitle("提示");//设置对话框标题
builder.setPositiveButton("确认", new OnClickListener() //显示确定按钮
@Override
public void onClick(DialogInterface dialog, int which)
dialog.dismiss();
Main.this.finish();
);
builder.setNegativeButton("取消", new OnClickListener() //显示取消按钮
@Override
public void onClick(DialogInterface dialog, int which)
dialog.dismiss();
);
builder.create().show();
第二种方式:
View contentView = LayoutInflater.from(mContext).inflate(
R.layout.pop_window, null);//获取自定义view
//创建popupWindow对象
final PopupWindow popupWindow = new PopupWindow(contentView,
LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT, true);
popupWindow.setTouchable(true);
//设置背景颜色
popupWindow.setBackgroundDrawable(getResources().getDrawable(
R.drawable.selectmenu_bg_downward));
// 设置好参数之后再show,显示对话框
popupWindow.showAsDropDown(view);
在iOS中实现类似安卓自动消失提示框
类方法:
+ (void)showMessage:(NSString *)message { // 获取window UIWindow *window = [UIApplication sharedApplication].keyWindow; UIView *showView = [[UIView alloc] init]; showView.backgroundColor = [UIColor blackColor]; showView.frame = CGRectMake(1, 1, 1, 1); showView.alpha = 1.0f; showView.layer.cornerRadius = 5.0f; showView.layer.masksToBounds = YES; [window addSubview:showView]; UILabel *label = [[UILabel alloc] init]; UIFont *font = [UIFont systemFontOfSize:15]; CGRect labelRect = [message boundingRectWithSize:CGSizeMake(290, CGFLOAT_MAX) options:NSStringDrawingUsesLineFragmentOrigin attributes:@{NSFontAttributeName: font} context:nil]; label.frame = CGRectMake(10, 5, CGRectGetWidth(labelRect), CGRectGetHeight(labelRect)); label.text = message; label.textColor = [UIColor whiteColor]; label.textAlignment = NSTextAlignmentCenter; label.backgroundColor = [UIColor clearColor]; [showView addSubview:label]; showView.frame = CGRectMake((SCREEN_WIDTH - CGRectGetWidth(labelRect) - 20)/2, SCREEN_HEIGHT - 100, CGRectGetWidth(labelRect)+20, CGRectGetHeight(labelRect)+10); [UIView animateWithDuration:1.5 animations:^{ showView.alpha = 0; } completion:^(BOOL finished) { [showView removeFromSuperview]; }]; }
以上是关于android中有没有类似ShowMessage那种模态对话框的主要内容,如果未能解决你的问题,请参考以下文章
DELPHI中怎么调用showmessage这个函数来弹出一个对话框