快速创建弹窗PopupWindow、提示框AlertDialog、列表对话框Dialog

26 篇文章 0 订阅
26 篇文章 0 订阅

快速创建弹窗PopupWindow、提示框AlertDialog、列表对话框Dialog


1.创建PopupWindow:

01.创建PopupWindow的布局文件:
注意:设置了多个Item,不需要时可以选择隐藏

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@color/color_tran">

    <LinearLayout
        android:layout_width="@dimen/dp_320"
        android:layout_height="wrap_content"
        android:background="@drawable/dialog_bg"
        android:orientation="vertical">

        <RelativeLayout
            android:id="@+id/layout1"
            android:layout_width="match_parent"
            android:layout_height="@dimen/dp_60">

            <com.hikvision.showexamplepad101.widget.TextDrawable
                android:id="@+id/tv1"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_alignParentLeft="true"
                android:layout_centerVertical="true"
                android:layout_marginLeft="@dimen/dp_32"
                android:text="IP"
                android:textColor="@color/color_333333"
                android:textSize="@dimen/sp_20" />

            <Switch
                android:id="@+id/switch1"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_alignParentEnd="true"
                android:layout_centerVertical="true"
                android:layout_marginRight="@dimen/dp_32"
                android:checked="false"
                android:visibility="gone" />

        </RelativeLayout>

        <TextView
            android:id="@+id/line1"
            android:layout_width="match_parent"
            android:layout_height="1dp"
            android:layout_marginLeft="@dimen/dp_32"
            android:layout_marginRight="@dimen/dp_32"
            android:background="@color/gray_line" />

        <RelativeLayout
            android:id="@+id/layout2"
            android:layout_width="match_parent"
            android:layout_height="@dimen/dp_60">

            <com.hikvision.showexamplepad101.widget.TextDrawable
                android:id="@+id/tv2"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_alignParentLeft="true"
                android:layout_centerVertical="true"
                android:layout_marginLeft="@dimen/dp_32"
                android:text="账号"
                android:textColor="@color/color_333333"
                android:textSize="@dimen/sp_20" />

            <Switch
                android:id="@+id/switch2"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_alignParentEnd="true"
                android:layout_centerVertical="true"
                android:layout_marginRight="@dimen/dp_32"
                android:checked="false"
                android:visibility="gone" />
        </RelativeLayout>

        <TextView
            android:id="@+id/line2"
            android:layout_width="match_parent"
            android:layout_height="1dp"
            android:layout_marginLeft="@dimen/dp_32"
            android:layout_marginRight="@dimen/dp_32"
            android:background="@color/gray_line"
            android:visibility="gone" />

        <RelativeLayout
            android:id="@+id/layout3"
            android:layout_width="match_parent"
            android:layout_height="@dimen/dp_60"
            android:visibility="gone">

            <com.hikvision.showexamplepad101.widget.TextDrawable
                android:id="@+id/tv3"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_alignParentLeft="true"
                android:layout_centerVertical="true"
                android:layout_marginLeft="@dimen/dp_32"
                android:text="账号"
                android:textColor="@color/color_333333"
                android:textSize="@dimen/sp_20" />

            <Switch
                android:id="@+id/switch3"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_alignParentEnd="true"
                android:layout_centerVertical="true"
                android:layout_marginRight="@dimen/dp_25"
                android:checked="false"
                android:visibility="gone" />
        </RelativeLayout>

        <TextView
            android:id="@+id/line3"
            android:layout_width="match_parent"
            android:layout_height="1dp"
            android:layout_marginLeft="@dimen/dp_32"
            android:layout_marginRight="@dimen/dp_32"
            android:background="@color/gray_line"
            android:visibility="gone" />

        <RelativeLayout
            android:id="@+id/layout4"
            android:layout_width="match_parent"
            android:layout_height="@dimen/dp_60"
            android:visibility="gone">

            <com.hikvision.showexamplepad101.widget.TextDrawable
                android:id="@+id/tv4"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_alignParentLeft="true"
                android:layout_centerVertical="true"
                android:layout_marginLeft="@dimen/dp_32"
                android:text="账号"
                android:textColor="@color/color_333333"
                android:textSize="@dimen/sp_20" />

            <Switch
                android:id="@+id/switch4"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_alignParentEnd="true"
                android:layout_centerVertical="true"
                android:layout_marginRight="@dimen/dp_32"
                android:checked="false"
                android:visibility="gone" />
        </RelativeLayout>

        <TextView
            android:id="@+id/line4"
            android:layout_width="match_parent"
            android:layout_height="1dp"
            android:layout_marginLeft="@dimen/dp_32"
            android:layout_marginRight="@dimen/dp_32"
            android:background="@color/gray_line"
            android:visibility="gone" />

        <RelativeLayout
            android:id="@+id/layout5"
            android:layout_width="match_parent"
            android:layout_height="@dimen/dp_60"
            android:visibility="gone">

            <com.hikvision.showexamplepad101.widget.TextDrawable
                android:id="@+id/tv5"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_alignParentLeft="true"
                android:layout_centerVertical="true"
                android:layout_marginLeft="@dimen/dp_32"
                android:text="账号"
                android:textColor="@color/color_333333"
                android:textSize="@dimen/sp_20" />

            <Switch
                android:id="@+id/switch5"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_alignParentEnd="true"
                android:layout_centerVertical="true"
                android:layout_marginRight="@dimen/dp_32"
                android:checked="false"
                android:visibility="gone" />
        </RelativeLayout>
    </LinearLayout>
