上一页介绍Android项目简单的页面跳转实例,算是对开发环境的熟悉,这一篇将在此基础上增加一些简单的逻辑,实现登录的效果。

登录之前:

登录成功:

watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvdTAxMDUwODgyNg==/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/SouthEast" style="width:191px; height:327px">

登录信息有误:

watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvdTAxMDUwODgyNg==/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/Center" alt="" style="width:202px; height:247px">

在主页面上加入两个editText控件,作为用户信息录入的控件。须要做的就是获得这里个editText中的文本。之后对其做一个推断就可以实现登录功能!

相对于之前的页面跳转。仅仅是多加了一个信息验证的推断。体如今代码中就是MainActivity Java类中多了对于用户信息的推断、activity_main
XML文件里多了两个editText控件的代码以及second_main XML文件里对登录成功之后的页面做了一些设计。

MainActivity java代码:

package com.example;

import android.R.color;
import android.os.Bundle;
import android.app.Activity;
import android.app.AlertDialog;
import android.app.Dialog;
import android.content.Intent;
import android.graphics.Color;
import android.graphics.pdf.PdfDocument;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView; public class MainActivity extends Activity {
//定义自己的
private Button btnButton;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
btnButton = (Button)findViewById(R.id.button1);
btnButton.setWidth(200);
btnButton.setHeight(50);
btnButton.setBackgroundColor(Color.RED); btnButton.setOnClickListener(new OnClickListener() { @Override
public void onClick(View v) {
//获取editText中的文本
EditText userNameEditText=(EditText)findViewById(R.id.editText2);
EditText userPWD=(EditText)findViewById(R.id.editText1); // TODO Auto-generated method stub
//推断登录身份
if (userNameEditText.getText().toString().equals("zhanghui") && userPWD.getText().toString().equals("1")) {
Intent intent = new Intent();
intent.setClass(MainActivity.this, SecondActivity.class);
startActivity(intent);
} else {
setTitle("error");
} }
}); } @Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
} }

activity_main XML代码:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".MainActivity" > <TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/hello_world" /> <!-- editText of userName -->
<EditText
android:id="@+id/editText2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/textView1"
android:layout_centerHorizontal="true"
android:layout_marginTop="64dp"
android:ems="10"
android:inputType="textPersonName"
android:text="@string/UserName"/> <!-- editText of password -->
<EditText
android:id="@+id/editText1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/editText2"
android:layout_below="@+id/editText2"
android:layout_marginTop="54dp"
android:ems="10"
android:inputType="numberPassword"
android:text="@string/PWD"> <requestFocus />
</EditText> <Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/editText1"
android:layout_centerHorizontal="true"
android:layout_marginTop="86dp"
android:text="@string/login" /> </RelativeLayout>

second_main XML代码:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" > <TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Welcome"
android:layout_marginLeft="90dp"
android:layout_marginTop="200dp"
android:textColor="#9900CC"
android:textSize="36dp"
/>
</LinearLayout>

这样便能够实现登录中对信息的验证功能了。须要说明的是此登录Demo是在上一篇android页面跳转的Demo基础之上做的,鉴于自己文笔有限。假设有什么看不明确的,大家能够先从上一文章开始。那么这将是简单的事非常多!

版权声明:本文博主原创文章,博客,未经同意不得转载。

最新文章

  1. 推荐几款jquery图片切换插件
  2. 【BZOJ-2653】middle 可持久化线段树 + 二分
  3. Python 进程间通信
  4. python-异常处理
  5. IBM x3850 x5 服务器 安装 Windows Server 2008
  6. mybatis输出SQL
  7. puppet组织架构
  8. 关于JDBC链接数据库的代码实现
  9. post 相比get 有很多优点,为什么现在的HTTP通信中大多数请求还是使用get?
  10. 【巧妙】【3-21个人赛】Problem C 01串
  11. 初学laravel框架,解决访问路由404的问题
  12. java--方法重写与重载
  13. vue1与vue2的路由 以及vue2项目大概了解
  14. MySQL刷新事务日志级别设置
  15. Xcode中为何要为设置bundle和App分别设置两份一样的图片资源
  16. off-canvas:抽屉式页面布局的纯css实现
  17. js 异步加载和同步加载
  18. hql语句中的分页显示
  19. Educational Codeforces Round 45 (Rated for Div. 2) F - Flow Control
  20. Implement JSON Web Tokens Authentication in ASP.NET Web API and Identity 2.1 Part 3 (by TAISEER)

热门文章

  1. Unity3D之Vector3.Dot和Vector3.Cross采用
  2. iOS 获取联系人,并调用系统地址簿UI
  3. zookeeper错误KeeperErrorCode = ConnectionLoss解决
  4. svn加入新的文件夹
  5. Android 省市县 三级联动(android-wheel的使用)
  6. Spring的文件上传
  7. 一份关于jvm内存调优及原理的学习笔记
  8. HTML的标签使用
  9. 做一个自动修改本机IP和mac的bat文件
  10. SQL Server 日志收缩