<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="bawei.com.xliscom.administrator.spjizhumima"> <application android:allowBackup="true" android:icon="@mipmap/ic_launcher" android:label="@string/app_name" android:supportsRtl="true" android:theme="@style/AppTheme"> <activity android:name=".MainActivity"> <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> <activity android:name=".TwoActivity"></activity> </application> </manifest>package bawei.com.xliscom.administrator.spjizhumima; import android.content.Intent; import android.content.SharedPreferences; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.text.TextUtils; import android.view.View; import android.widget.Button; import android.widget.CheckBox; import android.widget.EditText; import android.widget.Toast; public class MainActivity extends AppCompatActivity implements View.OnClickListener { private EditText et_name; private EditText et_pass; private Button btn_login; private CheckBox cb_login; private SharedPreferences sp; private SharedPreferences.Editor editor; private CheckBox cb; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); initview(); } private void initview() { et_name = (EditText) findViewById(R.id.et_name); et_pass = (EditText) findViewById(R.id.et_pass); btn_login = (Button) findViewById(R.id.btn_login); cb_login = (CheckBox) findViewById(R.id.cb_login); cb = (CheckBox) findViewById(R.id.zidong); sp=getSharedPreferences("conf",MODE_PRIVATE); editor=sp.edit(); btn_login.setOnClickListener(this); boolean flag = sp.getBoolean("boolean", false); if(flag){ cb_login.setChecked(true); String name = sp.getString("name", ""); String trim = sp.getString("pass", ""); et_name.setText(name); et_pass.setText(trim); } boolean cb2 = sp.getBoolean("cb", false); if(cb2){ cb.setChecked(true); Intent in=new Intent(MainActivity.this,TwoActivity.class); startActivity(in); } } @Override public void onClick(View v) { //记住密码 boolean checked = cb_login.isChecked(); if(checked){ String name = et_name.getText().toString().trim(); String trim = et_pass.getText().toString().trim(); if(TextUtils.isEmpty(name)||TextUtils.isEmpty(trim)){ Toast.makeText(MainActivity.this, "请输入完整信息", Toast.LENGTH_SHORT).show(); } editor.putString("name",name); editor.putString("pass",trim); editor.putBoolean("boolean",true); editor.commit(); }else{ editor.clear(); editor.commit(); } final boolean checked1 = cb.isChecked(); if(checked1){ editor.putBoolean("cb",true); editor.commit(); } Intent in=new Intent(MainActivity.this,TwoActivity.class); startActivity(in); } }
sharedpreference自动登陆记住密码
最新推荐文章于 2023-01-09 00:36:29 发布