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(); } });
效果图: