虽然是非常简单的自定义dialog Demo而已,但是总感觉从上一个项目中复制粘贴闹心,于是所幸把代码提出来整成工具类,下次直接从博客里复制粘贴罢了,俺这个人就是懒,废话少说,先看看运行效果。

           

先看看集成的DialogHelper。

package com.example.keranbin.dialogdemo;

import android.app.Activity;
import android.app.AlertDialog;
import android.app.Dialog;
import android.graphics.drawable.BitmapDrawable;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.PopupWindow;
import android.widget.TextView;

import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;

/**
 * Created by keranbin on 2016/7/18.
 */
public class DialogHelper {
    /*
    * layout 自定义的dialog样式
    * listViewId 自定义的listView Id
    * backId 自定义的自定义的dialog样式取消键Id
    * titleId 自定义的自定义的dialog样式标题文字Id
    * jsonList 要在listView上显示的数据
    * 获取Json数据中某个属性key
    * */
    public static void createListViewlDialog(Activity context, int layout, int listViewId, int backId, int titleId, final ArrayList<JSONObject> jsonList, String titleName, final DialogListviewListener dialogListviewListener) throws JSONException {

        final Dialog builder = new AlertDialog.Builder(context).create();  //先得到构造器
        final View view = LayoutInflater.from(context).inflate(layout, null);
        ListView listView = (ListView) view.findViewById(listViewId);
        //监听返回键
        ((ImageView) view.findViewById(backId)).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                builder.dismiss();
            }
        });
        //设置dialog的标题
        ((TextView) view.findViewById(titleId)).setText(jsonList.get(0).getString(titleName));

        listView.setAdapter(new SelectNextPeopleAdapter(context, jsonList.subList(1, jsonList.size()), titleName));
        listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                //回调所选数据,方便用户进行下一步操作
                dialogListviewListener.setOnlistViewItemClick(jsonList.get(position + 1));
                builder.dismiss();
            }
        });
        builder.show();
        builder.setCancelable(false);//取消底部返回键事件
        builder.getWindow().setContentView(view);
        builder.getWindow().setLayout(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
    }


    /*
    * @context 上下文对象
    * @layout 自定义的dialog样式布局文件
    * @sureId 确定按钮的Id
    * @cancelId 取消按钮的Id
    * @titleId 对话框Textview 的id
    * @title 对话框显示的文本
    * @position 考虑到可能是点击列表的某一项,比如说删除某一项,提示用户是否确定删除,传入下标position,
    *           在回调接口中返回给用户,方便用户获取列表点击项的数据,如果不是这种情况,则可随便传个数值即可,
    * @dialogPromptListener 回调接口
    * */
    public static void createPromptDialog(Activity context, int layout, int sureId, int cancelId, int titleId, String title, final int position, final DialogPromptListener dialogPromptListener) {
        final Dialog builder = new AlertDialog.Builder(context).create();  //先得到构造器
        View view = LayoutInflater.from(context).inflate(layout, null);
        ((TextView) view.findViewById(titleId)).setText(title);
        //监听返回键
        ((Button) view.findViewById(cancelId)).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                builder.dismiss();
            }
        });

        ((Button) view.findViewById(sureId)).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                dialogPromptListener.setOnPrompItemClick(position);
                builder.dismiss();
            }
        });
        builder.show();
        builder.setCancelable(false);//取消底部返回键事件
        builder.getWindow().setContentView(view);
        builder.getWindow().setLayout(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
    }


    public interface DialogListviewListener {
        public void setOnlistViewItemClick(JSONObject jsonObject);
    }

    public interface DialogPromptListener {
        public void setOnPrompItemClick(int position);
    }

}

MainActivity 调用代码

package com.example.keranbin.dialogdemo;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.TextView;
import android.widget.Toast;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import org.json.JSONTokener;

import java.util.ArrayList;

