如何在Android中使用SharedPreferences更改PIN码?

时间:2015-06-10 11:40:43

标签: android sharedpreferences

我创建了一个具有4位PIN码的应用程序。我把它保存在像String defaultPin="1234";这样的字符串变量中。我在OptionsMenu中提供了一个更改PIN的选项。如果我们点击更改PIN码,它会提示Dialog有三个EditText框以获取旧密码,新密码和重新输入密码以及Button以保存密码。它可以正常更改PIN码。但是,如果我再次重新启动应用程序,则新的PIN无效。默认PIN有效。 我的代码是

public class LoginActivity extends ActionBarActivity {

String defaultPin="1234";
EditText etOldPin,etNewPin,etRePin;
Button btnCPin;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_login);

    final EditText etPin=(EditText)findViewById(R.id.editText);
    final ImageButton btnTest=(ImageButton)findViewById(R.id.imageButton);

    btnTest.setOnClickListener(new OnClickListener() {
        @Override
        public void onClick(View v) {
            if (etPin.getText().toString().equals(defaultPin)){

                Intent i=new Intent(LoginActivity.this,DashboardActivity.class);
                startActivity(i);
            } else {
                Toast.makeText(LoginActivity.this,"Error! Check PIN",Toast.LENGTH_LONG).show();

            }
        }
    });
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {

    getMenuInflater().inflate(R.menu.menu_login, menu);
    return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    int id = item.getItemId();

    switch (id){
        case R.id.changePin:
            Toast.makeText(LoginActivity.this,"Change Pin Clicked",Toast.LENGTH_LONG).show();
            showDialogPin();
            return true;
        default:
            return super.onOptionsItemSelected(item);
    }
}

private void showDialogPin() {
    AlertDialog.Builder alertBuilder=new AlertDialog.Builder(this);
    LayoutInflater inflater=this.getLayoutInflater();
    View dialogView=inflater.inflate(R.layout.change_pin, null);
    alertBuilder.setView(dialogView);
    AlertDialog alertDialog=alertBuilder.create();
    alertDialog.show();
    etOldPin=(EditText)alertDialog.findViewById(R.id.editText3);
    etNewPin=(EditText)alertDialog.findViewById(R.id.editText9);
    etRePin=(EditText)alertDialog.findViewById(R.id.editText10);
    btnCPin=(Button)alertDialog.findViewById(R.id.button);

    btnCPin.setOnClickListener(new OnClickListener() {
        @Override
        public void onClick(View v) {
            String strOldPin=etOldPin.getText().toString();
            String strNewPin=etNewPin.getText().toString();
            String strRePin=etRePin.getText().toString();

            if (strOldPin.equals("") || strNewPin.equals("") || strRePin.equals("")) {
                Toast.makeText(LoginActivity.this, "All Fields must be filled", Toast.LENGTH_LONG).show();
            } else {
                if (strOldPin.equals(defaultPin)) {
                    if (strNewPin.equals(strRePin)) {
                        defaultPin = strNewPin;
                        Toast.makeText(LoginActivity.this, "PIN Changed", Toast.LENGTH_LONG).show();
                    } else {
                        Toast.makeText(LoginActivity.this, "PIN's Mismatch", Toast.LENGTH_LONG).show();
                    }
                } else {
                    Toast.makeText(LoginActivity.this, "Please Enter exact Old PIN", Toast.LENGTH_LONG).show();
                }
            }
        }
    });
}

}

这是我的代码。 截图是 enter image description here

请帮助我......

使用SharedPreference

进行编辑
public class LoginActivity extends ActionBarActivity {

EditText etOldPin,etNewPin,etRePin;
Button btnCPin;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_login);

    final EditText etPin=(EditText)findViewById(R.id.editText);
    final ImageButton btnTest=(ImageButton)findViewById(R.id.imageButton);

    final SharedPreferences preferences=getSharedPreferences("account",MODE_PRIVATE);
    Editor editor=preferences.edit();
    editor.putString("pin","1234");
    editor.commit();
    editor.clear();


    btnTest.setOnClickListener(new OnClickListener() {
        @Override
        public void onClick(View v) {
            if (etPin.getText().toString().equals(preferences.getString("pin",""))) {
                Intent i = new Intent(LoginActivity.this, DashboardActivity.class);
                startActivity(i);
            } else {
                Toast.makeText(LoginActivity.this, "Error! Check PIN", Toast.LENGTH_LONG).show();
            }
        }
    });
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    getMenuInflater().inflate(R.menu.menu_login, menu);
    return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    int id = item.getItemId();

    switch (id){
        case R.id.changePin:
            Toast.makeText(LoginActivity.this,"Change Pin Clicked",Toast.LENGTH_LONG).show();
            showDialogPin();
            return true;
        default:
            return super.onOptionsItemSelected(item);
    }
}

private void showDialogPin() {
    AlertDialog.Builder alertBuilder=new AlertDialog.Builder(this);
    LayoutInflater inflater=this.getLayoutInflater();
    View dialogView=inflater.inflate(R.layout.change_pin, null);
    alertBuilder.setView(dialogView);
    AlertDialog alertDialog=alertBuilder.create();
    alertDialog.setCancelable(true);
    alertDialog.show();
    etOldPin=(EditText)alertDialog.findViewById(R.id.editText3);
    etNewPin=(EditText)alertDialog.findViewById(R.id.editText9);
    etRePin=(EditText)alertDialog.findViewById(R.id.editText10);
    btnCPin=(Button)alertDialog.findViewById(R.id.button);

    btnCPin.setOnClickListener(new OnClickListener() {
        @Override
        public void onClick(View v) {
            String strOldPin=etOldPin.getText().toString();
            String strNewPin=etNewPin.getText().toString();
            String strRePin=etRePin.getText().toString();

            SharedPreferences preferences=getSharedPreferences("account",MODE_PRIVATE);
            Editor editor=preferences.edit();
            String storedPin=preferences.getString("pin","");
            if (strOldPin.equals("") || strNewPin.equals("") || strRePin.equals("")) {
                Toast.makeText(LoginActivity.this, "All Fields must be filled", Toast.LENGTH_LONG).show();
            } else {
                if (strOldPin.equals(storedPin)) {
                    if (strNewPin.equals(strRePin)) {
                       editor.putString("pin",strNewPin);
                        editor.commit();
                        editor.clear();
                        Toast.makeText(LoginActivity.this, "PIN Changed", Toast.LENGTH_LONG).show();
                    } else {
                        Toast.makeText(LoginActivity.this, "PIN's Mismatch", Toast.LENGTH_LONG).show();
                    }
                } else {
                    Toast.makeText(LoginActivity.this, "Please Enter exact Old PIN", Toast.LENGTH_LONG).show();
                }
            }
        }
    });
}

}

2 个答案:

答案 0 :(得分:2)

重新启动应用后它无效的原因是共享首选项未使用新引脚更新。

您需要使用btnCPin click listener

中的新引脚更新共享首选项

要更新共享偏好,请查看
http://developer.android.com/training/basics/data-storage/shared-preferences.html

答案 1 :(得分:0)

从共享偏好

编辑数据
SharedPreferences.Editor editor = getPreferences(MODE_PRIVATE).edit();
editor.putString("PIN", defaultPin.getText().toString());
editor.commit();

从共享偏好设置检索数据

SharedPreferences prefs = getPreferences(MODE_PRIVATE); 
String restoredPin = prefs.getString("PIN", null);
if (restoredPin != null) 
{
  //HERE YOU HAVE AVAILABLE YOUR PIN IN restoredPin 
}

更多信息:

Data Storage

Example 1

Example 2