package com.desiner.ui;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.os.Bundle;
import android.text.Editable;
import android.text.InputType;
import android.text.TextWatcher;
import android.view.Gravity;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ArrayAdapter;
import android.widget.AutoCompleteTextView;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.CompoundButton.OnCheckedChangeListener;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.Toast;
import com.desiner.phone_com.R;
import com.desiner.user.User;
public class LoginActivity extends Activity {
private Button btLogin;
private Button btAdd;
private CheckBox check;
private EditText password;
private SharedPreferences sp;
private AutoCompleteTextView userName;
private String userNameValue, passwordValue;
private boolean isChecked = true;
public void setChecked(boolean isChecked) {
this.isChecked = isChecked;
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_login);
btLogin = (Button) findViewById(R.id.btLogin);
btAdd = (Button) findViewById(R.id.btAdd);
check = (CheckBox) findViewById(R.id.check);
password = (EditText) findViewById(R.id.password);
userName = (AutoCompleteTextView) findViewById(R.id.userName);
sp = this.getSharedPreferences("passwordFile", Context.MODE_WORLD_READABLE);
check.setChecked(true);// 默認記住密碼
// userName.setThreshold(1);// 輸入一個字母就開始自動提示
// password.setInputType(InputType.TYPE_CLASS_TEXT
// | InputType.TYPE_TEXT_VARIATION_PASSWORD);
// 隐藏密码为InputType.TYPE_TEXT_VARIATION_PASSWORD,也就是0x81
// 显示密码为InputType.TYPE_TEXT_VARIATION_VISIBLE_PASSWORD,也就是0x91
btLogin.setOnClickListener(new buttonListener());
btAdd.setOnClickListener(new buttonListener());
/*
* 下拉輸入框變更事件
*/
userName.addTextChangedListener(new TextWatcher() {
@Override
public void onTextChanged(CharSequence s, int start, int before,
int count) {
// TODO Auto-generated method stub
/*
* 將帳號和密碼存入passwordfile文件
*/
String[] allUserName = new String[sp.getAll().size()];// sp.getAll().size()返回的是有多少个键值对
allUserName = sp.getAll().keySet().toArray(new String[0]);
// sp.getAll()返回一张hash map
// keySet()得到的是a set of the keys.
// hash map是由key-value组成的
ArrayAdapter<String> adapter = new ArrayAdapter<String>(
LoginActivity.this,
android.R.layout.simple_dropdown_item_1line,
allUserName);
userName.setAdapter(adapter);
// 設置數據適配器
}
@Override
public void beforeTextChanged(CharSequence s, int start, int count,
int after) {
// TODO Auto-generated method stub
}
/*
* 帳號變更後自動輸入相對應的密碼
*/
@Override
public void afterTextChanged(Editable s) {
// TODO Auto-generated method stub
if(isChecked){
password.setText(sp
.getString(userName.getText().toString(), "").toString());
// 自動輸入密碼
}
}
});
}
class isChecked implements OnCheckedChangeListener {
@Override
public void onCheckedChanged(CompoundButton buttonView,
boolean isChecked) {
// TODO Auto-generated method stub
setChecked(isChecked);
}
}
class buttonListener implements OnClickListener {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
userNameValue = userName.getText().toString();
passwordValue = password.getText().toString();
switch (v.getId()){
/**
* 登錄按鈕事件
*/
case R.id.btLogin:
Toast toast = Toast.makeText(getApplicationContext(), "Loading....", 0);
toast.setGravity(Gravity.CENTER, 0, 0);
//創建圖片視圖對象
ImageView imageView = new ImageView(getApplicationContext());
//設置圖片
imageView.setImageResource(R.drawable.wait);
//獲得toast的佈局
LinearLayout toastView = (LinearLayout) toast.getView();
//設置此佈局爲橫向的
toastView.setOrientation(LinearLayout.HORIZONTAL);
//將ImageView加入到此佈局中的第一個位置
toastView.addView(imageView,0);
toast.show();
User user = new User();
user.setUserName(userName.getText().toString());
user.setPassword(password.getText().toString());
//判斷帳號和密碼
if(userNameValue.equals(sp.getString("userName", "").toString())
&& passwordValue.equals(sp.getString("password", ""))){
//判斷記住密碼多選框的狀態
if(isChecked){
sp.edit().putString(userNameValue, passwordValue).commit();
}
Intent intent = new Intent(LoginActivity.this,Funcall.class);
startActivity(intent);
Toast.makeText(LoginActivity.this, "登錄成功", 0).show();
} else {
Toast.makeText(LoginActivity.this,"帳號或密碼錯誤", 0).show();
}
break;
/*
* 註冊按鈕點擊事件
*/
case R.id.btAdd:
new AlertDialog.Builder(LoginActivity.this)
.setTitle("用戶註冊")
.setMessage("確認好你的註冊信息無誤了嗎")
.setPositiveButton("提交",
new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface arg0, int arg1) {
// TODO Auto-generated method stub
User user = new User();
user.setUserName(userName.getText().toString());
user.setPassword(password.getText().toString());
userNameValue = userName.getText().toString();
passwordValue = password.getText().toString();
//記住用戶名和密碼
Editor editor = sp.edit();
editor.putString("userName",userNameValue);
editor.putString("password",passwordValue);
editor.commit();
Intent intent = new Intent(LoginActivity.this,Funcall.class);
startActivity(intent);
Toast.makeText(LoginActivity.this, "註冊成功", 0).show();
}
}).setNegativeButton("返回", null).show();
break;
default:
break;
}
}
}
}
其中
public void afterTextChanged(Editable s) {
// TODO Auto-generated method stub
if(isChecked){
password.setText(sp
.getString(userName.getText().toString(), "").toString());
// 自動輸入密碼
}
沒有得到執行效果