zl程序教程

您现在的位置是:首页 >  移动开发

当前栏目

易Android登录Demo

Android 登录 Demo
2023-09-14 09:08:06 时间
    上一页介绍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基础之上做的,鉴于自己文笔有限。假设有什么看不明确的,大家能够先从上一文章开始。那么这将是简单的事非常多!

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