Android实现简单的登陆注册跳转

  • Post author:
  • Post category:其他




项目整体思路
登陆成功
登陆选项 登陆失败
首页
注册成功 返回首页
注册选项 注册页面
注册失败

具体实现过程如下:


1,


更改MainActiity.xml 代码如下:

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content" >

    <ImageView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:background="@drawable/bj1" />

    <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:orientation="vertical" >

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:paddingLeft="20dp"
            android:text="  电信1601冯欢160101010008" />

        <TextView
            android:id="@+id/loginxx"
            android:layout_width="80dp"
            android:layout_height="30dp"
            android:layout_gravity="center"
            android:layout_marginBottom="30dp"
            android:layout_marginTop="50dp" />

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:paddingLeft="20dp"
            android:text="掌上微博登陆系统"
            android:textSize="30sp" />

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:orientation="horizontal" >

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:paddingLeft="20dp"
                android:text="账号"
                android:textSize="30sp" />

            <EditText
                android:id="@+id/name"
                android:layout_width="match_parent"
                android:layout_height="50dp"
                android:background="#ffffffff"
                android:paddingLeft="20dp"
                android:textColor="#ff000000"
                android:textSize="14sp" />
        </LinearLayout>

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:orientation="horizontal" >

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:paddingLeft="20dp"
                android:text="密码"
                android:textSize="30sp" />

            <EditText
                android:id="@+id/password"
                android:layout_width="match_parent"
                android:layout_height="50dp"
                android:background="#ffffffff"
                android:paddingLeft="20dp"
                android:password="true"
                android:textColor="#ff000000"
                android:textSize="14sp" />
        </LinearLayout>

        <LinearLayout
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:orientation="horizontal" >

            <Button
                android:id="@+id/login_button"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginLeft="80dp"
                android:layout_marginRight="20dp"
                android:gravity="center"
                android:text="登 陆"
                android:textColor="#ffffffff"
                android:textSize="16sp" />

            <Button
                android:id="@+id/login_button1"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginLeft="20dp"
                android:layout_marginRight="20dp"
                android:gravity="center"
                android:text="注 册"
                android:textColor="#ffffffff"
                android:textSize="16sp" />
        </LinearLayout>

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:gravity="bottom"
            android:orientation="horizontal" >
        </LinearLayout>
    </LinearLayout>

</FrameLayout>


2,


更改MainActiity.java如果点击登陆按钮则执行登陆逻辑 如果点击注册则跳转到注册页面  我的java包路径为com.example.app1 后面不做累述 代码如下:

package com.example.app1;

import android.support.v7.app.ActionBarActivity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;

public class AppMainActivity extends ActionBarActivity {
	public Button bt;
	Button bt1;
	public EditText name;
	public EditText password;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_app_main);
		bt = (Button) findViewById(R.id.login_button);
		bt1 = (Button) findViewById(R.id.login_button1);
		bt.setOnClickListener(new Button.OnClickListener() {
			public void onClick(View v) {
				name = (EditText) findViewById(R.id.name);
				password = (EditText) findViewById(R.id.password);
				String myName = name.getText().toString();
				String mypassword = password.getText().toString();// 获取文本框输入信息
				Intent intent = new Intent();
				intent.setClass(AppMainActivity.this, SecondActivity.class);// 指定接下来要启动的class
				Bundle bundle = new Bundle();
				bundle.putString("name", myName);
				bundle.putString("password", mypassword);
				intent.putExtras(bundle);// 封装数据准备传递
				startActivity(intent);// 调用一个新的页面
			}
		});
		bt1.setOnClickListener(new Button.OnClickListener() {
			public void onClick(View v) {

				Intent intent = new Intent();
				intent.setClass(AppMainActivity.this, Shi.class);// 指定接下来要启动的class
				Bundle bundle = new Bundle();

				startActivity(intent);// 调用一个新的页面
			}
		});
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.app_main, menu);
		return true;
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		// Handle action bar item clicks here. The action bar will
		// automatically handle clicks on the Home/Up button, so long
		// as you specify a parent activity in AndroidManifest.xml.
		int id = item.getItemId();
		if (id == R.id.action_settings) {
			return true;
		}
		return super.onOptionsItemSelected(item);
	}
}