</RelativeLayout>

02.创建PopupWindow:

    if (null == cameraControlPopWindow)
    {
        val view = layoutInflater.inflate(R.layout.base_dialog_item_large, null)
        view.line1.visibility = if (confType == ConfType.LOCAL_PTOP) View.GONE else View.VISIBLE
        view.layout2.visibility = if (confType == ConfType.LOCAL_PTOP) View.GONE else View.VISIBLE
        view.tv1.text = "本地会场"
        view.tv2.text = "对方会场"
        view.layout1.setOnClickListener {
           //TODO 点击事件
        }
        view.layout2.setOnClickListener {
          //TODO 点击事件
        }
        cameraControlPopWindow = PopupWindow(
            view,
            ViewGroup.LayoutParams.WRAP_CONTENT,
            ViewGroup.LayoutParams.WRAP_CONTENT,
            true
        )
        //解决6.0以下系统点击外部不会自动消失的问题
        cameraControlPopWindow!!.setBackgroundDrawable(getDrawable())
        cameraControlPopWindow!!.isOutsideTouchable = true
        cameraControlPopWindow!!.isFocusable = true
    }
    if (cameraControlPopWindow!!.isShowing)
    {
        cameraControlPopWindow!!.dismiss()
    } else
    {
        cameraControlPopWindow!!.showAsDropDown(cameraControlIcon, 0, 10, Gravity.BOTTOM)
    }

2.创建AlertDialog:

01.创建AlertDialog的布局文件:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:background="@color/color_tran"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <LinearLayout
        android:layout_width="500dp"
        android:layout_height="wrap_content"
        android:background="@drawable/dialog_bg"
        android:gravity="center_horizontal"
        android:orientation="vertical">

        <TextView
            android:id="@+id/tv"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:paddingLeft="40dp"
            android:paddingRight="40dp"
            android:layout_marginTop="80dp"
            android:layout_marginBottom="70dp"
            android:gravity="center"
            android:text="确定取消会议?"
            android:textColor="@color/black"
            android:textSize="23sp" />

        <TextView
            android:layout_width="match_parent"
            android:layout_height="1dp"
            android:background="@color/gray_line" />

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="60dp"
            android:layout_alignParentBottom="true"
            android:orientation="horizontal">

            <Button
                android:id="@+id/btn_cancel"
                android:layout_width="0dp"
                android:layout_height="match_parent"
                android:layout_weight="1"
                android:background="@drawable/dialog_btn_left"
                android:text="取消"
                android:textColor="@color/color_666666"
                android:textSize="20sp" />

            <TextView
                android:layout_width="1px"
                android:layout_height="match_parent"
                android:background="@color/gray_line" />

            <Button
                android:id="@+id/btn_sure"
                android:layout_width="0dp"
                android:layout_height="match_parent"
                android:layout_weight="1"
                android:background="@drawable/dialog_btn_right"
                android:text="确定"
                android:textColor="@color/color_4194FF"
                android:textSize="20sp" />

        </LinearLayout>
    </LinearLayout>
</RelativeLayout>

