实现记住密码功能

Android——P235

实现登陆界面记住密码功能:

xml代码:

 

  1 <?xml version="1.0" encoding="utf-8"?>
  2 <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
  3     xmlns:tools="http://schemas.android.com/tools"
  4     android:layout_width="match_parent"
  5     android:layout_height="match_parent"
  6     android:background="#E6E6E6"
  7     android:orientation="vertical" >
  8     <ImageView
  9         android:id="@+id/iv_head"
 10         android:layout_width="50dp"
 11         android:layout_height="50dp"
 12         android:layout_centerHorizontal="true"
 13         android:layout_marginTop="40dp"
 14         android:src="@mipmap/dddss" />
 15     <LinearLayout
 16         android:id="@+id/layout"
 17         android:layout_width="match_parent"
 18         android:layout_height="wrap_content"
 19         android:layout_below="@+id/iv_head"
 20         android:layout_margin="10dp"
 21         android:background="#ffffff"
 22         android:orientation="vertical" >
 23         <!--账号-->
 24         <RelativeLayout
 25             android:id="@+id/rl_username"
 26             android:layout_width="match_parent"
 27             android:layout_height="wrap_content"
 28             android:layout_margin="10dp" >
 29 
 30             <TextView
 31                 android:id="@+id/tv_name"
 32                 android:layout_width="wrap_content"
 33                 android:layout_height="wrap_content"
 34                 android:layout_centerVertical="true"
 35                 android:text="账号:" />
 36 
 37             <EditText
 38                 android:id="@+id/et_number"
 39                 android:layout_width="match_parent"
 40                 android:layout_height="wrap_content"
 41                 android:layout_marginLeft="5dp"
 42                 android:layout_toRightOf="@+id/tv_name"
 43                 android:background="@null" />
 44         </RelativeLayout>
 45         <View
 46             android:layout_width="match_parent"
 47             android:layout_height="2dp"
 48             android:background="#E6E6E6" />
 49         <!--密码-->
 50         <RelativeLayout
 51             android:id="@+id/rl_userpsw"
 52             android:layout_width="match_parent"
 53             android:layout_height="wrap_content"
 54             android:layout_margin="10dp" >
 55 
 56             <TextView
 57                 android:id="@+id/tv_psw"
 58                 android:layout_width="wrap_content"
 59                 android:layout_height="wrap_content"
 60                 android:layout_centerVertical="true"
 61                 android:text="密码:" />
 62 
 63             <EditText
 64                 android:id="@+id/et_password"
 65                 android:layout_width="match_parent"
 66                 android:layout_height="wrap_content"
 67                 android:layout_marginLeft="5dp"
 68                 android:layout_toRightOf="@+id/tv_psw"
 69                 android:inputType="textPassword"
 70                 android:background="@null" />
 71         </RelativeLayout>
 72         <View
 73             android:layout_width="match_parent"
 74             android:layout_height="2dp"
 75             android:background="#E6E6E6" />
 76         <!--记住密码-->
 77         <RelativeLayout
 78             android:layout_width="match_parent"
 79             android:layout_height="wrap_content"
 80             >
 81             <CheckBox
 82                 android:layout_width="wrap_content"
 83                 android:layout_height="wrap_content"
 84                 android:id="@+id/chkRemember"
 85                 android:text="记住密码"
 86                 android:checked="false"
 87                 />
 88         </RelativeLayout>
 89     </LinearLayout>
 90     <Button
 91         android:id="@+id/btn_login"
 92         android:layout_width="match_parent"
 93         android:layout_height="wrap_content"
 94         android:layout_below="@+id/layout"
 95         android:layout_centerHorizontal="true"
 96         android:layout_marginLeft="10dp"
 97         android:layout_marginRight="10dp"
 98         android:layout_marginTop="20dp"
 99         android:background="#3C8DC4"
100         android:text="登录"
101         android:textColor="#ffffff" />
102 </RelativeLayout>

 

——————————————————————————————————————————

Java代码:

Button btn;
    EditText eduse;
    EditText edpsw;
    CheckBox checkbox;
    SharedPreferences sp = null;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        btn = (Button) findViewById(R.id.btn_login);
        eduse = (EditText) findViewById(R.id.et_number);
        edpsw = (EditText) findViewById(R.id.et_password);
        checkbox = (CheckBox) findViewById(R.id.chkRemember);
        sp = getSharedPreferences("userinfo",MODE_PRIVATE);

        if (sp.getBoolean("checkboxBoolean", true))
        {
            eduse.setText(sp.getString("num", null));
            edpsw.setText(sp.getString("psw", null));
            checkbox.setChecked(true);
        }
        btn.setOnClickListener(new View.OnClickListener()
        {
            @Override
            public void onClick(View v) {
                boolean CheckBoxLogin = checkbox.isChecked();
                if (CheckBoxLogin) {
                    SharedPreferences.Editor editor = sp.edit();
                    editor.putString("num", eduse.getText().toString());
                    editor.putString("psw", edpsw.getText().toString());
                    editor.putBoolean("auto", true);
                    editor.commit();
                } else {
                    SharedPreferences.Editor editor = sp.edit();
                    editor.putString("num", null);
                    editor.putString("psw", null);
                    editor.putBoolean("auto", false);
                    editor.commit();
                }
                Toast.makeText(MainActivity.this, "登陆成功!", Toast.LENGTH_SHORT).show();
            }
        }
        );
    }

 

posted @ 2016-11-16 10:33  X'n  阅读(496)  评论(0编辑  收藏  举报

首页 - 创建于 2016年11月15日

内容提供自个人生活!

致力于情感之间


Font Awesome | Respond.js | Bootstrap中文网