Android data storage - SharedPreferences

1. Steps to store data using SharedPreferences

Steps to store data using SharedPreferences

2. Steps to read data using SharedPreferences

Steps to read data using SharedPreferences

Next, we use an example to familiarize, understand, and master SharedPreferences

Create two pages or two activities

The first page is the login page, with three controls: a username edit box, a password edit box, and a login button.

Second page customization. (Remember to register in Manifest)

Realized function: Enter the user name and password defined in the background for the first time, then jump to the second page; otherwise, the login fails. If you log in successfully for the first time and then enter this application, you don't need to enter the user name and password, it will directly jump to the second page.

public class MainActivity extends AppCompatActivity {
    
    

    @Override
    protected void onCreate(Bundle savedInstanceState) {
    
    
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        EditText editText = findViewById(R.id.editText);
        EditText editText2 = findViewById(R.id.editText2);
        Button button = findViewById(R.id.button);
        
		//获取SharedPreferences对象
        SharedPreferences sp = getSharedPreferences("login",MODE_PRIVATE);
        
        //给sp里面放置默认数据(键值对的方式)
        String username = sp.getString("username",null);
        String password = sp.getString("password",null);
        
        //如果username和password是admin、123456,则直接跳转到第二个页面
        if(username!=null && password!=null) {
    
    
            if (username.equals("admin") && password.equals("123456")) {
    
    
                Intent intent = new Intent(MainActivity.this, MainActivity2.class);
                startActivity(intent);
            }
        }else{
    
       //否则给button设置监听器
           button.setOnClickListener(new View.OnClickListener() {
    
    
               @Override
               public void onClick(View v) {
    
    
                   String Input_username = editText.getText().toString();
                   String Input_password = editText2.getText().toString();

                   SharedPreferences.Editor editor = sp.edit();       //获取editor对象

             //如果输入的用户名和密码为admin、123456则将用户名和密码放到editor里面,并将editor进行提交,并跳转到第二个页面
                   if (Input_username.equals("admin") && Input_password.equals("123456")) {
    
    
                       editor.putString("username", Input_username);
                       editor.putString("password", Input_password);
                       editor.commit();
                       Intent intent = new Intent(MainActivity.this, MainActivity2.class);
                       startActivity(intent);
                       Toast.makeText(MainActivity.this, "用户名和密码已经保存", Toast.LENGTH_SHORT).show();
                   } else {
    
    
                       Toast.makeText(MainActivity.this, "用户名和密码错误", Toast.LENGTH_SHORT).show();
                   }

               }
           });
        }
    }
}

Guess you like

Origin blog.csdn.net/qq_43467892/article/details/117879096