3,


新建登陆成功页面second.xml代码如下:

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout 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" >

    <ImageView
        android:id="@+id/image"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_weight="0.9"
        android:background="@drawable/bj1" />

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical" >

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_gravity="center"
            android:orientation="vertical" >

            <TextView
                android:id="@+id/tv1"
                android:layout_width="fill_parent"
                android:layout_height="wrap_content" />

            <TextView
                android:id="@+id/loginxx"
                android:layout_width="80dp"
                android:layout_height="80dp"
                android:layout_gravity="center"
                android:layout_marginBottom="30dp"
                android:layout_marginTop="50dp" />
        </LinearLayout>

        <LinearLayout
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:orientation="horizontal" >
        </LinearLayout>
    </LinearLayout>

</FrameLayout>


4,


新建登陆失败页面no.xml代码如下:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/LinearLayout1" 
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    >
    <TextView
        android:id="@+id/no"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="账号或密码错误 !  请点击返回键返回上一页面重新验证 !  (账号名为你的姓名拼音小写缩写 密码为你的8位生日数字)  如仍无法登陆 请联系管理员hfeng"
        />
    
            <ImageView
              
		android:layout_width="wrap_content"  
        android:layout_height="wrap_content" 
        android:layout_below="@id/no" 
        
        android:src="@drawable/qq" /> 
    
  </RelativeLayout>


5,


新建登陆监听SecondActivity.java判断是否能成功登陆,如果密码符合条件就跳转到成功页面,密码不符合条件就跳到登陆失败页面具体代码如下:

package com.example.app1;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

public class SecondActivity extends Activity {
	Button bt;
	Button bt0;
	ImageView iv;
	int Alpha = 255;
	private TextView tv;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);// 保存当前页面状态信息

		Bundle bundle = this.getIntent().getExtras();// 获取前一个页面传递来的数据
		String myName = bundle.getString("name");
		String mypassword = bundle.getString("password");

		if ((myName.equals("yys") & mypassword.equals("1314"))
				)) //密码规则自己在这里面设置  
{
			 
			setContentView(R.layout.second);// 显示下一跳页面
			tv = (TextView) findViewById(R.id.tv1);
			tv.setText("          欢迎" + myName + "登陆");
			
		} else {
			setContentView(R.layout.no);// 显示下一跳页面
		}
	}

}



6,


新建注册页面shi.xml代码如下:

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content" >

    <ImageView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:background="@drawable/bj1" />

    <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:orientation="vertical" >

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:paddingLeft="20dp"
            android:text="  电信1601冯欢160101010008" />

        <TextView
            android:id="@+id/loginxx"
            android:layout_width="80dp"
            android:layout_height="30dp"
            android:layout_gravity="center"
            android:layout_marginBottom="30dp"
            android:layout_marginTop="50dp" />

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:paddingLeft="20dp"
            android:text="掌上微博系统注册"
            android:textSize="30sp" />

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:orientation="horizontal" >

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:paddingLeft="20dp"
                android:text="输入账号"
                android:textSize="30sp" />

            <EditText
                android:id="@+id/name"
                android:layout_width="match_parent"
                android:layout_height="50dp"
                android:background="#ffffffff"
                android:paddingLeft="20dp"
                android:textColor="#ff000000"
                android:textSize="14sp" />
        </LinearLayout>

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:orientation="horizontal" >

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:paddingLeft="20dp"
                android:text="输入密码"
                android:textSize="30sp" />

            <EditText
                android:id="@+id/password"
                android:layout_width="match_parent"
                android:layout_height="50dp"
                android:background="#ffffffff"
                android:paddingLeft="20dp"
                android:password="true"
                android:textColor="#ff000000"
                android:textSize="14sp" />
        </LinearLayout>

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:orientation="horizontal" >

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:paddingLeft="20dp"
                android:text="确认密码"
                android:textSize="30sp" />

            <EditText
                android:id="@+id/password1"
                android:layout_width="match_parent"
                android:layout_height="50dp"
                android:background="#ffffffff"
                android:paddingLeft="20dp"
                android:password="true"
                android:textColor="#ff000000"
                android:textSize="14sp" />
        </LinearLayout>

        <LinearLayout
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:orientation="horizontal" >

            <Button
                android:id="@+id/login_button11"
                android:layout_width="fill_parent"
                android:layout_height="fill_parent"
                android:layout_marginLeft="80dp"
                android:layout_marginRight="20dp"
                android:gravity="center"
                android:text="注册"
                android:textColor="#ffffffff"
                android:textSize="16sp" />
        </LinearLayout>

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:gravity="bottom"
            android:orientation="horizontal" >
        </LinearLayout>
    </LinearLayout>

