用户登录保存数据实例(慕课笔记 使用SharedPreferences保存用户名)

学习视频之后自己操作时的笔记。

0.视频地址:http://www.imooc.com/video/3265

1.功能预览:

说明:1)输入错误用户名和密码,点击登录,弹出提示框“禁止登录”;

        2)输入正确用户名和密码,点击登录,弹出提示框“登录成功”;

        3)输入正确用户名和密码,并且勾选保存用户名,点击登录,弹出框显示“登录成功”,退出APP,再次打开,用户名已有。

2.具体布局:

 activity_main.xml:

<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"
    tools:context=".MainActivity" >

     <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_alignParentTop="true"
        android:layout_marginTop="16dp"
        android:text="用户名:" />

    <EditText
        android:id="@+id/etuserName"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentRight="true"
        android:layout_alignTop="@+id/textView1"
        android:layout_toRightOf="@+id/textView1"
        android:ems="10" />

    <TextView
        android:id="@+id/aa"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_below="@+id/etuserName"
        android:text="密        码" />

    <EditText
        android:id="@+id/etuserPass"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@+id/etuserName"
        android:layout_alignParentRight="true"
        android:layout_alignTop="@+id/aa"
        android:ems="10" >

        <requestFocus />
    </EditText>

    <Button
        android:id="@+id/btnLogin"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_below="@+id/etuserPass"
        android:layout_marginTop="62dp"
        android:onClick="doClick"
        android:text="登陆" />

    <Button
        android:id="@+id/btnCancel"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignBaseline="@+id/btnLogin"
        android:layout_alignBottom="@+id/btnLogin"
        android:layout_toRightOf="@+id/btnLogin"
        android:onClick="doClick"
        android:text="取消" />

    <CheckBox
        android:id="@+id/checkBox1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignRight="@+id/btnCancel"
        android:layout_below="@+id/etuserPass"
        android:layout_marginTop="15dp"
        android:checked="false"
        android:text="保存用户名" />
    
</RelativeLayout>
View Code

 

3.MainActivity.java:

public class MainActivity extends Activity {
    EditText etUserName,etUserPass;
    CheckBox chk;
    SharedPreferences pref;
    Editor editor;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        //绑定对应布局控件
        etUserName=(EditText)findViewById(R.id.etuserName);
        etUserPass=(EditText)findViewById(R.id.etuserPass);
        chk=(CheckBox)findViewById(R.id.checkBox1);
        pref=getSharedPreferences("UserInfo",MODE_PRIVATE);
        editor=pref.edit();
        String name=pref.getString("userName", "");
        
        if(name==null){
            chk.setChecked(false);
        }else{
            chk.setChecked(true);
            etUserName.setText(name);
        }
    }
//为按钮添加响应
public void doClick(View v){
    switch(v.getId()){
    case R.id.btnLogin:
        //转成字符串进行判断
        String name=etUserName.getText().toString().trim();
        String pass=etUserPass.getText().toString().trim();
        if("admin".equals(name)&&"123456".equals(pass)){
            if(chk.isChecked()){
                //用户名与密码均正确且保存用户名确认框处于选,
                //则保存数据并提交到数据库
                editor.putString("userName", name);
                editor.commit();
            }else{
                editor.remove("userName");
                editor.commit();
            }
            //加信息提示框
            Toast.makeText(MainActivity.this, "登录成功", 
                    Toast.LENGTH_LONG).show();
            }else{Toast.makeText(MainActivity.this, "禁止登录", 
                Toast.LENGTH_LONG).show();    
        }
        break;
        default:
            break;
    }
}
    
}

 

posted on 2017-02-04 11:59  moonlight.ml  阅读(612)  评论(0编辑  收藏  举报

导航