位置: 首页>> Android字节流模拟注册、登录功能小程序

Android字节流模拟注册、登录功能小程序

登录页面一:main.xml布局代码如下:

<?xml
version=“1.0”
encoding=“utf-8”?>

<AbsoluteLayout
android:id=“@+id/widget0”

    android:layout_width=“fill_parent”
android:layout_height=“fill_parent”

    xmlns:android=“http://schemas.android.com/apk/res/android”>


    <TextView
android:id=“@+id/TextView”
android:layout_width=“wrap_content”

        android:layout_height=“wrap_content”
android:text=
用户名:

        android:layout_x=“51px”
android:layout_y=“107px”>

    </TextView>

    <AutoCompleteTextView
android:id=“@+id/name”

        android:layout_width=“140px”
android:layout_height=“wrap_content”

        android:text=“”
android:textSize=“18sp”
android:layout_x=“137px”

        android:layout_y=“97px”
android:singleLine=“true”>

    </AutoCompleteTextView>

    <TextView
android:id=“@+id/widget35”
android:layout_width=“wrap_content”

        android:layout_height=“wrap_content”
android:text=

码:

        android:layout_x=“50px”
android:layout_y=“167px”>

    </TextView>

    <EditText
android:id=“@+id/pass”
android:layout_width=“140px”

        android:layout_height=“wrap_content”
android:text=“”
android:textSize=“18sp”

        android:layout_x=“139px”
android:layout_y=“167px”
android:singleLine=“true”

        android:password=“true”>

    </EditText>

    <Button
android:id=“@+id/login”
android:layout_width=“wrap_content”

        android:layout_height=“wrap_content”
android:text=
登录

        android:layout_x=“65px”
android:layout_y=“222px”>

    </Button>

    <Button
android:id=“@+id/zhuce”
android:layout_width=“wrap_content”

        android:layout_height=“wrap_content”
android:text=
注册

        android:layout_x=“173px”
android:layout_y=“222px”>

    </Button>

</AbsoluteLayout>

登录页面:LoginActivity.java代码如下:

import java.io.*;

import java.util.ArrayList;

import com.yinlan.login.success.Success;

import android.app.Activity;

import android.app.AlertDialog;

import android.app.Dialog;

import android.content.DialogInterface;

import android.content.Intent;

import android.os.Bundle;

import android.view.View;

import android.view.View.OnClickListener;

import android.widget.ArrayAdapter;

import android.widget.AutoCompleteTextView;

import android.widget.Button;

import android.widget.EditText;

import android.widget.TextView;

import android.widget.Toast;

public class LoginActivity extends Activity {

private ArrayAdapter ap;

private AutoCompleteTextView name;

private EditText pass;

private String userna=””;

private String passwd=””;

/** Called when the activity is first created. */

@Override

public void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.main);

Button login = (Button) this.findViewById(R.id.login);

Button zhuce = (Button) this.findViewById(R.id.zhuce);

//将保存的内容读出来

final ArrayList<String> array = new ArrayList<String>();

File file = new File(“/sdcard/user.txt”);

try {

FileReader fr = new FileReader(file);

BufferedReader br = new BufferedReader(fr);

String line = br.readLine();

while (line != null) {

array.add(line);

line = br.readLine();

}

br.close();

} catch (FileNotFoundException e1) {

// TODO Auto-generated catch block

e1.printStackTrace();

} catch (IOException e) {

// TODO Auto-generated catch block

e.printStackTrace();

}

String[] namesAll = new String[array.size()];

array.toArray(namesAll);

// 实现下拉列表

ap = new ArrayAdapter<String>(LoginActivity.this,

android.R.layout.simple_dropdown_item_1line, namesAll);

name = (AutoCompleteTextView) this.findViewById(R.id.name);

name.setAdapter(ap);

pass = (EditText) this.findViewById(R.id.pass);

zhuce.setOnClickListener(new OnClickListener() {

public void onClick(View v) {

// TODO Auto-generated method stub

Intent tent = new Intent(LoginActivity.this, Regin.class);

startActivity(tent);

}

});

