布局文件
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:background="@drawable/back">
<ImageView
android:id="@+id/image_touxiang"
android:layout_width="170dp"
android:layout_height="170dp"
android:layout_marginLeft="20dp"
android:src="@drawable/denglutubiao"/>
<LinearLayout
android:id="@+id/line1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@+id/image_touxiang"
android:layout_marginTop="50dp"
android:background="@drawable/round_button"
android:orientation="vertical">
<EditText
android:id="@+id/edit_name"
android:layout_width="match_parent"
android:layout_height="50dp"
android:layout_weight="1"
android:background="@drawable/round_editext"
android:drawableLeft="@drawable/people"
android:layout_marginTop="30dp"
android:layout_marginLeft="20dp"
android:layout_marginRight="20dp"
android:hint="请输入用户名"></EditText>
<EditText
android:id="@+id/edit_password"
android:layout_width="match_parent"
android:layout_height="50dp"
android:layout_marginTop="20dp"
android:layout_weight="1"
android:background="@drawable/round_editext"
android:drawableLeft="@drawable/password"
android:hint="请输入密码"
android:layout_marginLeft="20dp"
android:layout_marginRight="20dp"
android:password="true"></EditText>
<Button
android:id="@+id/btn_login"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_weight="1"
android:background="@drawable/round_button"
android:text="login"
android:layout_marginTop="40dp"
android:onClick="btn_login">
</Button>
</LinearLayout>
<LinearLayout
android:id="@+id/line3"
android:layout_width="match_parent"
android:layout_height="40dp"
android:layout_gravity="bottom"
android:gravity="bottom"
android:layout_marginBottom="0dp"
android:layout_marginTop="120dp"
android:orientation="horizontal"
android:layout_alignParentBottom="true">
<TextView
android:id="@+id/text_lostp"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_gravity="left"
android:gravity="center"
android:layout_weight="2"
android:text="忘记密码" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:text="|"/>
<TextView
android:id="@+id/text_regist"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_gravity="right"
android:layout_marginRight="0dp"
android:layout_weight="2"
android:gravity="center"
android:onClick="textviewClick"
android:text="用户注册" />
</LinearLayout>
</RelativeLayout>
注意在对文件操作时,要文件输入输出流一定要写在try{}catch{}中,异常处理,不然会出错(闪退),
主要代码:读取,存储,将密码转换成哈希值存储
登录界面:一行行读取用户名密码,匹配
public class MainActivity extends AppCompatActivity {
private EditText reditext_name;
private EditText getReditext_password;
FileInputStream fileInputStream = null;
BufferedReader bufferedReader = null;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ActionBar actionBar = getSupportActionBar();
if (actionBar != null) {
actionBar.hide();
}
}
public void textviewClick(View v){
Intent intent = new Intent(MainActivity.this,register.class);
startActivity(intent);
}
@Override
protected void onDestroy() {
super.onDestroy();
try {
bufferedReader.close();
} catch (IOException e) {
e.printStackTrace();
}
}
public void btn_login(View v) {
reditext_name=findViewById(R.id.edit_name);
getReditext_password=findViewById(R.id.edit_password);
String name=reditext_name.getText().toString();
String password=hashCode(getReditext_password.getText().toString());
String content=null;
if(name.equals("")||password.equals("")){
Toast.makeText(MainActivity.this,"用户名或密码为空",Toast.LENGTH_LONG).show();
}else {
**try{
fileInputStream=openFileInput("data");
bufferedReader=new BufferedReader(new InputStreamReader(fileInputStream));
while (bufferedReader.readLine()!=null){//一行行读取,直到结束
String[] contents = bufferedReader.readLine().split("###");
if (contents[0].equals(name)&&contents[1].equals(password)) {
Toast.makeText(MainActivity.this, "登录成功", Toast.LENGTH_LONG).show();
Intent intent = new Intent(MainActivity.this,head.class);
startActivity(intent);
return;
}**
}
Toast.makeText(MainActivity.this,"用户名或密码不正确,请重新输入",Toast.LENGTH_LONG).show();
} catch (Exception e) {
e.printStackTrace();
}
}
}
//
public String hashCode(String str) {
// 最终计算得出的哈希值,转化为int以后的哈希值
int hashcode = 0;
// 临时哈希值变量
int hash = 0;
if (hash == 0) {
// 当前char的索引
int off = 0;
// 字符串str的字符数组表示
char val[] = str.toCharArray();
// 字符串str的长度
int len = str.length();
for (int i = 0; i < len; i++) {
hash = 31 * hash + val[off++];
}
hashcode = hash;
}
return String.valueOf(hashcode);
}
}
注册界面:读取判断是否已经注册过,否则注册
public class register extends AppCompatActivity {
private EditText reditext_name;
private EditText getReditext_password;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.layout_register);
ActionBar actionBar = getSupportActionBar();
if (actionBar != null) {
actionBar.hide();
}
}
public void btn_register(View v) {
reditext_name=findViewById(R.id.edit_name);
getReditext_password=findViewById(R.id.edit_password);
String name=reditext_name.getText().toString();
String password=hashCode(getReditext_password.getText().toString());
FileInputStream fileInputStream = null;
BufferedReader bufferedReader = null;
String content=null;
//String[] content = load().split("###");
if(name.equals("")||getReditext_password.getText().toString().equals("")){
Toast.makeText(register.this,"用户名或密码为空",Toast.LENGTH_LONG).show();
return;
}
try{//一定要写在try里面否则会有错误
fileInputStream=openFileInput("data");
bufferedReader=new BufferedReader(new InputStreamReader(fileInputStream));
while ((content=bufferedReader.readLine())!=null) {
String[] contentS = content.split("###");
if (contentS[0].equals(name)) {
Toast.makeText(register.this, "用户名已经存在", Toast.LENGTH_LONG).show();
reditext_name.setText("");
return;
}
}
bufferedReader.close();
Log.d("检测读取完毕", "btn_register: ");
Toast.makeText(register.this,"注册成功",Toast.LENGTH_LONG).show();
save(name,password);
Intent intent = new Intent(register.this,MainActivity.class);
startActivity(intent);
} catch (Exception e) {
e.printStackTrace();
}
}
public void save(String name,String password){
FileOutputStream fileOutputStream = null;
BufferedWriter bufferedWriter =null;
try{
fileOutputStream = openFileOutput("data", Context.MODE_APPEND);
bufferedWriter =new BufferedWriter(new OutputStreamWriter(fileOutputStream));
bufferedWriter.write(name+"###"+password);
bufferedWriter.write("\n");
//因为多个用户,所以我每次写入一个换行,实现一行一个用户
Log.d("存储成功", "save: ");
} catch (IOException e) {
e.printStackTrace();
} finally {
try{
if (bufferedWriter != null) {
bufferedWriter.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
public String hashCode(String str) {
// 最终计算得出的哈希值,转化为int以后的哈希值
int hashcode = 0;
// 临时哈希值变量
int hash = 0;
if (hash == 0) {
// 当前char的索引
int off = 0;
// 字符串str的字符数组表示
char val[] = str.toCharArray();
// 字符串str的长度
int len = str.length();
for (int i = 0; i < len; i++) {
hash = 31 * hash + val[off++];
}
hashcode = hash;
}
return String.valueOf(hashcode);
}
}
注册结果可以从注册文件中获取
进入Android studio中,点击view,点tools window,然后点击Device File Explorer,在出来的界面找自己的项目文件和自己创建的文件,就可以查看了,双击可以进入了
打开如下
因为之前存储用户名和密码的时候用了###作为分隔符,以便后面分开,所以需要用户名绝对不能有###,密码因为转换成哈希值了,所以不考虑。可以用下面几句代码规范用户输入
String pattern = ".*###.*";
boolean isMatch = Pattern.matches(pattern, name);
if(editText_password.getText().toString().length()<=7){
Toast.makeText(RegisterActivity.this,"密码不符合要求,必须大于7位",Toast.LENGTH_LONG).show();
editText_password.setText("");
}
else if(isMatch){
Toast.makeText(RegisterActivity.this,"用户名不符合要求,请注意不能包含###",Toast.LENGTH_LONG).show();
editText_name.setText("");
}