这种对话框的特点:自定义布局,统一接口,可任意设置为全屏。根据不同的应用场景传入不同的布局文件和对应的onClickListener的list即可
这种对话框的应用场景:
* 纯android java应用必备,作为一个ui工具类,可以避免重复造轮子
* 使用了jni的android应用,但是在弹框时,不方便由C++/C层弹框,同时又认为android原生的对话框与整个应用的图片风格不匹配。
* 封装一个特定功能的SDK,比如写一个集成了登录支付退出等功能的第三方SDK,那么登录框就可以用这个来写,同时还可以用在切换账号框、退出框、支付框等等上面。
以下代码是从我写的一个SDK中截取下来的,代码如下:
import java.util.ArrayList;
import android.app.DialogFragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.view.Window;
import android.widget.Button;
public class FullScreenDialogFragment extends DialogFragment {
private ArrayList<OnClickListener> mListeners = null;//布局文件中的按钮的 监听
private int mResource = -1;//布局文件的 source id
public FullScreenDialogFragment(int resource){
super();
mResource = resource;
}
private void getButtons(ViewGroup container){
//遍历所有button进行监听
for(int i=0, j=0; i<container.getChildCount(); i++){
View view = container.getChildAt(i);
if(view instanceof Button){
Button btn = (Button) view;
btn.setOnClickListener(mListeners.get(j));
j++;
}else if( view instanceof ViewGroup){
this.getButtons((ViewGroup)view);//递归寻找设置监听
}
}
}
public void setBtnClickListener(ArrayList<OnClickListener> listeners){
mListeners = listeners;
}
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
getDialog().requestWindowFeature(Window.FEATURE_NO_TITLE); //这里设置的是无标题style
View view = inflater.inflate(mResource, container,
false);
this.getButtons((ViewGroup)view);
return view;
}
}
以上就是一个ui工具类,使用方式也比较简单
* 根据布局文件new一个对话框
* 设置按键的监听
* show
private void login(){
fullScreenDialogFragment = new FullScreenDialogFragment(R.layout.login_select_dialog);
ArrayList<OnClickListener> listeners = new ArrayList<OnClickListener>();
listeners.add(new OnClickListener(){
@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
//handle your logic
fullScreenDialogFragment.dismiss();
fullScreenDialogFragment = null;
}
});
listeners.add(new OnClickListener(){
@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
fullScreenDialogFragment.dismiss();
fullScreenDialogFragment = null;
}
});
fullScreenDialogFragment.setBtnClickListener(listeners);
fullScreenDialogFragment.show(mActivity.getFragmentManager(), "XXX");
}
需要注意的是按键的监听的顺序,是根据布局文件的顺序进行的,换句话说,在布局文件中把一个按键写在最前面的位置,那么这个按键的监听就会是ArrayList的index为0的位置的监听。
另外,再附上我demo里的布局文件吧
login_select_dialog.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/login_dialog"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="@drawable/login_background"
>
<Button
android:id="@+id/login_shouq"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
android:layout_marginBottom="75dp"
android:layout_marginLeft="111dp"
android:background="@drawable/login_qq"
/>
<Button
android:id="@+id/login_wechat"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBottom="@+id/login_shouq"
android:layout_alignParentRight="true"
android:layout_marginRight="92dp"
android:background="@drawable/login_wechat"
/>
</RelativeLayout>