1. 程式人生 > >android 登入介面 記住多組賬號與密碼

android 登入介面 記住多組賬號與密碼

android
package com.android_login;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import android.app.Activity;
import android.content.SharedPreferences;
import android.graphics.drawable.ColorDrawable;
import android.os.Bundle;
import android.util.Log;
import android.view.Gravity;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup.LayoutParams;
import android.view.WindowManager;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.PopupWindow;
import android.widget.TextView;
import android.widget.Toast;

public class MainActivity extends Activity implements OnClickListener {

private ImageView select;
private PopupWindow pw;
private int width, i;
private LinearLayout parent, option;
private EditText et_user, et_pwd;
private ListView listView;
private Button login;
private CheckBox checkBox;
private SharedPreferences sPreferences;
private Map<String, String> map;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

init();
}

@Override
public void onClick(View v) {
// TODO Auto-generated method stub
switch (v.getId()) {
case R.id.select:
//為什麼x正方向偏移不了???當取負時就可以,有人可以解釋下嗎
pw.showAsDropDown(parent, 15, -4);
break;
case R.id.login:
login();
break;
}
}

private void init() {


et_user = (EditText) findViewById(R.id.et_user);
et_pwd = (EditText) findViewById(R.id.et_pwd);
parent = (LinearLayout) findViewById(R.id.llayout);

select = (ImageView) findViewById(R.id.select);
login = (Button) findViewById(R.id.login);
checkBox = (CheckBox) findViewById(R.id.checkBox1);


select.setOnClickListener(this);
login.setOnClickListener(this);

//讀取已經記住的使用者名稱與密碼
sPreferences = getSharedPreferences("session", MODE_PRIVATE);
map = (Map<String, String>) sPreferences.getAll();
List<String> list = new ArrayList<String>();

for (int i = 0; i < (map.size()/2); i++) {
String name = sPreferences.getString("name"+ i,"");
list.add(name);
}


// 用4個引數的指定,哪個listview中的textview
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
R.layout.items, R.id.item, list);
option = (LinearLayout) getLayoutInflater().inflate(R.layout.option,
null);
// 要在這個linearLayout裡面找listView......
listView = (ListView) option.findViewById(R.id.op);
listView.setAdapter(adapter);



//獲取螢幕的寬度並設定popupwindow的寬度為width,我這裡是根據佈局控制元件所佔的權重
WindowManager wManager = (WindowManager) getSystemService(this.WINDOW_SERVICE);
width = wManager.getDefaultDisplay().getWidth() * 4 / 5;


//例項化一個popupwindow物件
pw = new PopupWindow(option, width, LayoutParams.WRAP_CONTENT, true);
ColorDrawable dw = new ColorDrawable(00000);
pw.setBackgroundDrawable(dw);
pw.setOutsideTouchable(true);

listView.setOnItemClickListener(new OnItemClickListener() {

@Override
public void onItemClick(AdapterView<?> adapterView, View view,
int position, long id) {

// 獲取選中項內容及從sharePreferences中獲取對應的密碼
String username = adapterView.getItemAtPosition(position)
.toString();
String pwd = sPreferences.getString("pwd"+ position,"");

et_user.setText(username);
et_pwd.setText(pwd);

// 選擇後,popupwindow自動消失
pw.dismiss();
}

});
}

private void login() {

// 若是第二次開啟軟體時,將map在size賦給i,若是不是,則i只要實現i++
if (i == 0) {
i = map.size()/2;
}

// 若有勾選記住,則儲存
if (checkBox.isChecked()) {
String name = et_user.getText().toString().trim();
String pwd = et_pwd.getText().toString().trim();
if (!"".equals(name) && !"".equals(pwd)) {

sPreferences.edit().putString("name"+ i, name)
.putString("pwd"+ i, pwd).commit();
i++;

Toast.makeText(this,"使用者名稱與密碼已經記住,下次進入軟體時將生效!",0).show();

}
}

}
}