First, configure the login xml file, and set two EditText, a Button and a CheckBox;
The configuration code is as follows:
Then configure the code of the main activity page. The main content is to set the click to listen event for the button. If you listen to the clicked event, you need to perform the following operations, First, check whether the CheckBox is selected to call the ischecked method in checkbox. If it is selected through if statement, it needs to remember the operation of the input account password. The key operation to remember the password is * * SharedPreferences * * this project uses this method to save the user name and password. The data storage is very similar to the Map collection. The key codes stored in the way of key value pairs are as follows : SharedPreferences config = config.getsharedpreferences ("config", mode "private); where config is the file name and mode" private "is the permission of the file. Then the editor to edit this file is obtained by defining the editor. The main code is as follows Editor editor=config.edit(); / / get the editor to edit this file editor.putString("username",et_userName); / / edit content with editor editor.commit() / / call this release to save the data. The specific execution code is as follows:<EditText android:id="@+id/et_userName" android:layout_width="300dp" android:layout_height="wrap_content" /> </LinearLayout> <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="wrap_content" android:orientation="horizontal"> <TextView android:layout_height="wrap_content" android:layout_width="100dp" android:text="dense code:" android:textSize="30dp" android:textColor="@android:color/black" /> <EditText android:id="@+id/et_passWord" android:layout_width="300dp" android:layout_height="wrap_content" /> </LinearLayout> <CheckBox android:id="@+id/cbox_remember" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center_horizontal" android:text="Remember password" /> <Button android:id="@+id/btn_login" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center_horizontal" android:text="Land" />
public class MainActivity extends AppCompatActivity {
private EditText et_userName;
private EditText et_passWord;
private CheckBox cbox_remember;
private Button btn_login;
private SharedPreferences config;
protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); config = getSharedPreferences("config", MODE_PRIVATE); et_passWord = findViewById(R.id.et_passWord); et_userName = findViewById(R.id.et_userName); cbox_remember = findViewById(R.id.cbox_remember); btn_login = findViewById(R.id.btn_login); boolean isChecked = config.getBoolean("isChecked", false); if (isChecked) { et_userName.setText(config.getString("username", "")); et_passWord.setText(config.getString("password", "")); } cbox_remember.setChecked(isChecked); btn_login.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { SharedPreferences.Editor editor = config.edit(); String username = et_userName.getText().toString(); String password = et_passWord.getText().toString(); boolean isChecked = cbox_remember.isChecked(); //Get whether selected editor.putBoolean("isChecked", isChecked); //Save the selected information to the editor if (cbox_remember.isChecked()) { editor.putString("username", username).putString("password", password); } else { editor.remove("username").remove("password"); } editor.commit(); Toast.makeText(MainActivity.this, "Landing successfully", Toast.LENGTH_SHORT).show(); } }); }
}