02.创建AlertDialog

        val view = layoutInflater.inflate(R.layout.dialog_common, null, false)
        view.tv.text = "确定取消会议?"
        val alertDialog = AlertDialog.Builder(this, R.style.DialogActivityTheme).setView(view).create()
        view.btn_sure.setOnClickListener {
            //调用接口删除终端中的数据
            myConferencePresenter.cancelConference(conferenceId, ivv)
            alertDialog.dismiss()
        }

        view.btn_cancel.setOnClickListener {
            alertDialog.dismiss()
        }
        alertDialog.show()


3.创建列表对话框Dialog:

01.创建列表的适配器Adapter:

public class DialogListAdapter extends BaseAdapter {

    private List<HashMap<String,String>> mapList;
    private LayoutInflater inflater;
    private Context mContext;

    public DialogListAdapter(Context context, List<HashMap<String,String>>list){
        this.mContext = context;
        this.mapList = list;
        inflater = LayoutInflater.from(mContext);

    }

    @Override
    public int getCount() {
        return mapList.size();  //数据源的长度
    }

    @Override
    public Object getItem(int position) {
        return mapList.get(position); //返回数据源的其中某一个对象
    }

    @Override
    public long getItemId(int position) {
        return position; //返回adapter中的其中一个项的id
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        myViewHolder hv;
        if (null==convertView){
            hv = new myViewHolder();
            convertView = inflater.inflate(R.layout.adapter_dialog_view,null,false);
            hv.tvName = convertView.findViewById(R.id.tvName);
            hv.tvCountry = convertView.findViewById(R.id.tvCountry);
            hv.agree = convertView.findViewById(R.id.agree);
            hv.disAgree = convertView.findViewById(R.id.disAgree);
            convertView.setTag(hv);

        }else {
            hv = (myViewHolder)convertView.getTag();
        }
        //一定要判刑断下数据源是否为空,否则很大几率就crash了
        if (mapList!=null && !mapList.isEmpty()){
            hv.tvName.setText(mapList.get(position).get("name"));
            hv.tvCountry.setText(mapList.get(position).get("country"));
            //设置监听事件
            hv.agree.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    Toast.makeText(mContext, mapList.get(position).get("name"), Toast.LENGTH_SHORT).show();
                }
            });
            //设置监听事件
            hv.disAgree.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    Toast.makeText(mContext, mapList.get(position).get("country"), Toast.LENGTH_SHORT).show();
                }
            });
        }
        return convertView;
    }

    class myViewHolder{
        TextView tvName;
        TextView tvCountry;
        Button agree;
        Button disAgree;
    }

}

02.创建列表Dialog:

public class MainActivity extends AppCompatActivity {

    private DialogListAdapter listAdapter;
    private List<HashMap<String, String>> lists = new ArrayList<>();
    String[] names = {"Tom", "jane", "kangkang", "mike"};
    String[] countries = {"china", "japan", "germany", "usa"};

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        initViewDatas();
        findViewById(R.id.bt).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                show_adapter(v);
            }
        });

    }
    /**
     * 初始化adapter并填充数据
     */
    void initViewDatas() {
        listAdapter = new DialogListAdapter(this, lists);
        for (int i = 0; i < 10; i++) {
            HashMap<String, String> map = new HashMap<>();
            map.put("name", names[i % 4]);
            map.put("country", countries[i % 4]);
            lists.add(map);
        }
        listAdapter.notifyDataSetChanged();

    }

    //这是一个button的onclick事件
    public void show_adapter(View view) {

        AlertDialog.Builder adapterBuilder = new AlertDialog.Builder(this);
        adapterBuilder.setIcon(R.mipmap.ic_launcher_round);
        adapterBuilder.setTitle("This is Adapter Dialog");
        ListView listView = new ListView(this);

        adapterBuilder.setView(listView);
        listView.setAdapter(listAdapter);

        //设置适配器,设置监听事件,但是点击后会退出对话框
//        adapterBuilder.setAdapter(listAdapter, new DialogInterface.OnClickListener() {
//            @Override
//            public void onClick(DialogInterface dialog, int which) {
//                HashMap<String, String> selectMap = (HashMap<String, String>) listAdapter.getItem(which);
//                Toast.makeText(MainActivity.this, selectMap.get("name"), Toast.LENGTH_SHORT).show();
//            }
//        });

        adapterBuilder.setPositiveButton("Ok", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                //do what you want
            }
        });

        adapterBuilder.setNegativeButton("Cancle", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                //do what you want
            }
        });
        adapterBuilder.show();
    }
}


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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值