轻松实现Android登录Demo

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

登录之前:

登录成功:

登录信息有误:

在主页面上添加两个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基础之上做的,鉴于自己文笔有限,如果有什么看不明白的,大家可以先从上一篇文章下手,然后在做这个就会简单很多!

时间: 2024-08-03 14:41:44

轻松实现Android登录Demo的相关文章

Xamarin.Android再体验之简单的登录Demo

一.前言 在空闲之余,学学新东西 二.服务端的代码编写与部署 这里采取的方式是MVC+EF返回Json数据,(本来是想用Nancy来实现的,想想电脑太卡就不开多个虚拟机了,用用IIS部署也好) 主要是接受客户端的登陆请求,服务器端返回请求的结果 这里的内容比较简单不在啰嗦,直接上代码了: 1 using System.Linq; 2 using System.Web.Mvc; 3 namespace Catcher.AndroidDemo.EasyLogOn.Service.Controller

百度地图SDK for Android【Demo兴趣点搜索】

百度地图SDK为开发者提供了便捷的检索服务.今天我将为大家介绍Poi检索相关的内容. 首先,我们要构建一个最基本的地图应用,具体介绍请参考:百度地图SDK for Android[Demo地图展示] 在这个工程的基础之上我们做一定的修改. 第一步,修改布局文件,添加关键字输入框和用于执行搜索操作的按钮.代码如下: [html] view plaincopy <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/a

记住密码(Android登录界面)

在编写安卓登录界面时,我们如何记住密码,方便下一次登录呢? 首先,先创建一个安卓项目(我的版本是4.4.2的),名字为"记住密码" 然后在res文件夹下找到layout文件夹,找到activity_main.xml或fragment_main.xml,在里面输入或拖拽按钮 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http

android Junit demo

package com.sondon.dhjk.test; import com.sondon.dhjk.utils.LogUtil; import junit.framework.TestCase; public class Test extends TestCase { private static final String TAG = "Test"; /** * 构造函数 * @param name */ public Test(String name) { super(name

android登录界面

Gang android登录界面,布布扣,bubuko.com

SpringMVC 登录DEMO

引言:最近工作上接触到了Spring的mvc,之前没有接触过,感觉很陌生.今天有点闲就在网上找了点资料学习了,也算是成功了吧!之前一直都在用Struts2的mvc,相比之下,我更喜欢用Spring的mvc.总之各有各的优点吧!看看各位怎么用了. 下面是我基于Spring MVC写的一个简单的登录demo. 工程一览图: 下面简单的看下具体的代码: web.xml: <?xml version="1.0" encoding="UTF-8"?> <we

SQL Server之 (四) ADO增删查改 登录demo 带参数的sql语句 插入自动返回行号

SQL Server之 (四) ADO增删查改  登录demo  带参数的sql语句  插入自动返回行号 自己学习笔记,转载请注明出处,谢谢!---酸菜 1.什么是ADO.NET ADO.NET是一组类库,这组类库可以让我们通过程序的方式访问数据库,并以各种方式操作存储在其中的数据; ADO.NET是基于.NET FrameWork,与.NET FrameWork类库的其余部分是高度集成的 2.连接数据库的步骤 ①创建连接字符串 Data Source=XXX-PC; Initial Catal

谁能提供一份用springmvc+hibernate+jstl写的完整用户注册,登录demo呢

想看看springmvc到底有多么方便 经验证,注册中文有乱码,并且pom.xml中缺少 <dependency> <groupId>javassist</groupId> <artifactId>javassist</artifactId> <version>3.12.1.GA</version> </dependency> 大家注意下! 访问地址: 首页 http://xxxx:端口/项目名称或者没有/us

Android Service demo例子使用详解

Android Service demo例子使用详解\ 概述Service 是 Android 的四大组件之一,它主要的作用是后台执行操作,Activity 属于带有 UI 界面跟用户进行交互,而 Service 则没有 UI 界面,所有的操作都是基于后台运行完成.并且 Service 跟 Activity 一样也是可以由其它的应用程序调用启动的,而且就算用户切换了应用程序,Service 依旧保持运行.一个组件如果与 Service 进行了绑定( bind ),就可以跟 Service 进行数