android記住登陸信息登陸狀態 使用SharePreference接口

public class MainActivity extends AppCompatActivity {
EditText ueditText, peditText;
CheckBox checkBox;
Button button;
/**
* ATTENTION: This was auto-generated to implement the App Indexing API.
* See https://g.co/AppIndexing/AndroidStudio for more information.
*/
private GoogleApiClient client;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ueditText = (EditText) findViewById(R.id.ed_user);
peditText = (EditText) findViewById(R.id.ed_password);
checkBox= (CheckBox) findViewById(R.id.cb_remember);
button= (Button) findViewById(R.id.button);
//使用SharePreferences取出保存的數據,並把數據顯示在手機屏幕上
//初始化數據
SharedPreferences sharedPreferences=getSharedPreferences("config",0);
//取出數據,若是取出的數據時空時,只需把getString("","")第二個參數設置成空字符串就好了,不用在判斷
String name=sharedPreferences.getString("name","");
String password=sharedPreferences.getString("password","");
//獲取勾選的狀態
boolean checkbox=sharedPreferences.getBoolean("checkbox",false);
ueditText.setText(name);
peditText.setText(password);
checkBox.setChecked(checkbox);
}
//使用Sharepreferences進行保存數據
public void login(View view){
//獲取密碼和用戶名
String username=ueditText.getText().toString();
String passwowrd=peditText.getText().toString();
//文本判斷是否爲空,新的API:TextUtils.isEmty()
if (TextUtils.isEmpty(username)&&TextUtils.isEmpty(passwowrd)){
Toast.makeText(MainActivity.this,"用戶名和密碼不能爲空",Toast.LENGTH_LONG).show();
}else{
System.out.println("之後補上");
if (checkBox.isChecked()){
//把密碼和用戶名存起來
//getSharedPreferences(name,model);,name 會生成一個xml文件,model :模式,可讀可寫等模式
SharedPreferences sp=getSharedPreferences("config",0);
SharedPreferences.Editor editor=sp.edit();
//把數據進行保存
editor.putString("name",username);
editor.putString("password",passwowrd);
//記住勾選的狀態
editor.putBoolean("checkbox",checkBox.isChecked());
//提交數據
editor.commit();
}else{
Toast.makeText(MainActivity.this,"未勾選",Toast.LENGTH_LONG).show();
}
}
}
/**
* Sharepreference使用的步驟
* 1.獲取sp的實例
* Sharepreference sp=getSharepreference(name,model);
* 2.獲取編輯器
* Editor editor=sp.edit();
* 3.存數據
* editor.putString(name,值)
* 4.提交
* editor.commit();
*/
}
xml文件
<LinearLayout 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:orientation="vertical"    tools:context="com.hx.myapplication.MainActivity">   <EditText       android:id="@+id/ed_user"       android:layout_width="match_parent"       android:layout_height="wrap_content"       android:hint="@string/husername"/>    <EditText        android:id="@+id/ed_password"        android:layout_width="match_parent"        android:layout_height="wrap_content"        android:password="true"        android:hint="@string/hpsword"/>    <RelativeLayout        android:layout_width="wrap_content"        android:layout_height="wrap_content">        <CheckBox            android:layout_marginTop="20dp"            android:id="@+id/cb_remember"            android:layout_width="wrap_content"            android:layout_height="wrap_content"            android:text="@string/remember"/>        <Button            android:id="@+id/button"            android:layout_width="match_parent"            android:layout_height="wrap_content"            android:layout_below="@+id/cb_remember"            android:text="@string/login"            android:onClick="login"/>    </RelativeLayout></LinearLayout>
相關文章
相關標籤/搜索