android 对话框的样式,android之Dialog对话框样式的四种实现方式

1.最好的实现方式,重写dialog样式。html

特色:1)须要自定义整个布局内容。java

2)能够设置点击对话框外禁止取消窗口。android

3)能够弹出输入法。app

4)点返回键能够取消。ide

package com.ebt.app.msettings.view;

import android.app.Dialog;

import android.content.Context;

import android.os.Bundle;

import android.view.Gravity;

import android.view.LayoutInflater;

import android.view.ViewGroup;

import android.view.WindowManager.LayoutParams;

import com.ebt.app.R;

/**

* 对话框基类

*

* @author Allen.li

*

*/

public class BaseDialog extends Dialog{

private Context context;

private LayoutInflater inflater;

private ViewGroup view;

public BaseDialog(Context context) {

//super(context);

super(context, R.style.dialog);

this.context = context;

}

@Override

protected void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setCanceledOnTouchOutside(false);//点击对话框外禁止取消窗口

LayoutParams params = getWindow().getAttributes();

params.windowAnimations = R.style.popupAnimation;

getWindow().setGravity(Gravity.CENTER_HORIZONTAL|Gravity.CENTER_VERTICAL);

getWindow().setAttributes(

(android.view.WindowManager.LayoutParams) params);

this.inflater = getLayoutInflater();

view = (ViewGroup) inflater.inflate(R.layout.widget_window_msg_phone, null);

setContentView(view);

view.setFocusable(true);

view.setFocusableInTouchMode(true);

view.requestFocus();

}

}

<?xml version="1.0" encoding="utf-8"?>布局

true

true

@null

true

@null

@color/full_transparent

@null

true

BaseDialog bd = new BaseDialog(getContext());bd.show();

2.在用AlertDialog来实现。代码同dialog同样,不过输入法不弹出了。解决这个问题参看以下连接,能够弹出输入法,能够设置点击对话框外禁止取消窗口。this

http://blog.csdn.net/fastthinking/article/details/38492389

spa

3.在用activity来实现。样式设置成dialog样式。不过用Acitivity来实现简单的对话框有些没必要要。.net

4.用PopupWindow实现,不过在有输入框的对话框时,PopupWindow不能很好的实现点击返回键退出。缘由以下:code

1).发现PopupWindow一个bug。

只有获取焦点才能弹出输入法,若是PopupWindow的ContentView想弹出输入法的话,必须设置PopupWindow获取焦点,即PopupWindow.setFocusable(true);

可是会致使设置禁止点击弹出窗口外失败,PopupWindow.setOutsideTouchable(false);即点击弹出窗口外会取消窗口。由于

setOutsideTouchable的设置仅仅对处于未获取焦点的且处于可触摸模式的PopupWindow起做用。setOutsideTouchable默认值是false.也就是说若是PopupWindow获取了焦点,此方法不起做用。点击PopupWindow窗口外的动做响应是取消窗口。

原文注释:

void android.widget.

PopupWindow.setOutsideTouchable(

boolean touchable)

Controls whether the pop-up will be informed of touch events outside of its window.This only makes sense for pop-ups that are touchable but not focusable, which means touches outside of the window will be delivered to the window behind. The default is false.

If the popup is showing, calling this method will take effect only the next time the popup is shown or through a manual call to one of the

Parameters:

touchable true if the popup should receive outside touch events, false otherwise

See Also:

2).若是想设置点击PopupWindow外事件不取消PopupWindow的话,必须以下设置,

PopupWindow.setTouchable(true);//默认

PopupWindow.setFocusable(false);//取消获取焦点,这会致使没法弹出输入法

PopupWindow.setOutsideTouchable(false);

3).看PopupWindow源码知,点击PopupWindow外面区域,自动dismiss,须要调用

PopupWindow.setBackgroundDrawable(new ColorDrawable());//这个很关键

PopupWindow.setOutsideTouchable(true);

4).须要的效果之一,点击点击PopupWindow外面区域,不dismiss对话框,而且点击弹出窗口的输入组件会弹出输入法。(设置PopupWindow.setFocusable(true))

View contentView = LayoutInflater.from(getContext()).inflate(

R.layout.widget_window_security, null);

// 重写onKeyListener,获取焦点的视图上捕获返回键,取消弹出窗口

contentView.setOnKeyListener(new OnKeyListener() {

@Override

public boolean onKey(View v, int keyCode, KeyEvent event) {

if (keyCode == KeyEvent.KEYCODE_BACK) {

if (securityWindow != null) {

securityWindow.dismiss();

}

return true;

}

return false;

}

});

Button mBtnCancel = (Button)contentView.findViewById(R.id.window_btn_cancel);

mBtnCancel.setOnClickListener(this);

Button mBtnFinish = (Button)contentView.findViewById(R.id.window_btn_finish);

mBtnFinish.setOnClickListener(this);

mEditPwd = (EditText) contentView.findViewById(R.id.window_input_pwd);

//若是还想实现返回键退出窗口效果,只能是在获取焦点的视图上捕捉返回按钮,或者是Activity捕捉返回按钮。

mEditPwd .setOnKeyListener(new OnKeyListener() {

@Override

public boolean onKey(View v, int keyCode, KeyEvent event) {

if (keyCode == KeyEvent.KEYCODE_BACK) {

if (securityWindow != null) {

securityWindow.dismiss();

}

return true;

}

return false;

}

});

contentView.setFocusable(true); // 这个很重要

contentView.setFocusableInTouchMode(true);

securityWindow = new PopupWindow(contentView, LayoutParams.WRAP_CONTENT,

LayoutParams.WRAP_CONTENT, true);

//securityWindow.setBackgroundDrawable(new ColorDrawable());//这句不能加,加了点击空白处就取消窗口啦

securityWindow.setInputMethodMode(PopupWindow.INPUT_METHOD_NEEDED);

securityWindow

.setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_PAN);

securityWindow.setFocusable(true);//设置容许获取焦点,能够弹出输入法

securityWindow.setWidth(UIHelper.dip2px(getContext(), 520));

securityWindow.setHeight(UIHelper.dip2px(getContext(), 250));

securityWindow.showAtLocation(this, Gravity.CENTER, 0, 0);

5).看PopupWindow源码知,PopupWindow的PopupViewContainer类是私有的,用来控制显示和处理点击事件,包括点击窗口外取消窗口,无法重写它来处理点击窗口外取消窗口事件。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值