</FrameLayout>


7,


新建监听Shi.java封装注册数据 具体代码如下:

package com.example.app1;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

public class Shi extends Activity{
	Button bt;
	Button bt0;
	ImageView iv;
	int Alpha=255;
	public EditText password1;
	public EditText password;
	@Override
	public void onCreate(Bundle savedInstanceState){
		super.onCreate(savedInstanceState);//保存当前页面状态信息
		setContentView(R.layout.shi);//显示下一跳页面
		bt=(Button)findViewById(R.id.login_button11);
		bt.setOnClickListener(new Button.OnClickListener(){
        	public void onClick(View v){
        		password1 = (EditText) findViewById(R.id.password1);
				password = (EditText) findViewById(R.id.password);
				String mypassword1 = password1.getText().toString();
				String mypassword = password.getText().toString();// 获取文本框输入信息
        		Intent intent = new Intent();
        		
        		intent.setClass(Shi.this, shiyi.class);//指定接下来要启动的class
        		Bundle bundle = new Bundle();
				bundle.putString("password1", mypassword1);
				bundle.putString("password", mypassword);
				intent.putExtras(bundle);// 封装数据准备传递
        		startActivity(intent);
        	}
        	}
	);
		
		}
		
	}


8,


新建注册成功页面yse.xml代码如下:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/LinearLayout1"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >

    <TextView
        android:id="@+id/no"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="注册成功 点击返回按钮返回登陆页面" />

    <Button
        android:id="@+id/login_button2"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_below="@id/no"
        android:layout_marginLeft="80dp"
        android:layout_marginRight="20dp"
        android:gravity="center"
        android:text="返回登陆"
        android:textColor="#ffffffff"
        android:textSize="16sp" />
</RelativeLayout>


9,新建shiyi.java如果密码符合条件就跳转到注册成功页面,并监听是否返回主页面登陆 密码不符合条件就提示注册失败 具体代码如下: 代码如下

package com.example.app1;


import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;

import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

public class shiyi extends Activity {
	Button bt;
	Button bt0;
	ImageView iv;
	int Alpha = 255;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);// 保存当前页面状态信息
		Bundle bundle = this.getIntent().getExtras();// 获取前一个页面传递来的数据
		String password1 = bundle.getString("password1");
		String mypassword = bundle.getString("password");
		if (password1.equals(mypassword)) {
			setContentView(R.layout.yse);// 显示下一跳页面
			bt = (Button) findViewById(R.id.login_button2);
			bt.setOnClickListener(new Button.OnClickListener() {
				public void onClick(View v) {

					Intent intent = new Intent();

					intent.setClass(shiyi.this, AppMainActivity.class);// 指定接下来要启动的class

					startActivity(intent);
				}
			});

		} else {
			setContentView(R.layout.shi);// 显示下一跳页面
			Toast.makeText(shiyi.this, "两次输入的密码不一致!",
					Toast.LENGTH_SHORT).show();
			bt = (Button) findViewById(R.id.login_button11);
			bt.setOnClickListener(new Button.OnClickListener() {
				public void onClick(View v) {

					Intent intent = new Intent();

					intent.setClass(shiyi.this, Shi.class);// 指定接下来要启动的class

					startActivity(intent);
				}
			});
		}

	}
}




需要注意的是 有几个布局中使用了图片  所以需要自己将

<


ImageView> 标签

中的图片

换为自己的图片

否则代码会报错




版权声明:本文为hf_yys原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。