login.setOnClickListener(new OnClickListener() {

public void onClick(View v) {

// TODO Auto-generated method stub

if (!(name.getText() + “”).equals(“”)) {

if ((pass.getText() + “”).equals(“”)) {

Toast.makeText(LoginActivity.this, “密码不可为空”,

Toast.LENGTH_SHORT).show();

}

userna = name.getText() + “”;

passwd = pass.getText() + “”;

//System.out.println(userna + ” ” + passwd);

ArrayList<String> users = readFile();

if (checkLogon(userna, passwd, users)) {

AlertDialog.Builder ab = new AlertDialog.Builder(

LoginActivity.this);

ab.setMessage(“是否保存用户名”);

ab.setTitle(“提示”);

ab.setPositiveButton(“是”,

new DialogInterface.OnClickListener() {

public void onClick(DialogInterface dialog,

int which) {

// TODO Auto-generated method stub

boolean flag=true;

while(!array.isEmpty()){

if(array.get(0).equals(userna)){

flag=false;

break;

}else{

array.remove(0);

}

}

if(array.isEmpty()&&flag==true){

//对用户名进行保存

File file = new File(“/sdcard/user.txt”);

try {

RandomAccessFile raf = new RandomAccessFile(

file, “rw”);

raf.skipBytes((int) file.length());

raf.writeBytes(userna + “\n”);

raf.close();

} catch (FileNotFoundException e) {

// TODO Auto-generated catch block

e.printStackTrace();

} catch (IOException e) {

// TODO Auto-generated catch block

e.printStackTrace();

}

}

//页面跳转

Intent tent = new Intent();

tent.setClass(LoginActivity.this, Show.class);

tent.putExtra(“loginUser”, userna);

startActivity(tent);

}

}).setNegativeButton(“否”,

new DialogInterface.OnClickListener() {

public void onClick(DialogInterface dialog,

int which) {

// TODO Auto-generated method stub

dialog.dismiss();

Intent tent = new Intent();

tent.setClass(LoginActivity.this, Show.class);

tent.putExtra(“loginUser”, userna);

startActivity(tent);

}

});

ab.show();

}

} else {

Toast.makeText(LoginActivity.this, “用户名不可为空”,

Toast.LENGTH_SHORT).show();

}

}

});

}

private ArrayList<String> readFile() {

ArrayList<String> array = new ArrayList<String>();

File file = new File(“/sdcard/names.txt”);

try {

FileReader fr = new FileReader(file);

BufferedReader br = new BufferedReader(fr);

String line = br.readLine();

while (line != null) {

array.add(line);

line = br.readLine();

}

br.close();

} catch (FileNotFoundException e1) {

// TODO Auto-generated catch block

e1.printStackTrace();

} catch (IOException e) {

// TODO Auto-generated catch block

e.printStackTrace();

}

// System.out.println(array);

return array;

}

// checkLogin

private boolean checkLogon(String usr, String pwd, ArrayList<String> list) {

boolean flag = false;

System.out.println(usr + ” ” + pwd + ” ” + list.size());

while (!list.isEmpty()) {

String all = list.get(0);

String user = all.substring(0, all.indexOf(“|”));

String pass = all.substring(all.indexOf(“|”) + 1, all.length());

//System.out.println(user + ” ” + pass);

if (user.equals(usr)) {

if (pass.equals(pwd)) {

flag = true;

break;

} else {

Toast.makeText(LoginActivity.this, “密码不正确”,

Toast.LENGTH_SHORT).show();

break;

}

} else {

list.remove(0);

}

}

if (list.isEmpty() && flag == false) {

Toast.makeText(LoginActivity.this, “您还没有注册或用户名有误”,

Toast.LENGTH_SHORT).show();

}

//System.out.println(flag);

return flag;

}

}

如图:


登录页面功能解析:1.在输入用户名时,会显示下拉列表。这个列表显示的是你保存在user.txt里的用户名。如图:

2.输入完用户名和密码,单击登录,弹出是否保存用户名对话框。如图:

3.不管你保不保存用户名都会登录到显示用户页面;区别是如果你不保存你的用户名,当你下次登录的时候列表中不会显示。



注册页面二:zhuce.xml布局文件代码如下:

<?xml
version=“1.0”
encoding=“utf-8”?>

<AbsoluteLayout
android:id=“@+id/widget0”

    android:layout_width=“fill_parent”
android:layout_height=“fill_parent”

    xmlns:android=“http://schemas.android.com/apk/res/android”>

    <TextView
android:id=“@+id/widget31”
android:layout_width=“wrap_content”

        android:layout_height=“wrap_content”
android:text=
用户名:

        android:layout_x=“40px”
android:layout_y=“40px”>

    </TextView>

    <AutoCompleteTextView
android:id=“@+id/name2”

        android:layout_width=“140px”
android:layout_height=“wrap_content”

        android:text=“”
android:textSize=“18sp”
android:layout_x=“120px”

        android:layout_y=“40px”
android:singleLine=“true”>

    </AutoCompleteTextView>

    <TextView
android:id=“@+id/widget33”
android:layout_width=“wrap_content”

        android:layout_height=“wrap_content”
android:text=

码:

        android:layout_x=“40px”
android:layout_y=“100px”>

    </TextView>

    <EditText
android:id=“@+id/pass2”
android:layout_width=“140px”

        android:layout_height=“wrap_content”
