package com.ui;
import android.app.Activity;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
import com.Thread.LoginThread;
public class loginActivity extends Activity {
String usernameString=null;
String pwdString=null;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.login);
SharedPreferences sharedpreferences=loginActivity.this.getSharedPreferences("test", Activity.MODE_PRIVATE);
String username=sharedpreferences.getString("usernameString","");//第二个参数是设定的值的默认值
String pwd=sharedpreferences.getString("pwdString","");
if(!username.equals("")&&!pwd.equals("")){
//跳转第三个界面
Intent intent=new Intent(loginActivity.this,MyShopActivity.class);
startActivity(intent);
}
final EditText nameedit=(EditText) this.findViewById(R.id.name);
final EditText pwdedit=(EditText) this.findViewById(R.id.pwd);
Button loginbtn=(Button)this.findViewById(R.id.login);
loginbtn.setOnClickListener(new OnClickListener()
{
public void onClick(View v)
{
usernameString=nameedit.getText().toString();
pwdString=pwdedit.getText().toString();
Toast.makeText(loginActivity.this,"用户名:"+ usernameString+",密码:"+pwdString, Toast.LENGTH_SHORT).show();
//类型
String typeString="login";
new Thread(new LoginThread(usernameString, pwdString, typeString, handler)).start();
Intent intent=new Intent(loginActivity.this,homeActivity.class);
startActivity(intent);
}
});
Button button1=(Button)this.findViewById(R.id.register);
button1.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
// TODO Auto-generated method stub
usernameString=nameedit.getText().toString();
pwdString=pwdedit.getText().toString();
Toast.makeText(loginActivity.this,"用户名:"+ usernameString+",密码:"+pwdString, Toast.LENGTH_SHORT).show();
//类型
String typeString="register";
new Thread(new LoginThread(usernameString, pwdString, typeString, handler)).start();
Intent intent=new Intent(loginActivity.this,homeActivity.class);
startActivity(intent);
}
});
}
private Handler handler=new Handler(){
public void handleMessage(Message msg) {
String typeString=msg.getData().getString("type");
if(typeString.equals("login")){
String resuString=msg.getData().getString("result");
if(resuString.equals("success")){
Toast.makeText(loginActivity.this, "登陆成功"+resuString,Toast.LENGTH_SHORT).show();
Toast.makeText(loginActivity.this, usernameString+"\t"+pwdString, Toast.LENGTH_SHORT).show();
//一旦登陆成功就将用户名和密码写入我的保存文件中
//创建一个偏好设定 将数据保存在Mytest文件中并且设置成私有的 只有本屏幕的对象可以访问
SharedPreferences sharedPreferences=loginActivity.this.getSharedPreferences("Mytest", Activity.MODE_PRIVATE);
//获得偏好设定的编辑对象
SharedPreferences.Editor editor=sharedPreferences.edit();
//往编辑器中写入数据 用户名和密码
editor.putString("username", usernameString);
editor.putString("pwdString", pwdString);
//编辑完数据之后提交数据
editor.commit();
Toast.makeText(loginActivity.this,"数据保存成功", Toast.LENGTH_SHORT).show();
}else{
Toast.makeText(loginActivity.this, "登录失败",Toast.LENGTH_SHORT).show();
}
}else if(typeString.equals("register")){
String resuString=msg.getData().getString("result");
if(resuString.equals("success")){
Toast.makeText(loginActivity.this, "注册成功"+resuString,Toast.LENGTH_SHORT).show();
}else{
Toast.makeText(loginActivity.this, "注册失败",Toast.LENGTH_SHORT).show();
}
}
}
};
}