观察QQ的密码保存效果有以下几个特点:
新的账号会保存起来,如果是从下拉列表里选择的账号则不会保存;
2.点击向下的箭头会展开以前登陆过的按钮;
3.如果删除账号的一个字符,密码都会消失;
4.打开这个页面就会显示上一次登录的账号,如果没有为空白;
5.如果只删除密码而账号不变;并且删除一个字符整个字符串全部清零;
6.如果再下拉列表里选择一组账号密码,如果在账号的栏里点击删除,会清空密码栏;如果在密码栏点击删除,也会清空密码栏。如果是因为删除账号而导致密码栏被清空,再次输入密码时后再点击删除账号,密码栏不会被清空。密码栏被清空后再输入一组密码再次删除密码密码栏不会被清空。
解决方案:
第1.:把每一个组密码都放到sp中,比如第一组的账号的键值是name0,密码的键值是pwd0;假设一共存储了n组密码,第n组的账号的键值是name n-1,密码 的键值是pwd n-1;、
第2:点击向下的箭头,则会弹出一个popupwindow,里面是自己写listView,然后用map<String,String>将SP存储的所有账号密码getAll;得到map.size,遍历整个map将其存储到表。点击每个listview子项时,将子项的内容赋值到密码框和账号框;
第3.:为账号框添加监听事件;
第4.:我有三个方案,第一是登录时把账号密码放到saveInstanceState,再启动这个页面时检查赋值的,但是不知道为什么就是没有成功;第二个方案就是当我每当启动这个页面就把SP存储的数据Map.getAll;当我登陆的时候我把当前我用来登录的账号密码的键值设置为最大,打个比方说:假如我之前存储了5个账号,如果我用name2的账号密码的登陆;那么登录时我将name2的账号密码改为name4/pwd4;原先的name3--name4变为name2--name3;当我重新打开这个页面时直接将键值号最大的账号密码直接放入密码栏账号栏;第三个方案也是我采用 的方案,直接再新设置一个SP专门存放登录的账号密码;
第5..:为密码栏设置监听事件;
第6.:我设置了两个Boolean变量,初始化为false;当页面打开密码框账号框被赋值或者是我从listView里面选择了一组账号密码从而密码框账号框被赋值时,两个变量被赋值为true;当账号密码其中一个被修改时,其对应的Boolean赋值为false;把之前设置的密码框账号框的监听事件用Boolean的判定下,当两个Boolean都不是true时,两个框才能随便修改;
这是登录界面的布局:activity_second_login.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@drawable/loginbg"
tools:context=".SecondLogin">
<TextView
android:layout_width="match_parent"
android:layout_height="0.5dp"
android:layout_centerInParent="true"
android:id="@+id/textView2" />
<RelativeLayout
android:background="@drawable/loginkuang"
android:layout_marginLeft="30dp"
android:layout_marginRight="30dp"
android:layout_width="match_parent"
android:layout_height="60dp"
android:layout_above="@+id/textView2"
android:layout_alignParentStart="true"
android:id="@+id/relativeLayout"
android:layout_alignParentLeft="true">
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:src="@drawable/youjian"
android:layout_marginLeft="15dp"
android:id="@+id/imageView21" />
<TextView
android:layout_width="2dp"
android:layout_height="40dp"
android:layout_centerVertical="true"
android:layout_toEndOf="@+id/imageView21"
android:layout_marginStart="12dp"
android:background="#2b2b2b"
android:id="@+id/textView57"
android:layout_toRightOf="@+id/imageView21"
android:layout_marginLeft="12dp" />
<RelativeLayout
android:layout_width="300dp"
android:layout_height="45dp"
android:id="@+id/parent"
android:layout_centerVertical="true"
android:layout_toEndOf="@+id/textView57"
android:layout_toRightOf="@+id/textView57">
<EditText
android:layout_marginLeft="10dp"
android:hint="请输入账号/手机号"
android:textColorHint="#FFFFFF"
android:layout_width="218dp"
android:layout_height="45dp"
android:textColor="#FFFFFF"
android:maxLength="11"
android:layout_centerVertical="true"
android:id="@+id/zhanghuming"
android:background="#00FFFFFF" />
<ImageView
android:layout_width="72px"
android:layout_height="72px"
android:layout_marginRight="10dp"
android:id="@+id/select"
android:src="@drawable/xiangxiajiantou"
android:layout_centerVertical="true"
android:layout_alignParentEnd="true"
android:layout_alignParentRight="true" />
</RelativeLayout>
</RelativeLayout>
<RelativeLayout
android:background="@drawable/loginkuang"
android:layout_width="match_parent"
android:layout_height="60dp"
android:layout_marginLeft="30dp"
android:layout_marginRight="30dp"
android:layout_below="@+id/textView2"
android:layout_alignParentStart="true"
android:id="@+id/relativeLayout2"
android:layout_alignParentLeft="true">
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/suo"
android:layout_marginLeft="15dp"
android:id="@+id/imageView22"
android:layout_centerVertical="true"
android:layout_alignParentStart="true"
android:layout_marginStart="20dp"
android:layout_alignParentLeft="true" />
<TextView
android:layout_width="2dp"
android:layout_height="40dp"
android:background="#2b2b2b"
android:layout_centerVertical="true"
android:layout_marginStart="14dp"
android:layout_toEndOf="@+id/imageView22"
android:id="@+id/textView58"
android:layout_marginLeft="14dp"
android:layout_toRightOf="@+id/imageView22" />
<EditText
android:layout_marginLeft="10dp"
android:hint="请输入密码"
android:maxLength="11"
android:textColorHint="#FFFFFF"
android:layout_width="300dp"
android:inputType="textPassword"
android:textColor="#FFFFFF"
android:layout_height="45dp"
android:layout_alignTop="@+id/textView58"
android:layout_toEndOf="@+id/textView58"
android:id="@+id/mima"
android:background="#00FFFFFF"
android:layout_toRightOf="@+id/textView58" />
</RelativeLayout>
<ImageView
android:layout_width="80dp"
android:layout_height="80dp"
android:src="@drawable/loginshipin"
android:layout_above="@+id/relativeLayout"
android:layout_centerHorizontal="true"
android:layout_marginBottom="56dp" />
<Button
android:layout_width="200dp"
android:layout_height="40dp"
android:text="登 录"
android:textSize="22dp"
android:textColor="#FFFFFF"
android:background="@drawable/loginbutton"
android:layout_marginTop="44dp"
android:layout_below="@+id/relativeLayout2"
android:layout_centerHorizontal="true"
android:id="@+id/logainmainbutton" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="忘记密码?"
android:textColor="#FFFFFF"
android:layout_below="@+id/logainmainbutton"
android:layout_centerHorizontal="true"
android:layout_marginTop="25dp"
android:id="@+id/loginmaintextview2" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="注册账号?"
android:textColor="#FFFFFF"
android:layout_below="@+id/logainmainbutton"
android:layout_centerHorizontal="true"
android:layout_marginTop="60dp"
android:id="@+id/loginmaintexrview1" />
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="30dp"
android:layout_marginBottom="55dp"
android:layout_alignParentBottom="true"
android:layout_alignParentStart="true"
android:layout_alignParentLeft="true">
<TextView
android:layout_width="100dp"
android:layout_height="4dp"
android:background="#BBBBBB"
android:id="@+id/textView59"
android:layout_centerVertical="true"
android:layout_toStartOf="@+id/textView63"
android:layout_marginEnd="27dp"
android:layout_toLeftOf="@+id/textView63"
android:layout_marginRight="27dp" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="18dp"
android:text="or"
android:textColor="#FFFFFF"
android:layout_gravity="center"
android:layout_centerVertical="true"
android:layout_centerHorizontal="true"
android:id="@+id/textView63" />
<TextView
android:layout_width="100dp"
android:layout_height="4dp"
android:background="#BBBBBB"
android:layout_centerVertical="true"
android:layout_toEndOf="@+id/textView63"
android:layout_marginStart="27dp"
android:layout_toRightOf="@+id/textView63"
android:layout_marginLeft="27dp" />
</RelativeLayout>
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@mipmap/ic_qq"
android:layout_marginBottom="10dp"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:id="@+id/qqloginway" />
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@mipmap/ic_weixin"
android:layout_marginBottom="10dp"
android:layout_marginRight="40dp"
android:layout_alignParentBottom="true"
android:layout_toStartOf="@+id/qqloginway"
android:id="@+id/wechatloginway"
android:layout_toLeftOf="@+id/qqloginway" />
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@mipmap/ic_xinlang"
android:layout_marginLeft="40dp"
android:layout_marginBottom="10dp"
android:layout_alignParentBottom="true"
android:layout_toEndOf="@+id/qqloginway"
android:id="@+id/renrenloginway"
android:layout_toRightOf="@+id/qqloginway" />
</RelativeLayout>
其中PopupWindow需要加载一个布局option.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="wrap_content"
android:background="#FFFFFF"
android:layout_height="match_parent">
<ListView
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:id="@+id/op"></ListView>
</LinearLayout>
ListView的子项还需要写一个单独的布局:items.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="60dp">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/item"
android:layout_marginLeft="10dp"
android:layout_centerVertical="true"
android:textSize="20dp"/>
</RelativeLayout>
然后是SecondLogin.java:
public class SecondLogin extends AppCompatActivity implements View.OnClickListener{
private Button button3;
private ImageView imageButton1,imageButton2,imageButton,select;
private TextView textView1,textView2;
public static SecondLogin instance = null;
private SharedPreferences pref;
private SharedPreferences.Editor editor;
private SharedPreferences sPreferences;
private Map<String, String> map;
private PopupWindow pw;
private LinearLayout option;
private EditText zhanghuming,mima;
private ListView listView;
private int width, i;
private Boolean fir=false,sec =false;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_second_login);
initSetting();
initPopWindow();
initView();
}
private void initView() {
zhanghuming = (EditText) findViewById(R.id.zhanghuming);
mima = (EditText) findViewById(R.id.mima);
select = (ImageView) findViewById(R.id.select);
imageButton1 = (ImageView) findViewById(R.id.qqloginway);
imageButton2 = (ImageView) findViewById(R.id.renrenloginway);
imageButton = (ImageView) findViewById(R.id.wechatloginway);
button3 = (Button) findViewById(R.id.logainmainbutton);
textView1 = (TextView) findViewById(R.id.loginmaintexrview1);
textView2 = (TextView) findViewById(R.id.loginmaintextview2);
imageButton.setOnClickListener(this);
imageButton1.setOnClickListener(this);
imageButton2.setOnClickListener(this);
button3.setOnClickListener(this);
textView1.setOnClickListener(this);
textView2.setOnClickListener(this);
select.setOnClickListener(this);
pref = PreferenceManager.getDefaultSharedPreferences(this);
initData();
zhanghuming.addTextChangedListener(textWatcher);
mima.addTextChangedListener(a);
}
private void initData() {
String name1 = pref.getString("name1","");
String pwd1 = pref.getString("pwd1","");
zhanghuming.setText(name1);
mima.setText(pwd1);
if(!name1.equals("")){
sec = true;
}
if(!name1.equals("")){
fir = true;
}
}
private void initSetting() {
instance =this;
SysApplication.getInstance().addActivity(this);//透明状态栏
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
Window window = getWindow();// Translucent status bar
window.setFlags(
WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS,
WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
}
}
private TextWatcher textWatcher = new TextWatcher(){
@Override
public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {}
@Override
public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {
if(fir == true){
mima.setText("");
}
}
@Override
public void afterTextChanged(Editable editable) {
fir =false;
}
};
private TextWatcher a= new TextWatcher(){
int o;
@Override
public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {
o =mima.getText().toString().length();
}
@Override
public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {
if(o>mima.getText().toString().length() && sec == true) {
mima.setText("");
}
}
@Override
public void afterTextChanged(Editable editable) {
sec =false;
fir = false;
}
};
@Override
public void onClick(View view) {
switch (view.getId()){
case R.id.qqloginway ://向qq登录页面跳转
break;
case R.id.wechatloginway://向微信登录页面跳转
break;
case R.id.renrenloginway ://向新浪登录页面跳转
break;
case R.id.loginmaintexrview1 ://向注册页面跳转
break;
case R.id.loginmaintextview2 ://向帮助页面跳转
break;
case R.id.select:
//
pw.showAsDropDown(zhanghuming,-20,20);
break;
case R.id.logainmainbutton :
//
initSaveData();
if (i == 0) {
i = map.size()/2;
}
if(!isexist()) {
// 如果不存在,则保存
String name = zhanghuming.getText().toString().trim();
String pwd = mima.getText().toString().trim();
if (!"".equals(name) && !"".equals(pwd)) {
sPreferences.edit().putString("name" + i, name)
.putString("pwd" + i, pwd).commit();
i++;
Toast.makeText(SecondLogin.this, "用户名与密码已经记住,下次进入软件时将生效!", Toast.LENGTH_SHORT).show();
}
}
Intent intent2 = new Intent(SecondLogin.this, MainView.class);
startActivity(intent2);
break;
}
}
private void initPopWindow() {
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() * 3 / 5;
//实例化一个popupwindow对象
pw = new PopupWindow(option, width, LinearLayout.LayoutParams.WRAP_CONTENT, true);
ColorDrawable dw = new ColorDrawable(00000);
pw.setBackgroundDrawable(dw);
pw.setOutsideTouchable(true);
listView.setOnItemClickListener(new AdapterView.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, "");
zhanghuming.setText(username);
mima.setText(pwd);
fir =true;
sec =true;
// 选择后,popupwindow自动消失
pw.dismiss();
}
});
}
private void initSaveData() {
String text = zhanghuming.getText().toString();
String password_1 = mima.getText().toString();
editor = pref.edit();
editor.putString("name1",text);
editor.putString("pwd1",password_1);
editor.commit();
}
private boolean isexist() {
int in = 0;
sPreferences = getSharedPreferences("session", MODE_PRIVATE);
map = (Map<String, String>) sPreferences.getAll();
for(; in < (map.size() / 2); in++){
if(zhanghuming.getText().toString().
equals(sPreferences.getString("name" + in, ""))){
break;
}
}
if(in < (map.size() / 2)){
return true;
}
else{
return false;
}
}//
}