SharedPreferences

来源:互联网 发布:mysql持久化 编辑:程序博客网 时间:2024/06/16 12:15

用户名储存

简单的实现上一次的用户名信息储存

//MainActivity.javapublic class MainActivity extends AppCompatActivity {    EditText editText_name,editText_pass;    CheckBox checkBox;    Button button_dl,button_qx;    SharedPreferences.Editor editor;    String name;    @Override    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.activity_main);        editText_name = (EditText) findViewById(R.id.name);        editText_pass = (EditText) findViewById(R.id.pass);        checkBox = (CheckBox) findViewById(R.id.checkBox);        button_dl = (Button) findViewById(R.id.button);        SharedPreferences pref = getSharedPreferences("mypref",MODE_PRIVATE);        editor = pref.edit();        name = pref.getString("Username","");        if(name==null){            checkBox.setChecked(false);        }else{            checkBox.setChecked(true);            editText_name.setText(name);        }    }    public void dl_click(View v){        switch (v.getId()){            case R.id.button:                name = editText_name.getText().toString().intern();                String pass = editText_pass.getText().toString().intern();                if("admin".equals(name)&&"123456".equals(pass))                {                    if(checkBox.isChecked())                    {                        editor.putString("Username",name);                        editor.commit();                    }else{                        editor.remove("Username");                        editor.commit();                    }                    Toast.makeText(this,"登陆成功",Toast.LENGTH_SHORT).show();                }else{                    Toast.makeText(this,"登陆失败",Toast.LENGTH_SHORT).show();                }        }    }}
//activity_main.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:id="@+id/activity_main"    android:layout_width="match_parent"    android:layout_height="match_parent"    tools:context="com.example.administrator.demo1.MainActivity">    <EditText        android:layout_width="match_parent"        android:layout_height="wrap_content"        android:id="@+id/name"        android:layout_alignTop="@+id/textView1"        android:layout_toRightOf="@+id/textView1"        android:layout_toEndOf="@+id/textView1"        android:layout_marginLeft="12dp"        android:layout_marginStart="12dp" />    <TextView        android:text="用户名"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:id="@+id/textView1"        android:layout_marginLeft="32dp"        android:layout_marginStart="32dp"        android:layout_alignParentTop="true"        android:layout_alignLeft="@+id/checkBox"        android:layout_alignStart="@+id/checkBox"        android:layout_marginTop="45dp" />    <TextView        android:text="密码"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:id="@+id/textView2"        android:layout_marginTop="22dp"        android:layout_below="@+id/name"        android:layout_toLeftOf="@+id/name"        android:layout_toStartOf="@+id/name" />    <EditText        android:layout_width="match_parent"        android:layout_height="wrap_content"        android:id="@+id/pass"        android:layout_alignTop="@+id/textView2"        android:layout_alignLeft="@+id/name"        android:layout_alignStart="@+id/name" />    <CheckBox        android:text="保存用户名"        android:layout_width="match_parent"        android:layout_height="wrap_content"        android:checked="false"        android:id="@+id/checkBox"        android:layout_below="@+id/pass"        android:layout_alignParentLeft="true"        android:layout_alignParentStart="true"        android:layout_marginTop="19dp" />    <Button        android:text="登录"        android:onClick="dl_click"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:id="@+id/button"        android:layout_centerVertical="true"        android:layout_alignLeft="@+id/textView2"        android:layout_alignStart="@+id/textView2" />    <Button        android:text="取消"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:id="@+id/button2"        android:layout_marginRight="59dp"        android:layout_marginEnd="59dp"        android:layout_alignBaseline="@+id/button"        android:layout_alignBottom="@+id/button"        android:layout_alignParentRight="true"        android:layout_alignParentEnd="true" /></RelativeLayout>
0 0
原创粉丝点击