Android自定义一个对话框,Android一个简单的自定义对话框制作
布局文件xmlns:tools="http://schemas.android.com/tools"android:layout_width="match_parent" android:layout_height="match_parent">android:layout_width="397dp"android:layout_height="131dp">android:id="@
布局文件
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent" android:layout_height="match_parent">
android:layout_width="397dp"
android:layout_height="131dp">
android:id="@ id/namet"
tools:text="姓名">
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();
}
});
效果图:
更多推荐
所有评论(0)