zoukankan      html  css  js  c++  java
  • Android中的AlertDialog使用示例五(自定义对话框)

    在Android开发中,我们经常会需要在Android界面上弹出一些对话框,比如询问用户或者让用户选择。这些功能我们叫它Android Dialog对话框,AlertDialog实现方法为建造者模式。AlertDialog中定义的一些对话框往往无法满足我们关于对话框的需求,这时我们就需要通过自定义对话框VIEW来实现需求,这里我自定义一个登陆的提示对话框,效果图显示如下:

    
    
    Layout(alertdialog自定义登陆按钮)界面代码:
     1 <?xml version="1.0" encoding="utf-8"?>
     2 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
     3     android:orientation="vertical" android:layout_width="match_parent"
     4     android:layout_height="match_parent">
     5     <Button
     6         android:text="自定义登陆"
     7         android:layout_width="match_parent"
     8         android:layout_height="wrap_content"
     9         android:id="@+id/button5"
    10         android:onClick="login"/>
    11 </LinearLayout>
    
    

    Layoutlogin_layout登陆窗口)界面:

     

     1 <?xml version="1.0" encoding="utf-8"?>
     2 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
     3     android:orientation="vertical" android:layout_width="match_parent"
     4     android:layout_height="match_parent">
     5     <EditText
     6         android:layout_width="match_parent"
     7         android:layout_height="wrap_content"
     8         android:inputType="text"
     9         android:hint="请输入用户名"
    10         android:id="@+id/et_username"/>
    11     <EditText
    12         android:layout_width="match_parent"
    13         android:layout_height="wrap_content"
    14         android:inputType="textPassword"
    15         android:hint="请输入密码"
    16         android:id="@+id/et_password"/>
    17 </LinearLayout>
    java功能实现代码:
     1 public class AlertDialogDemo extends AppCompatActivity {
     2     private EditText et_username,et_password;
     3     @Override
     4     protected void onCreate(@Nullable Bundle savedInstanceState) {
     5         super.onCreate(savedInstanceState);
     6         setContentView(R.layout.alertdialog);
     7     }
     8     public void login(View v){
     9         AlertDialog.Builder builder = new AlertDialog.Builder(this);
    10         builder.setTitle("登录"); 
    11         //通过布局填充器获login_layout
    12         View view = getLayoutInflater().inflate(R.layout.login_layout,null); 
    13         //获取两个文本编辑框(密码这里不做登陆实现,仅演示)
    14         final EditText et_username = (EditText) view.findViewById(R.id.et_username);
    15         final EditText et_password = (EditText) view.findViewById(R.id.et_password);
    16         builder.setView(view);//设置login_layout为对话提示框
    17         builder.setCancelable(false);//设置为不可取消
    18         //设置正面按钮,并做事件处理
    19         builder.setPositiveButton("确定", new DialogInterface.OnClickListener() {
    20             @Override
    21             public void onClick(DialogInterface dialogInterface, int i) {
    22                 String name = et_username.getText().toString().trim();
    23                 String pass = et_password.getText().toString().trim();
    24                 Toast.makeText(AlertDialogDemo.this,name + "正在登录....",Toast.LENGTH_SHORT).show();
    25             }
    26         });
    27         //设置反面按钮,并做事件处理
    28         builder.setNegativeButton("取消", new DialogInterface.OnClickListener() {
    29             @Override
    30             public void onClick(DialogInterface dialogInterface, int i) {
    31                 Toast.makeText(AlertDialogDemo.this,"取消登录",Toast.LENGTH_SHORT).show();
    32             }
    33         });
    34         builder.show();//显示Dialog对话框
    35     }
    36 }

     

    
    
  • 相关阅读:
    oracle 11g完全彻底的卸载
    Windows添加.NET Framework 3.0 NetFx3 失败
    Crontab中的除号(slash)到底怎么用?
    Codeigniter文件上传类型不匹配错误
    Mac下遇到 'reading initial communication packet’ 问题
    使用PHP的正则抓取页面中的网址
    关于Advertising Campaign
    20个Linux服务器安全强化建议(三)
    20个Linux服务器安全强化建议(二)
    20个Linux服务器安全强化建议(一)
  • 原文地址:https://www.cnblogs.com/panhouye/p/6107563.html
Copyright © 2011-2022 走看看