Android:创建能输入内容的常见对话框以及使用对话框实现逻辑处理

xiaoxiao2021-02-28  48

Android开发中,Toast已经不能满足需求了,弹出对话框的需求操作越来越频繁,这里 提供一个拿来就用的方法 使用xml文件填充的dialog,应该能满足大部分需求了

如下:

// 显示对话框 public void showWaiterAuthorizationDialog() { // LayoutInflater是用来找layout文件夹下的xml布局文件,并且实例化 LayoutInflater factory = LayoutInflater.from(getContext()); // 把布局文件中的控件定义在View中 final View textEntryView = factory.inflate(R.layout.activity_mobile_authentication_check_code, null); // 将自定义xml文件中的控件显示在对话框中 new AlertDialog.Builder(getContext()) // 对话框的标题 .setTitle("验证码") // 设定显示的View .setView(textEntryView) // 对话框中的“完成”按钮的点击事件 .setPositiveButton("完成", new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int whichButton) { // 获取用户输入的验证码 // 注意:textEntryView.findViewById很重要,因为上面factory.inflate(R.layout.activity_mobile_authentication_check_code, // null)将页面布局赋值给了textEntryView了 final EditText et_check_code = (EditText) textEntryView.findViewById(R.id.et_check_code); // 将页面输入框中获得的数据转为字符串 String checkCode = et_check_code.getText().toString().trim(); // 现在为止已经获得了字符型的用户名和密码了,接下来就是根据自己的需求来编写代码了 } }) // 对话框的“退出”单击事件 .setNegativeButton("退出", new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int whichButton) { //不做操作,关闭对话框 } }) // 设置dialog是否为模态,false表示模态,true表示非模态 .setCancelable(false) // 对话框的创建、显示 .create().show(); }

xml文件这里就不上传了,获取值  我也提供了一个……

哪里需要,那里调用……

转载请注明原文地址: https://www.6miu.com/read-83188.html

最新回复(0)