5.综合运用:实现用户登录

描述:

  实现一个登陆界面,并验证用户的登陆是否正确,探出相应的对话框。

目标页面的效果



静态布局文件设计

[html]  view plain  copy
 print ?
  1. <span style="font-size:18px;"><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  2.     xmlns:tools="http://schemas.android.com/tools"  
  3.     android:id="@+id/LinearLayout1"  
  4.     android:layout_width="match_parent"  
  5.     android:layout_height="match_parent"  
  6.     android:background="@drawable/bg"  
  7.     android:orientation="vertical"  
  8.     android:paddingBottom="@dimen/activity_vertical_margin"  
  9.     android:paddingLeft="@dimen/activity_horizontal_margin"  
  10.     android:paddingRight="@dimen/activity_horizontal_margin"  
  11.     android:paddingTop="@dimen/activity_vertical_margin"  
  12.     tools:context=".MainActivity" >  
  13.   
  14.     <TextView  
  15.         android:id="@+id/textView1"  
  16.         android:layout_width="wrap_content"  
  17.         android:layout_height="wrap_content"  
  18.         android:layout_marginLeft="20dp"  
  19.         android:layout_marginTop="180dp"  
  20.         android:text="用户名:"  
  21.         android:textAppearance="?android:attr/textAppearanceLarge" />  
  22.   
  23.     <EditText  
  24.         android:id="@+id/editText1"  
  25.         android:layout_width="match_parent"  
  26.         android:layout_height="wrap_content"  
  27.         android:layout_marginLeft="20dp"  
  28.         android:ems="10">  
  29.         <requestFocus />  
  30.     </EditText>  
  31.   
  32.     <TextView  
  33.         android:id="@+id/textView2"  
  34.         android:layout_width="wrap_content"  
  35.         android:layout_height="wrap_content"  
  36.         android:text="密码:"  
  37.         android:layout_marginLeft="20dp"  
  38.         android:textAppearance="?android:attr/textAppearanceLarge" />  
  39.   
  40.     <EditText  
  41.         android:id="@+id/userPasswd"  
  42.         android:layout_width="match_parent"  
  43.         android:layout_height="wrap_content"  
  44.         android:ems="10"  
  45.         android:layout_marginLeft="20dp"  
  46.         android:inputType="textPassword" />  
  47.   
  48.     <LinearLayout  
  49.         android:layout_width="match_parent"  
  50.         android:layout_height="wrap_content"   
  51.         android:layout_marginTop="20dp"  
  52.         android:layout_weight="0.26">  
  53.   
  54.         <Button  
  55.             android:id="@+id/login"  
  56.             android:layout_width="wrap_content"  
  57.             android:layout_height="wrap_content"  
  58.             android:layout_marginLeft="20dp"  
  59.             android:layout_weight="0.10"  
  60.               
  61.             android:text="登录" />  
  62.   
  63.         <Button  
  64.             android:id="@+id/cancel"  
  65.             android:layout_width="wrap_content"  
  66.             android:layout_height="wrap_content"  
  67.             android:layout_marginLeft="30dp"  
  68.             android:layout_weight="0.10"  
  69.             android:text="取消" />  
  70.   
  71.     </LinearLayout>  
  72.   
  73. </LinearLayout>  
  74. </span>  



逻辑代码实现

[java]  view plain  copy
 print ?
  1. <pre code_snippet_id="2578214" snippet_file_name="blog_20170914_2_3530214" name="code" class="java">package com.example.myloginapp;  
  2.   
  3. import android.os.Bundle;  
  4. import android.app.Activity;  
  5. import android.app.AlertDialog;  
  6. import android.app.AlertDialog.Builder;  
  7. import android.view.Menu;  
  8. import android.view.View;  
  9. import android.view.View.OnClickListener;  
  10. import android.widget.Button;  
  11. import android.widget.EditText;  
  12.   
  13. public class MainActivity extends Activity {  
  14.       
  15.     Button login;  
  16.     Button cancle;  
  17.     EditText id;  
  18.     EditText pw;  
  19.     Builder dialog;  
  20.   
  21.     @Override  
  22.     protected void onCreate(Bundle savedInstanceState)  
  23.     {  
  24.         super.onCreate(savedInstanceState);  
  25.         setContentView(R.layout.activity_main);  
  26.           
  27.         login=(Button)findViewById(R.id.login);  
  28.         cancle=(Button)findViewById(R.id.cancel);  
  29.         id=(EditText)findViewById(R.id.editText1);  
  30.         pw=(EditText)findViewById(R.id.userPasswd);  
  31.         dialog =new AlertDialog.Builder(this);  
  32.           
  33.         login.setOnClickListener(new OnClickListener()   
  34.         {  
  35.             public void onClick(View arg0)   
  36.             {  
  37.                 //获取账户和密码,并核实  
  38.                 String userId=id.getText().toString();  
  39.                 String userPw=pw.getText().toString();  
  40.                   
  41.                 if(userId.equals("Dog")  
  42.                     && userPw.equals("5201314"))   
  43.                 {  
  44.                     dialog.setTitle("登录成功~")  
  45.                     .setMessage("好狗狗上线啦~")  
  46.                     .setPositiveButton("确定",null)  
  47.                     .show();  
  48.                 }  
  49.             else  
  50.             {  
  51.                 dialog.setTitle("登录失败~")  
  52.                 .setMessage("您的账号或者密码不正确!!!")  
  53.                 .setPositiveButton("确定",null)  
  54.                 .show();  
  55.             }  
  56.         }  
  57.     });;  
  58.           
  59.         cancle.setOnClickListener(new OnClickListener()   
  60.         {  
  61.             public void onClick(View arg0)   
  62.             {  
  63.                 dialog.setTitle("退出成功~")  
  64.   
  65.                 .setMessage("狗狗再见~")  
  66.   
  67.                 .setPositiveButton("确定",null)  
  68.   
  69.                 .show();  
  70.             }  
  71.         });;  
  72.     }  
  73.   
  74.   
  75.     @Override  
  76.     public boolean onCreateOptionsMenu(Menu menu) {  
  77.         // Inflate the menu; this adds items to the action bar if it is present.  
  78.         getMenuInflater().inflate(R.menu.main, menu);  
  79.         return true;  
  80.     }  
  81.       
  82. }  
  83. </pre><br>  
  84. <br>  
  85. <pre></pre>  
  86. <div></div>  
  87. <div></div>  
  88. <h1><a name="t5"></a>运行效果</h1>  
  89. <pre></pre>  
  90. <pre></pre>  
  91. <pre></pre>  



参考链接:
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值