package com.example.test;
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
import java.io.FileInputStream;
import java.util.HashMap;
import java.util.Map;
public class MainActivity extends AppCompatActivity {
private Button login;
private Button register;
private EditText etname;
private EditText etpwd;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
login = (Button) findViewById(R.id.btn_login);
register = (Button) findViewById(R.id.btn_register);
etname = (EditText) findViewById(R.id.et_name);
etpwd = (EditText) findViewById(R.id.et_password);
login.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
String name = etname.getText().toString().trim();
String pwd = etpwd.getText().toString().trim();
String content="";
try {
FileInputStream fis=this.openFileInput("message.txt");
byte[] buffer = new byte[fis.available()];
fis.read(buffer);
content = new String(buffer);
Map<String, String> userMap = new HashMap<String, String>();
String[] infos = content.split(":");
userMap.put("name", infos[0]);
userMap.put("pwd", infos[1]);
fis.close();
return;
}catch (Exception e){
if (name.length() == 0 || pwd.length() == 0) {
Toast.makeText(MainActivity.this, "输入不能为空", Toast.LENGTH_SHORT).show(); //弹出一个自动消失的提示框
return;
} else if ((TextUtils.isEmpty(name)) && (TextUtils.isEmpty(pwd))) {
//页面跳转延迟线程
new Thread() {
public void run() {
super.run();
try {
Thread.sleep(500);
Intent i = new Intent(MainActivity.this, Activity.class);
startActivity(i);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}.start();
}
Toast.makeText(MainActivity.this, "登陆成功", Toast.LENGTH_SHORT).show();
return;
}
}
private FileInputStream openFileInput(String s) {
return null;
}
});
register.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Intent i = new Intent(MainActivity.this, RegisterActivity.class);
startActivity(i);
}
});
}
}