Dialog相关用法

在这里只要是对Dialog、AlertDialog、PopupWindow和DialogFragment的用法进行举例说明

值得注意的是DialogFragment是在android3.0之后才引进的,很有意思,为什么在3.0之后才引入DialogFragment呢?所以在这里不得不说明一下DialogFragment的优点:

1、有着和Fragment基本一样的生命周期,activity更好的管理DialogFragment

2、屏幕旋转时,DialogFragment自动灵活调整自身大小

3、在屏幕旋转时,解决了AlertDialog和PopupWindow随屏幕切换而消失的不足

需注意的是:使用DialogFragment至少需要实现onCreateView 或者onCreateDialog方法

在onCreateView即使用定义的xml布局文件展示Dialog

在onCreateDialog可以利用AlertDialog或者Dialog创建出Dialog


DialogFragment用onCreateView创建

public class OnCreateViewDialogFragment extends DialogFragment {

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
{
getDialog().requestWindowFeature(Window.FEATURE_NO_TITLE);
View view = inflater.inflate(R.layout.fragment_edit_name, container,false);
return view;
}

}


DialogFragment用onCreateDialog创建

public class LoginDialogFragment extends DialogFragment {
private EditText mUsername;
private EditText mPassword;

public interface LoginInputListener
{
void onLoginInputComplete(String username, String password);
}

@Override
public Dialog onCreateDialog(Bundle savedInstanceState)
{
AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
LayoutInflater inflater = getActivity().getLayoutInflater();
View view = inflater.inflate(R.layout.fragment_login_dialog, null);
mUsername = (EditText) view.findViewById(R.id.id_txt_username);
mPassword = (EditText) view.findViewById(R.id.id_txt_password);
builder.setView(view)
// 添加相应事件
.setPositiveButton("登录",
new DialogInterface.OnClickListener()
{
@Override
public void onClick(DialogInterface dialog, int id)
{
LoginInputListener listener = (LoginInputListener) getActivity();
listener.onLoginInputComplete(mUsername
.getText().toString(), mPassword
.getText().toString());
}
}).setNegativeButton("取消", null);
return builder.create();
}
}


上面的都是DialogFragment形式,比较容易就不多说了。接下来看一下PopupWindow,在做PopupWindow的demo时,遇到很多问题,然后度娘一把,下面是自己总结的一个例子,直接上代码

/**
* 显示popupWindow
*/
PopupWindow popWindow = null;
View popView = null;
private void showPopwindow() {
if (popWindow == null || popView == null){
LayoutInflater inflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
popView = inflater.inflate(R.layout.popwindowlayout, null);
popWindow = new PopupWindow(popView,
WindowManager.LayoutParams.MATCH_PARENT,
WindowManager.LayoutParams.WRAP_CONTENT);
}

/**
* 在这里重点强调:
* 1、如果要点击popWindow以外部分,要popWindow消失的话,
* 这个popWindow.setBackgroundDrawable必须得调用
*
* 2、如果要点击popWindow以外部分,popWindow不消失,
* 则popWindow.setOutsideTouchable就不能设置,要改变popupWindow背景色,只能在xml那里去设置,这个需要注意一下
* 还有个问题:如果不设置PopupWindow的背景,无论是点击外部区域还是Back键都无法dismiss弹框
* 我觉得这里是API的一个bug
*/
// popWindow.setBackgroundDrawable(new BitmapDrawable());
popWindow.setFocusable(true); // 设置popWindow弹出窗体可点击,这句话必须添加,并且是true
popWindow.setTouchable(true); //popupWindow是否可点
popWindow.setOutsideTouchable(false);

popView.setFocusable(true);//设置view能够监听事件,标注1
popView.setFocusableInTouchMode(true); //设置view能够监听事件 标注2

//操作相关控件,然后按返回键监听事件。如果不作相关控件返回键监听,按返回键没有任何效果【注:在自己的程序中,直接实现这个接口即可,这里就这样写了】
popView.setOnKeyListener(new View.OnKeyListener(){
@Override
public boolean onKey(View view, int keyCode, KeyEvent keyEvent) {
if (keyCode == KeyEvent.KEYCODE_BACK){
if(popWindow != null) {
popWindow.dismiss();
}
}
return false;
}
});

// 在参照的View控件下方显示
popWindow.showAsDropDown(MainActivity.this.findViewById(R.id.popup_window_layout_btn));

// 设置popWindow的显示和消失动画
popWindow.setAnimationStyle(R.style.mypopwindow_anim_style);
// 在底部显示
// popWindow.showAtLocation(MainActivity.this.findViewById(R.id.popup_window_layout_btn),
// Gravity.CENTER, 0, 0);

// 这里检验popWindow里的button是否可以点击
Button button1 = (Button) popView.findViewById(R.id.button1);
button1.setOnClickListener(new View.OnClickListener() {

@Override
public void onClick(View v) {
popWindow.dismiss();
Toast.makeText(MainActivity.this,"第一个按钮被点击了",Toast.LENGTH_SHORT).show();
}
});

//操作相关控件,然后按返回键监听事件。如果不作相关控件返回键监听,按返回键没有任何效果
popView.findViewById(R.id.second_et).setOnKeyListener(new View.OnKeyListener() {
@Override
public boolean onKey(View v, int keyCode, KeyEvent event) {
if (keyCode == KeyEvent.KEYCODE_BACK){
if(popWindow != null) {
popWindow.dismiss();
}
}
return false;
}
});


}

上面代码说到了popupWindow两种常用的用法

1、点击popupWindow之外地方,popupWindow消失,主要设置setFocusable 和 setBackgroundDrawable

2、点击popupWindow之外地方,popupWindow不消失,一定不能设置setBackgroundDrawable这个方法,如果要设置popupWindow背景色,只能在xml中设置

主要还是看上面代码,里面的注释比较清楚了,这里就不在解释了。


Dialog系列的,这里就不给出来了,有兴趣的可以下载源码看看

最后总结一下PopupWindow和Dialog用法

1、Popupwindow在显示之前一定要设置宽高,Dialog无此限制。

2、Popupwindow默认不会响应物理键盘的back,除非显示设置了popup.setFocusable(true);而在点击back的时候,Dialog会消失。
3、Popupwindow不会给页面其他的部分添加蒙层,而Dialog会。
4、Popupwindow没有标题,Dialog默认有标题,可以通过dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);取消标题
5、二者显示的时候都要设置Gravity。如果不设置,Dialog默认是Gravity.CENTER。
6、二者都有默认的背景,都可以通过setBackgroundDrawable(new ColorDrawable(android.R.color.transparent));去掉。

在开发时候到底是用PopupWindow还是Dialog根据自己需求来定,仁者见仁智者见智,下面提供一下源码

源码下载



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值