轻松实现Android登录Demo

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

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 22
    评论
评论 22
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值