Android中如何使用自定义对话框
16lz
2021-01-24
自定义创建一个XML布局
<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="match_parent" android:layout_height="match_parent"> <TextView android:layout_width="wrap_content" android:layout_height="fill_parent" android:text="请输入VIP账号" /> <EditText android:layout_width="fill_parent" android:layout_height="fill_parent" android:id="@+id/text_vip" /></LinearLayout>
给Button设置Click事件,将下面代码放入到Click事件中
AlertDialog.Builder myDialog = new AlertDialog.Builder(MainActivity.this); final View v = getLayoutInflater().inflate(R.layout.dialoglayout,null); //创建一个View 去获取刚才自定义创建的XML布局 myDialog.setTitle("自定义的对话框"); myDialog.setView(v); //把刚才的View设置到myDialog 中 myDialog.setPositiveButton("登录", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { EditText edt_vip=(EditText)v.findViewById(R.id.text_vip); //刚才布局中的控件,通过Id进行绑定 if(edt_vip.getText().toString().equals("123456")){ //如果是123456就是会员其他的为普通用户 ShowMessage("欢迎至尊会员"); } else{ ShowMessage("欢迎光临"); } } }); myDialog.create().show(); //创建运行
结果:
至尊会员输入123456
点击后
普通用户
点击后
更多相关文章
- layout_weight 权重
- BaseRecyclerViewAdapterHelper
- android:scrollbars属性和弹出键盘的问题
- Android布局自定义Shap圆形ImageView,可以单独设置背景与图片
- Android(安卓)性能典范:拯救计划
- Android第一步
- Android(安卓)如何在代码中动态的添加View 及 指定位置
- Android(安卓)学习 之 API文档汇总(不断更新)
- Android初步