Android SharedPreferences应用 实现本地注册登陆 功能简单易懂(实例)

Android登录示例
本文介绍了一个简单的Android登录界面实现方式,使用SharedPreferences存储用户信息,并演示了登录验证流程。

 

SharedPreferences

在Android系统中,这些信息以XML文件的形式保存在 /data/data/PACKAGE_NAME /shared_prefs 目录下

 

先贴个效果图

 

 

这个是自己的手机截图,部分手机分辨率可能不一样

1.

activity_main.xml

 

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical"
    android:background="@drawable/login" >
<TableLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
       >

        <TableRow>

            <TextView
                android:layout_width="90px"
                android:layout_height="wrap_content"
                android:layout_marginLeft="50px"
                android:layout_marginTop="200px"
                android:gravity="center_horizontal"
                android:text="用户名:"
                android:textColor="#f00"
                android:textSize="15sp" />

            <EditText
                android:id="@+id/username"
                android:layout_width="250px"
                android:layout_height="wrap_content"
                android:layout_marginTop="200px"
                android:singleLine="true" />
        </TableRow>

        <TableRow>

            <TextView
                android:layout_width="90px"
                android:layout_height="wrap_content"
                android:layout_marginLeft="50px"
                android:gravity="center_horizontal"
                android:text="密码:"
                android:textColor="#f00"
                android:textSize="15sp" />

            <EditText
                android:id="@+id/password"
                android:layout_width="250px"
                android:layout_height="wrap_content"
                android:password="true" />
        </TableRow>
   </TableLayout>
     <TableLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
       >
               <TableRow>
            <Button
                android:id="@+id/login"
                android:layout_width="90px"
                android:layout_height="wrap_content"
                android:layout_marginLeft="130px"
                android:layout_marginTop="30px"
                   android:gravity="center"  
                android:text="登录" />
            <Button
                android:id="@+id/exit"
             android:layout_width="90px"
              android:layout_height="wrap_content"
             android:text="退出"
            android:gravity="center"  
             android:layout_marginLeft="80px"
                  android:layout_marginTop="30px"
                 />
                 </TableRow>
                    </TableLayout>
                      <Button
                android:id="@+id/reg"
              android:layout_width="90px"
              android:layout_height="wrap_content"
             android:text="注册"
             android:gravity="center"  
             android:layout_marginLeft="130px"
              ></Button>
</LinearLayout>

2.activity_loginok.xml
 

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical"
    android:background="@drawable/mei" >
    

</LinearLayout>
MainActivity.java

 

package com.example.logineasydemo;

import android.os.Bundle;
import android.app.Activity;
import android.app.AlertDialog;
import android.app.Dialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

public class MainActivity extends Activity {
//声明需要的组件
private  Button login,exit,reg;
private  EditText   username,password;
 private SharedPreferences share;//声明SharedPreferences 
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        intiview();//初始化视图,寻找id
        saveuser();//先保存一个数据admin 123456
        exit.setOnClickListener(new Listenerimp());//退出的监听事件
        reg.setOnClickListener(new RegListenerimp());//注册的监听事件
        //登陆的事件监听处理内部类
        login.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				//获取输入的信息
	    String name=username.getText().toString();
		String pass=password.getText().toString();
		//判断输入信息是否为空
				if(name.trim().equals("") || pass.trim().equals("")) {  
                    Toast.makeText(MainActivity.this, "用户名和密码不能为空", Toast.LENGTH_LONG).show(); 
                }  
				 //获取保存文件中的用户名和密码    
                String savedUsername = share.getString("username","");  
               String savedPassword = share.getString("password","");  
                //查看输入的密码和名字是否一致    
                if(name.trim().equals(savedUsername) && pass.trim().equals(savedPassword)) {  
                	Toast.makeText(MainActivity.this, "恭喜,亲,用户名和密码都正确!", Toast.LENGTH_LONG).show(); 
                	
                	//成功登陆,进入LoginokActivity界面
                	Intent intent=new Intent(MainActivity.this,LoginokActivity.class);
                	startActivity(intent);
                	finish();
                   
                } else {  
                	//错误的话
                    Toast.makeText(MainActivity.this, "用户名或者密码错误,请确认信息或者去注册", Toast.LENGTH_LONG).show();  
                    return;  
                }  
			}
		});
    }
private class Listenerimp implements  OnClickListener{

	@Override
	public void onClick(View v) {
		// TODO Auto-generated method stub
		finish();//结束一个Activity
	}
}
	private class  RegListenerimp implements OnClickListener{

		@Override
		public void onClick(View v) {
			// TODO Auto-generated method stub
			//定义两个字符串常量,并获取信息
			 final String nam=username.getText().toString();
			final String pas=password.getText().toString();
			//判读信息是否空
			if(nam.trim().equals("") || pas.trim().equals("")) {  
                Toast.makeText(MainActivity.this, "注册时,用户名和密码都不能为空", Toast.LENGTH_LONG).show(); 
               return;//为空就会返回
            }  
			//进入注册的Dialog 
			Dialog dialog=new AlertDialog.Builder(MainActivity.this)
            .setTitle("注册")
            .setMessage("你确定注册信息吗?")
            .setPositiveButton("确定", new  DialogInterface.OnClickListener() {
				@Override
				public void onClick(DialogInterface dialog, int which) {
					// TODO Auto-generated method stub
					//保存输入的信息 	 Editor 别忘了		edit.commit();提交
				  	share=getSharedPreferences("info",Activity.MODE_PRIVATE);
					Editor edit=share.edit();
					edit.putString("username", nam);
					edit.putString("password", pas);
					edit.commit();
					//提示成功注册
					Toast.makeText(MainActivity.this, "恭喜,注册成功", Toast.LENGTH_LONG).show();
					
				}
			}).setNegativeButton("取消", new DialogInterface.OnClickListener() {
				
				@Override
				public void onClick(DialogInterface dialog, int which) {
					// TODO Auto-generated method stub
				}
			}).create();//创建一个dialog
            dialog.show();//显示对话框,否者不成功
		}
		
	}
	

//实现写一个admin 123456的用户
    private void saveuser() {
		// TODO Auto-generated method stub
    	share=getSharedPreferences("info",Activity.MODE_PRIVATE);
		Editor edit=share.edit();
		edit.putString("username", "admin");
		edit.putString("password", "123456");
		edit.commit();
	}


	private void intiview() {
		// TODO Auto-generated method stub
		login=(Button)findViewById(R.id.login);
		exit=(Button)findViewById(R.id.exit);
		reg=(Button)findViewById(R.id.reg);
		username=(EditText)findViewById(R.id.username);
		password=(EditText)findViewById(R.id.password);
	}
}

LoginokActivity

 

 

package com.example.logineasydemo;

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

public class LoginokActivity extends Activity {
	 protected void onCreate(Bundle savedInstanceState) {
	        super.onCreate(savedInstanceState);
	        setContentView(R.layout.activity_loginok);
	 }
}


代码上传,欢迎下载http://download.youkuaiyun.com/detail/h1028962069/5615549
 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值