Android program account password setting

Register the account and password on the registration interface. After the registration is successful, you will enter the next login interface. Enter the account number and password you just registered to enter the next main interface. If you forget the password key registered just now, go back to the previous registration interface to re-register. :

The layout interface is not uploaded, only the functions implemented by the code are uploaded, if you need the complete file, you can dd me!

MainActivity: The Java file stores the registered account number and password

package com.example.china;



import android.app.Activity;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.nfc.Tag;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

public class MainActivity extends Activity {
    private Button button;

    private EditText account;
    private EditText password;
     String TAG = "MainActivity";
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        button = (Button) findViewById(R.id.button);
        account = (EditText) findViewById(R.id.account);
        password = (EditText) findViewById(R.id.password);
        button.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                SharedPreferences.Editor editor = getSharedPreferences("data", MODE_PRIVATE).edit();//调用SharedPreferences对象的edit()方法来获取一个SharedPreferences.Editor对象
                editor.putString("name", account.getText().toString());//保存获取编辑栏上的内容植入键name里并将其放在editor中
                editor.putString("password", password.getText().toString());//
                editor.commit();//提交数据
                Toast.makeText(MainActivity.this,"注册成功  欢迎您成为党党流传用户",Toast.LENGTH_SHORT).show();
                Intent intent = new Intent(MainActivity.this, Main2Activity.class);//跳转activity_main.xml界面
                startActivity(intent);
            }
        });
    }

}

Main2Activity: Java file, the account number and password entered in the login interface are supervised by the supervisor, if they are the same as the registration information, an agreement dialog box will pop up, click agree to enter the next line interface, click exit to close the program!

package com.example.china;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.preference.PreferenceManager;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.Toast;


public class Main2Activity extends Activity{
    private EditText accountEdit;
    private EditText passwordEdit;
    private Button login;
    private SharedPreferences pref;
    private SharedPreferences.Editor editor;
    private CheckBox rememberpass;
    private Button registe;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main2);

        accountEdit = (EditText) findViewById(R.id.account);
        passwordEdit = (EditText) findViewById(R.id.password);
        login = (Button) findViewById(R.id.login);
        rememberpass = (CheckBox) findViewById(R.id.remember_password);
        registe = (Button) findViewById(R.id.registe);
        registe.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {//点击注册,回到上一注册界面
                // TODO Auto-generated method stub
                Intent intent = new Intent(Main2Activity.this, MainActivity.class);
                startActivity(intent);
            }
        });

        pref = PreferenceManager.getDefaultSharedPreferences(getApplicationContext());//获取 SharedPreferences 对象

        boolean isRemember = pref.getBoolean("remember_password", false);
        if(isRemember){
            String account = pref.getString("account", "");
            String password = pref.getString("password", "");
            accountEdit.setText(account);
            passwordEdit.setText(password);
            rememberpass.setChecked(true);
        }
        login.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {

                // TODO Auto-generated method stub
                String account = accountEdit.getText().toString();
                String password = passwordEdit.getText().toString();
                SharedPreferences pref = getSharedPreferences("data", MODE_PRIVATE);

                //.调用get方法,每种get方法都对应了SharedPreferences.Editor 中的一种put方法,
                // 读取一个布尔型数据则用 getBoolean()方法.
                // 这些get方法都接收两个参数,第一个参数是键,第二个参数是默认值,即当传入的键找不到对应的值时,返回这个歌默认值
                if(account.equals(pref.getString("name", ""))&&password.equals(pref.getString("password", ""))){
                    editor = pref.edit();
                    if (rememberpass.isChecked()) {
                        editor.putBoolean("remember_password", true);
                    }
                    else{
                        editor.clear();
                    }
                    editor.commit();
                   /* Intent intent = new Intent(Main2Activity.this, Main3Activity.class);
                    startActivity(intent);*/
                    AlertDialog.Builder builder=new AlertDialog.Builder(Main2Activity.this);
                    builder.setTitle("党党流传用户协议");
                    builder.setMessage("党党流传是宣扬中国共产党人物,事迹,红色景点及红色精神等积极向上的内容,用户必须遵守党党流传的要求规定,严禁用户" +
                            "将党党流传app里面的内容进行非法修改,以及商用,营造良好的社区氛围,希望你在党党流传能够培养良好的生活作风!");
                    builder.setNegativeButton("退出", new DialogInterface.OnClickListener() {//
                        @Override
                        public void onClick(DialogInterface dialogInterface, int i) {
                            Intent intent = new Intent(Intent.ACTION_MAIN);
                            intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                            intent.addCategory(Intent.CATEGORY_HOME);
                            startActivity(intent);
                            System.exit(0);
                        }
                    });
                    builder.setPositiveButton("同意", new DialogInterface.OnClickListener() {
                        @Override
                        public void onClick(DialogInterface dialogInterface, int i) {
                            Intent intent = new Intent(Main2Activity.this, Main3Activity.class);
                    startActivity(intent);
                        }
                    });
                    builder.create().show();
                    //  finish();
                }
                else {
                    Toast.makeText(Main2Activity.this, "用户名或密码不对", Toast.LENGTH_LONG).show();
                }
            }
        });
    }


}

screenshot:

 

 

 

 

Supongo que te gusta

Origin blog.csdn.net/Abtxr/article/details/124993641
Recomendado
Clasificación