在android应用程序中记住密码
我已经开发了一个登录表单用户名、密码和提交按钮,在我的android应用程序中通过soap webservices使用MySQL连接。 我在活动的XML资源中编写了以下代码:在android应用程序中记住密码,android,Android,我已经开发了一个登录表单用户名、密码和提交按钮,在我的android应用程序中通过soap webservices使用MySQL连接。 我在活动的XML资源中编写了以下代码: <?xml version="1.0" encoding="utf-8"?> <AbsoluteLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" and
<?xml version="1.0" encoding="utf-8"?>
<AbsoluteLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<EditText
android:id="@+id/tf_userName"
android:layout_width="194dp"
android:layout_height="wrap_content"
android:layout_x="106dp"
android:layout_y="80dp" >
<requestFocus />
</EditText>
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_x="23dp"
android:layout_y="93dp"
android:text="User name" />
<EditText
android:id="@+id/tf_password"
android:layout_width="189dp"
android:layout_height="wrap_content"
android:layout_x="108dp"
android:layout_y="161dp"
android:inputType="textPassword" />
<TextView
android:id="@+id/TextView01"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_x="23dp"
android:layout_y="176dp"
android:text="Password" />
<CheckBox android:layout_below="@+id/passwordview"
android:layout_width="wrap_content" android:text="Remember Password!"
android:layout_height="wrap_content" android:id="@+id/rempasswordcheckbox"
android:layout_x="23dp"
android:layout_y="200dp" />
<Button
android:id="@+id/btn_login"
android:layout_width="106dp"
android:layout_height="wrap_content"
android:layout_x="50dp"
android:layout_y="273dp"
android:text="Login" />
<TextView
android:id="@+id/TextView03"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_x="50dp"
android:layout_y="350dp"
android:text="Forget My password ?: Click here" />
<Button
android:id="@+id/btn_reg"
android:layout_width="106dp"
android:layout_height="wrap_content"
android:layout_x="175dp"
android:layout_y="273dp"
android:text="Register" />
<Button
android:id="@+id/btn_logout"
android:layout_width="106dp"
android:layout_height="wrap_content"
android:layout_x="215dp"
android:layout_y="25dp"
android:text="Logout" />
<TextView
android:id="@+id/tv_status"
android:layout_width="151dp"
android:layout_height="38dp"
android:layout_x="26dp"
android:layout_y="234dp"
android:textAppearance="?android:attr/textAppearanceMedium" />
但是如何创建Java代码呢?我似乎做不到这一点。请指导我。如果在登录时选中该复选框 将密码存储在 每当启动登录活动时,在运行时检索
如果在登录时选中该复选框 将密码存储在 每当启动登录活动时,在运行时检索 编辑-------
import....
import android.widget.CheckBox; // <--- Add
import android.content.SharedPreferences; // <--- Add
import android.widget.EditText; // <--- Add
.
.
.
.
private static final String SPF_NAME = "vidslogin"; // <--- Add this
private static final String USERNAME = "username"; // <--- To save username
private static final String PASSWORD = "password"; // <--- To save password
CheckBox chkRememberMe; // <--- You even not taken CheckBox, SILLY WORKER
EditText etUserName, etPassword; // <--- You even not taken CheckBox, SILLY WORKER
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.login);
chkRememberMe = (CheckBox) findViewById(R.id.rempasswordcheckbox); // <--- Instantiate CheckBox...
etUserName = (EditText) findViewById(R.id.tf_userName); // <--- Instantiate EditText...
etPassword = (EditText) findViewById(R.id.tf_password); // <--- Instantiate EditText...
// ADD THIS TO READ SAVED username & password NEXT-TIME OPENING Application
SharedPreferences loginPreferences = getSharedPreferences(SPF_NAME,
Context.MODE_PRIVATE);
etUserName.setText(loginPreferences.getString(USERNAME, ""));
etPassword.setText(loginPreferences.getString(PASSWORD, ""));
.
.
.
.
.
.
private void loginAction() {
.
.
.
.
if (status.equals("Success!"))
{
// ADD to save and read next time
String strUserName = etUserName.getText().toString().trim();
String strPassword = etPassword.getText().toString().trim();
if (null == strUserName || strUserName.length() == 0)
{
// showToast("Enter Your Name");
etUserName.requestFocus();
} else if (null == strPassword || strPassword.length() == 0)
{
// showToast("Enter Your Password");
etPassword.requestFocus();
} else
{
if (chkRememberMe.isChecked())
{
SharedPreferences loginPreferences = getSharedPreferences(SPF_NAME, Context.MODE_PRIVATE);
loginPreferences.edit().putString(USERNAME, strUserName).putString(PASSWORD, strPassword).commit();
} else
{
SharedPreferences loginPreferences = getSharedPreferences(SPF_NAME, Context.MODE_PRIVATE);
loginPreferences.edit().clear().commit();
}
// OVER
Intent intent = new Intent(Login.this, HomePage.class);
intent.putExtra("username", userName.getText().toString());
startActivity(intent);
}
编辑-------
import....
import android.widget.CheckBox; // <--- Add
import android.content.SharedPreferences; // <--- Add
import android.widget.EditText; // <--- Add
.
.
.
.
private static final String SPF_NAME = "vidslogin"; // <--- Add this
private static final String USERNAME = "username"; // <--- To save username
private static final String PASSWORD = "password"; // <--- To save password
CheckBox chkRememberMe; // <--- You even not taken CheckBox, SILLY WORKER
EditText etUserName, etPassword; // <--- You even not taken CheckBox, SILLY WORKER
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.login);
chkRememberMe = (CheckBox) findViewById(R.id.rempasswordcheckbox); // <--- Instantiate CheckBox...
etUserName = (EditText) findViewById(R.id.tf_userName); // <--- Instantiate EditText...
etPassword = (EditText) findViewById(R.id.tf_password); // <--- Instantiate EditText...
// ADD THIS TO READ SAVED username & password NEXT-TIME OPENING Application
SharedPreferences loginPreferences = getSharedPreferences(SPF_NAME,
Context.MODE_PRIVATE);
etUserName.setText(loginPreferences.getString(USERNAME, ""));
etPassword.setText(loginPreferences.getString(PASSWORD, ""));
.
.
.
.
.
.
private void loginAction() {
.
.
.
.
if (status.equals("Success!"))
{
// ADD to save and read next time
String strUserName = etUserName.getText().toString().trim();
String strPassword = etPassword.getText().toString().trim();
if (null == strUserName || strUserName.length() == 0)
{
// showToast("Enter Your Name");
etUserName.requestFocus();
} else if (null == strPassword || strPassword.length() == 0)
{
// showToast("Enter Your Password");
etPassword.requestFocus();
} else
{
if (chkRememberMe.isChecked())
{
SharedPreferences loginPreferences = getSharedPreferences(SPF_NAME, Context.MODE_PRIVATE);
loginPreferences.edit().putString(USERNAME, strUserName).putString(PASSWORD, strPassword).commit();
} else
{
SharedPreferences loginPreferences = getSharedPreferences(SPF_NAME, Context.MODE_PRIVATE);
loginPreferences.edit().clear().commit();
}
// OVER
Intent intent = new Intent(Login.this, HomePage.class);
intent.putExtra("username", userName.getText().toString());
startActivity(intent);
}
使用是最简单的方法
SharedPreferences settings = getSharedPreferences(PREFS_NAME, 0);
SharedPreferences.Editor editor = settings.edit();
editor.putString(PREFERENCE_IDENTIFIER, usersPassword);
editor.commit();
状况
if (rememberPasswordCheckbox.isChecked()) {
// save password here
}
使用是最简单的方法
SharedPreferences settings = getSharedPreferences(PREFS_NAME, 0);
SharedPreferences.Editor editor = settings.edit();
editor.putString(PREFERENCE_IDENTIFIER, usersPassword);
editor.commit();
状况
if (rememberPasswordCheckbox.isChecked()) {
// save password here
}
您好,现在请阅读我的问题。告诉我如何在我的编码部分中选中MemberPassword。记住的密码表示如果我输入用户名和密码,然后启用记住密码复选框表示它成功登录。然后我单击注销表示它是loggedout。但我再次打开d应用程序表示用户名和密码显示在d上edittext知道…但当我再次打开d应用程序时,当我记录到dis应用程序是loggedout之后,我打开d应用程序意味着用户名和密码没有保存。实际上它保存了。但在启动应用程序时,您没有读取SharedReferences SPF_名称,也没有在用户名和密码的编辑文本中填充值。嘿,您没有做任何研究/设计。您甚至没有在onCreate中获取EditText和CheckBox对象。您也没有搜索过SharedReferences,即使不是我的第一个答案,请看这里它显示了如何从/到SharedReferences读取和写入值。没有简单的逻辑和研究能力,你怎么能工作?!!!您好,现在请阅读我的问题。告诉我如何在我的编码部分中选中MemberPassword。记住的密码表示如果我输入用户名和密码,然后启用记住密码复选框表示它成功登录。然后我单击注销表示它是loggedout。但我再次打开d应用程序表示用户名和密码显示在d上edittext知道…但当我再次打开d应用程序时,当我记录到dis应用程序是loggedout之后,我打开d应用程序意味着用户名和密码没有保存。实际上它保存了。但在启动应用程序时,您没有读取SharedReferences SPF_名称,也没有在用户名和密码的编辑文本中填充值。嘿,您没有做任何研究/设计。您甚至没有在onCreate中获取EditText和CheckBox对象。您也没有搜索过SharedReferences,即使不是我的第一个答案,请看这里它显示了如何从/到SharedReferences读取和写入值。没有简单的逻辑和研究能力,你怎么能工作?!!!我想你想在点击登录按钮时保存密码???s我需要dis one…我怎么做我想你想在点击登录按钮时保存密码???s我需要dis one…我怎么做