public class MainActivity extends Activity implements DialogHelper.DialogListviewListener,DialogHelper.DialogPromptListener{


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        ((TextView) findViewById(R.id.tvDialog)).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                    showApprovalDialog();
            }
        });

        ((TextView)findViewById(R.id.tvDelete)).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                showDeleteDialog();
            }
        });
    }

    private void showApprovalDialog() {
        try {
            DialogHelper.createListViewlDialog(
                    MainActivity.this,
                    R.layout.dialog_select_approval_people,
                    R.id.lv_select_approval_people,
                    R.id.dialoBack,
                    R.id.titledialog,
                    initData(),
                    "name",
                    this
            );
        } catch (JSONException e) {
            e.printStackTrace();
        }
    }

    private ArrayList<JSONObject> initData() throws JSONException {
        ArrayList<JSONObject> jsonList = new ArrayList<JSONObject>();
        String strJson = "{\"ryList\":[{\"name\":\"请选择审批人\"},{\"name\":\"张三\"},{\"name\":\"李四\"},{\"name\":\"王五\"},{\"name\":\"王八\"},{\"name\":\"小明\"}]}";
        JSONArray jsonArray = (JSONArray) new JSONTokener(new JSONObject(strJson).getString("ryList")).nextValue();
        for (int i = 0; i < jsonArray.length(); i++) {
            jsonList.add(new JSONObject(jsonArray.get(i).toString()));
        }
        return jsonList;
    }

    @Override
    public void setOnlistViewItemClick(JSONObject jsonObject) {
        try {
            Toast.makeText(MainActivity.this, jsonObject.getString("name"), Toast.LENGTH_LONG).show();
        } catch (JSONException e) {
            e.printStackTrace();
        }
    }


    private void showDeleteDialog() {
        DialogHelper.createPromptDialog(MainActivity.this,
                R.layout.dialog_delete,
                R.id.btnRight,
                R.id.btnLeft,
                R.id.titledialog,
                "确定删除该条信息?",
                1,
                this
        );
    }

    @Override
    public void setOnPrompItemClick(int position) {
        Toast.makeText(MainActivity.this,"您成功删除了第"+position+"条数据",Toast.LENGTH_LONG).show();
    }
}

自定义的dialogListview样式xml布局文件

 1 <?xml version="1.0" encoding="utf-8"?>
 2 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
 3     android:layout_width="match_parent"
 4     android:layout_height="wrap_content"
 5     android:background="#FFF"
 6     android:orientation="vertical">
 7 
 8     <RelativeLayout
 9         android:layout_width="match_parent"
10         android:layout_height="40dip"
11         android:background="#00BFB0"
12         android:gravity="center_vertical">
13 
14         <ImageView
15             android:layout_width="25dp"
16             android:layout_height="25dp"
17             android:id="@+id/dialoBack"
18             android:src="@drawable/topico03"
19             android:layout_centerVertical="true"/>
20         <TextView
21             android:id="@+id/titledialog"
22             android:layout_width="match_parent"
23             android:layout_height="40dip"
24             android:layout_centerInParent="true"
25             android:gravity="center"
26             android:text="请选择"
27             android:textColor="#FFF"
28             android:textSize="18sp"
29             android:textStyle="bold" />
30     </RelativeLayout>
31 
32     <ListView
33         android:id="@+id/lv_select_approval_people"
34         android:layout_width="match_parent"
35         android:layout_height="match_parent"></ListView>
36 
37 </LinearLayout>

listview item布局就更简单啦

 1 <?xml version="1.0" encoding="utf-8"?>
 2 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
 3     android:layout_width="match_parent"
 4     android:layout_height="wrap_content">
 5     <TextView
 6         android:id="@+id/dialogItem"
 7         android:layout_width="match_parent"
 8         android:layout_height="40dip"
 9         android:gravity="center_vertical"
10         android:paddingLeft="20dip"
11         android:textSize="15sp"
12         android:textColor="#000"/>
13 </LinearLayout>

 自定义的dialogPrompt样式xml布局文件

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:background="#00000000"
    android:orientation="vertical">

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="40dip"
        android:background="@drawable/dialog_del_top_bkg"
        android:gravity="center_vertical">

        <TextView
            android:id="@+id/titledialog"
            android:layout_width="match_parent"
            android:layout_height="40dip"
            android:layout_centerInParent="true"
            android:gravity="center"
            android:text="请选择"
            android:textColor="#FFF"
            android:textSize="18sp"
            android:textStyle="bold" />
    </RelativeLayout>

    <LinearLayout
        android:id="@+id/rl_activity_bottom_bar"
        android:layout_width="match_parent"
        android:layout_height="100dp"
        android:background="@drawable/dialog_del_bottom_bkg"
        android:orientation="horizontal">

        <RelativeLayout
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight="2"
            android:gravity="center">

            <Button
                android:id="@+id/btnLeft"
                android:layout_width="150dp"
                android:layout_height="wrap_content"
                android:background="#E67e22"
                android:text="取消"
                android:textColor="#ffffff"
                android:textSize="18dp" />
        </RelativeLayout>

        <RelativeLayout
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight="2"
            android:gravity="center">

            <Button
                android:id="@+id/btnRight"
                android:layout_width="150dp"
                android:layout_height="wrap_content"
                android:background="#3c6b93"
                android:text="确定"
                android:textColor="#ffffff"
                android:textSize="20dp" />
        </RelativeLayout>
    </LinearLayout>


</LinearLayout>