android:text=“”
android:textSize=“18sp”

        android:layout_x=“120px”
android:layout_y=“100px”
android:singleLine=“true”

        android:password=“true”>

    </EditText>

    <TextView
android:id=“@+id/widget33”
android:layout_width=“wrap_content”

        android:layout_height=“wrap_content”
android:text=
确认密码:

        android:layout_x=“40px”
android:layout_y=“160px”>

    </TextView>

    <EditText
android:id=“@+id/pass3”
android:layout_width=“140px”

        android:layout_height=“wrap_content”
android:text=“”
android:textSize=“18sp”

        android:layout_x=“119px”
android:layout_y=“160px”
android:singleLine=“true”

        android:password=“true”>

    </EditText>

    <Button
android:id=“@+id/suc”
android:layout_width=“60px”

        android:layout_height=“wrap_content”
android:text=
注册

        android:layout_x=“80px”
android:layout_y=“220px”>

    </Button>

    <Button
android:id=“@+id/exit”
android:layout_width=“60px”

        android:layout_height=“wrap_content”
android:text=
返回

        android:layout_x=“220px”
android:layout_y=“220px”>

    </Button>

</AbsoluteLayout>

注册页面:Regin.java代码如下:

import java.io.BufferedReader;

import java.io.File;

import java.io.FileNotFoundException;

import java.io.FileOutputStream;

import java.io.FileReader;

import java.io.IOException;

import java.io.PrintStream;

import java.io.RandomAccessFile;

import java.util.ArrayList;

import android.app.Activity;

import android.app.AlertDialog;

import android.content.DialogInterface;

import android.content.Intent;

import android.os.Bundle;

import android.view.View;

import android.view.View.OnClickListener;

import android.widget.ArrayAdapter;

import android.widget.AutoCompleteTextView;

import android.widget.Button;

import android.widget.EditText;

import android.widget.Toast;

public class Regin extends Activity {

private AutoCompleteTextView name2;

private ArrayAdapter aa;

public void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.zhuce);

final EditText pass2 = (EditText) this.findViewById(R.id.pass2);

final EditText pass3 = (EditText) this.findViewById(R.id.pass3);

name2 = (AutoCompleteTextView) this.findViewById(R.id.name2);

// name2.setAdapter(aa);

final Button suc = (Button) this.findViewById(R.id.suc);

final Button exit = (Button) this.findViewById(R.id.exit);

// 实现注册按钮

suc.setOnClickListener(new OnClickListener() {

public void onClick(View v) {

// TODO Auto-generated method stub

if (!(name2.getText() + “”).equals(“”)

&& !(pass2.getText() + “”).equals(“”)&& !(pass3.getText() + “).equals(“”)) {

if((pass2.getText() + “”).equals((pass3.getText() + “”))){

ArrayList<String> list = readFile();

boolean flag=false;

while (!list.isEmpty()) {

String all = list.get(0);

String user = all.substring(0, all.indexOf(“|”));

if ((name2.getText() + “”).equals(user)) {

Toast.makeText(Regin.this, “该用户名已注册!”,

Toast.LENGTH_SHORT).show();

name2.setText(“”);

pass2.setText(“”);

pass3.setText(“”);

flag=true;

break;

} else {

list.remove(0);

}

}

if(list.isEmpty()&&flag==false){

String na = name2.getText() + “|” + pass2.getText()

+ “\n”;

File file = new File(“/sdcard/names.txt”);

try {

RandomAccessFile raf = new RandomAccessFile(

file, “rw”);

raf = new RandomAccessFile(file, “rw”);

raf.skipBytes((int) file.length());

raf.writeBytes(na);

raf.close();

} catch (FileNotFoundException e) {

// TODO Auto-generated catch block

e.printStackTrace();

} catch (IOException e) {

// TODO Auto-generated catch block

e.printStackTrace();

}

//init();

Toast.makeText(Regin.this, “注册成功!请返回登录或继续注册”,

Toast.LENGTH_SHORT).show();

name2.setText(“”);

pass2.setText(“”);

pass3.setText(“”);

}

}else{

Toast.makeText(Regin.this, “密码和确认密码不一致!”,

Toast.LENGTH_SHORT).show();

}

} else {

Toast.makeText(Regin.this, “用户名、密码确认密码不能为空!”,

Toast.LENGTH_SHORT).show();

}

}

});

// 实现返回按钮

exit.setOnClickListener(new OnClickListener() {

public void onClick(View v) {

// TODO Auto-generated method stub

Intent in = new Intent(Regin.this, LoginActivity.class);

startActivity(in);

}

});

}

