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

郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。