Android一个简单的自定义对话框制作

Posted 溜了溜

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Android一个简单的自定义对话框制作相关的知识,希望对你有一定的参考价值。

布局文件

<?xml version="1.0" encoding="utf-8"?>
<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent" android:layout_height="match_parent">

    <TableRow
        android:layout_width="397dp"
        android:layout_height="131dp">

        <TextView
            android:id="@+id/namet"
            tools:text="姓名">

        </TextView>

        <EditText android:id="@+id/edname"></EditText>
    </TableRow>
</TableLayout>

 Mainactivity中绑定按钮事件(Main布局就不给出了)

final Button diy=(Button)findViewById(R.id.diy);
        diy.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                LayoutInflater flater= LayoutInflater.from(MainActivity.this);
                final View dialogview=flater.inflate(R.layout.login,null);//布局文件转换为view
                Dialog dialog=new AlertDialog.Builder(MainActivity.this).setTitle("登录")//设置标题
                        .setView(dialogview)
                        .setPositiveButton("确定",
                                new DialogInterface.OnClickListener() {
                                    @Override
                                    public void onClick(DialogInterface dialog, int which) {
                           EditText ed=(EditText)dialogview.findViewById(R.id.edname);//获取弹窗中的组件
                                        String msg=ed.getText().toString();
                                                                   Toast.makeText(getApplicationContext(),msg,Toast.LENGTH_SHORT).show();
                                    }
                                }).setNegativeButton("取消", new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialog, int which) {

                            }
                        }).create();
                dialog.show();
            }
        });

  效果图:

 

以上是关于Android一个简单的自定义对话框制作的主要内容,如果未能解决你的问题,请参考以下文章

活动中自定义对话框内的 ANDROID 片段

带有圆角而不是 100% 屏幕宽度的自定义片段对话框

Android FlycoDialog 简单实用的自定义Android弹窗对话框之Dialog篇

为片段制作自定义列表视图?

片段内的自定义列表不起作用

浅谈android中的自定义封装易用的Dialog