private ArrayList<String> readFile() {

ArrayList<String> array = new ArrayList<String>();

File file = new File(“/sdcard/names.txt”);

try {

FileReader fr = new FileReader(file);

BufferedReader br = new BufferedReader(fr);

String line = br.readLine();

while (line != null) {

array.add(line);

line = br.readLine();

}

br.close();

} catch (FileNotFoundException e1) {

// TODO Auto-generated catch block

e1.printStackTrace();

} catch (IOException e) {

// TODO Auto-generated catch block

e.printStackTrace();

}

System.out.println(array);

return array;

}

}

注册页面如图:



注册页面功能解析:1.注册的用户名不能相同,密码和确认密码必须一致。否则会弹出【密码和确认密码不一致】对话框。2.注册成功后,会把用户名和密码保存到names.txt里。

用户显示页面三:show.xml布局代码如下:

<?xml
version=“1.0”
encoding=“utf-8”?>

<AbsoluteLayout
android:id=“@+id/widget0”

    android:layout_width=“fill_parent”
android:layout_height=“fill_parent”

    xmlns:android=“http://schemas.android.com/apk/res/android”>

<ListView
android:layout_width=“fill_parent”
android:id=“@+id/vshow”
android:layout_height=“wrap_content”
android:layout_x=“0dip”
android:layout_y=“68dip”></ListView>

<Button
android:text=
退出
android:layout_height=“36px”
android:id=“@+id/show”
android:layout_width=“60px”
android:layout_x=“250dip”
android:layout_y=“10dip”></Button>

<TextView
android:layout_height=“50px”
android:text=
当前用户
android:id=“@+id/view”
android:layout_width=“wrap_content”
android:layout_x=“10dip”
android:layout_y=“10dip”></TextView>

</AbsoluteLayout>

用户显示Show.java代码如下:

package com.yinlan.login;

import java.io.BufferedReader;

import java.io.File;

import java.io.FileNotFoundException;

import java.io.FileReader;

import java.io.IOException;

import java.io.RandomAccessFile;

import java.util.ArrayList;

import android.app.Activity;

import android.app.AlertDialog;

import android.content.DialogInterface;

import android.content.Intent;

import android.os.Bundle;

import android.view.View;

import android.view.View.OnClickListener;

import android.widget.ArrayAdapter;

import android.widget.Button;

import android.widget.ListView;

import android.widget.TextView;

import android.widget.Toast;

public class Show extends Activity {

public void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.show);

final TextView view=(TextView) this.findViewById(R.id.view);

final ListView vshow=(ListView) this.findViewById(R.id.vshow);

final Button bshow=(Button) this.findViewById(R.id.show);

Intent intent=this.getIntent();

view.setText(view.getText()+intent.getStringExtra(“loginUser”));

ArrayList<String> users=readFile();

for(int i=0;i<users.size();i++){

users.set(i, “用户名:”+users.get(i).replace(“|”, “\n密码:”));

}

String[] namesAll=new String[users.size()];

users.toArray(namesAll);

ArrayAdapter<String> aad = new ArrayAdapter<String>(this,

android.R.layout.simple_expandable_list_item_1,namesAll );

vshow.setAdapter(aad);

bshow.setOnClickListener(new OnClickListener() {

public void onClick(View v) {

// TODO Auto-generated method stub

AlertDialog.Builder ab = new AlertDialog.Builder(

Show.this);

ab.setMessage(“确定退出吗?”);

ab.setTitle(“提示”);

ab.setPositiveButton(“确定”,

new DialogInterface.OnClickListener() {

public void onClick(DialogInterface dialog,

int which) {

// TODO Auto-generated method stub

Intent in=new Intent(Show.this,LoginActivity.class);

startActivity(in);

}

}).setNegativeButton(“取消”,

new DialogInterface.OnClickListener() {

public void onClick(DialogInterface dialog,

int which) {

// TODO Auto-generated method stub

dialog.dismiss();

}

});

ab.show();

}

});

}

private ArrayList<String> readFile(){

ArrayList<String> array=new ArrayList<String>();

File file=new File(“/sdcard/names.txt”);

try {

FileReader fr=new FileReader(file);

BufferedReader br=new BufferedReader(fr);

String line=br.readLine();

while(line!=null){

array.add(line);

line=br.readLine();

}

br.close();

} catch (FileNotFoundException e1) {

// TODO Auto-generated catch block

e1.printStackTrace();

} catch (IOException e) {

//TODO Auto-generated catch block

e.printStackTrace();

}

System.out.println(array);

return array;

}

用户显示图:


页面显示功能解析:1.当你登录成功后,转到当前用户界面,当前用户显示的是你的用户名;

2.退出按钮,退出到登陆页面;

3.显示的用户名和密码是从names.txt文件中读取出来的。

No Responses To This Post So Far(Rss)

Post a comment