我希望有人可以提供帮助。对 Android/Java 来说还是很新的。我有一个包含多个活动的应用程序。当用户单击特定按钮时,我希望他们在继续之前需要登录。
我正在尝试这样做,因此如果密码为 NULL,它将提示创建密码,并在成功创建密码后继续进行活动。从那时起,如果应用程序重新启动,它会询问他们创建的密码以访问该活动。
这就是我所拥有的。我似乎无法设置密码。它始终保持为 NULL。我确定我对这段代码还有其他问题,但我正在尝试一步一步来。
package com.soboapps.todos;
import android.app.Activity;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.preference.PreferenceManager;
import android.view.Gravity;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;
public class Password extends Activity implements OnClickListener {
public static final String PASSWORD_PREF_KEY ="PasswordSharedPreferences";
public static final String PREFS_PRIVATE = "PREFS_PRIVATE";
public static final String KEY_PRIVATE = "KEY_PRIVATE";
public static final String PREFS_READ = "PREFS_READ";
public static final String KEY_READ = "KEY_READ";
public static final String PREFS_WRITE = "PREFS_WRITE";
public static final String KEY_WRITE = "KEY_WRITE";
public static final String PREFS_READ_WRITE = "PREFS_READ_WRITE";
public static final String KEY_READ_WRITE = "KEY_READ_WRITE";
EditText pass1, password, passwordEditText;
TextView messages, passHint;
//EditText pass1;
Button btnSubmit;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.password);
PreferenceManager.getDefaultSharedPreferences(getBaseContext());
SharedPreferences settings = this.getSharedPreferences("PasswordSharedPreferences", MODE_PRIVATE);
String password1 = settings.getString("User Password", "");
messages = (TextView) findViewById (R.id.text1);
pass1 = (EditText) findViewById(R.id.txtPassword);
btnSubmit = (Button) findViewById(R.id.btnSubmit);
passHint = (TextView) findViewById(R.id.textView1);
//passHint.setText(password1);
passwordEditText = (EditText) findViewById(R.id.password);
if(password1.isEmpty()) {
passHint.setText("Set a Secure Password");
}
btnSubmit.setOnClickListener(this);
}
public void onClick(View v) {
// TODO Auto-generated method stub
//PreferenceManager.getDefaultSharedPreferences(getBaseContext());
SharedPreferences settings = this.getSharedPreferences("PasswordSharedPreferences", MODE_PRIVATE);
final String password1 = settings.getString("User Password", "");
final String p1 = pass1.getText().toString();
if(password1.isEmpty()) {
//switch(v.getId()){
//case R.id.btnSubmit:
if (p1.toLowerCase().equals("password")) {
pass1.setText("");
messages.setText("Password cannot be \"password\"");
}
if (p1.length() < 3) {
messages.setText("Password must be at least 3 characters");
}
else {
settings.edit().putString("User Password", p1).apply();
Toast customToast = new Toast(getBaseContext());
customToast = Toast.makeText(getBaseContext(), "Password has been set!", Toast.LENGTH_SHORT);
customToast.setGravity(Gravity.CENTER|Gravity.CENTER, 0, 0);
customToast.show();
StartGallery();
}
}
else if(p1.equals(password1)) {
StartGallery();
}
else {
Toast customToast = new Toast(getBaseContext());
customToast = Toast.makeText(getBaseContext(), "Incorrect Password!", Toast.LENGTH_SHORT);
customToast.setGravity(Gravity.CENTER|Gravity.CENTER, 0, 0);
customToast.show(); {
}
}
}
public void StartGallery(){
Intent intent = new Intent(this, GalleryActivity.class);
startActivity(intent);
}
public boolean onCreateOptionsMenu(Menu menu){
super.onCreateOptionsMenu(menu);
MenuInflater Password = getMenuInflater();
Password.inflate(R.menu.prefs_menu, menu);
return true;
}
public boolean onOptionsItemSelected(MenuItem item){
switch(item.getItemId()){
case R.id.menuPrefs:
startActivity(new Intent("com.soboapps.todos.PASSPREFS"));
return true;
}
return false;
}
}