Android實現(xiàn)記住密碼功能
本文實例為大家分享了Android實現(xiàn)記住密碼功能的具體代碼,供大家參考,具體內容如下
LoginActivity.java
package com.wangdeqiang.www.chatwithrobot.BroadcastBestPractice;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.preference.PreferenceManager;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.Toast;
import com.wangdeqiang.www.chatwithrobot.R;
import static com.wangdeqiang.www.chatwithrobot.R.id.account;
/**
* @author
*/
public class LoginActivity extends BaseActivity {
private SharedPreferences pref;
private SharedPreferences.Editor editor;
private EditText accountEdit;
private EditText passwordEdit;
private Button login;
private CheckBox rememberPass;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_login);
pref = PreferenceManager.getDefaultSharedPreferences(this);
accountEdit = (EditText) findViewById(account);
passwordEdit = (EditText) findViewById(R.id.password);
rememberPass = (CheckBox) findViewById(R.id.remember_pass);
login = (Button) findViewById(R.id.login);
boolean isRemeber = pref.getBoolean("remember_password",false);
if(isRemeber) {
//將賬號和密碼都設置到文本框中
String account = pref.getString("account","");
String password = pref.getString("password","");
accountEdit.setText(account);
passwordEdit.setText(password);
rememberPass.setChecked(true);
}
login.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String account = pref.getString("account", "").toString();
String password = pref.getString("password", "").toString();
//如果賬號和密碼是admin和123456,就認為登陸成功
if (account.equals("admin") && password.equals("123456")) {
editor = pref.edit();
if (rememberPass.isChecked()) {
editor.putBoolean("remember", false);
editor.putString("account", account);
editor.putString("password", password);
} else {
editor.apply();
}
editor.commit();
Intent intent = new Intent(LoginActivity.this, Main3Activity.class);
startActivity(intent);
finish();
} else {
Toast.makeText(LoginActivity.this, "密碼或者賬號錯誤", Toast.LENGTH_SHORT).show();
}
}
});
}
}
activity_login.xml
<?xml version="1.0" encoding="utf-8"?>
<TableLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:stretchColumns="1"
>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="60dp"
android:orientation="horizontal">
<TextView
android:layout_width="90dp"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:text="Account:"
android:textSize="18sp" />
<EditText
android:id="@+id/account"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:layout_weight="1" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="60dp"
android:orientation="horizontal">
<TextView
android:layout_width="90dp"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:text="Password"
android:textSize="18sp" />
<EditText
android:id="@+id/password"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:layout_weight="1"
android:inputType="textPassword" />
</LinearLayout>
</LinearLayout>
<TableRow>
<CheckBox
android:id="@+id/remember_pass"
android:layout_height="wrap_content"
/>
<TextView
android:layout_height="wrap_content"
android:text="Remeber password"
/>
</TableRow>
<Button
android:id="@+id/login"
android:layout_height="wrap_content"
android:layout_span="2"
android:text="Login"
/>
</TableLayout>
以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。
- Android SharedPreferences實現(xiàn)記住密碼和自動登錄
- Android開發(fā)筆記SQLite優(yōu)化記住密碼功能
- Android實現(xiàn)用戶登錄記住密碼功能
- Android sharedPreferences實現(xiàn)記住密碼功能
- Android 使用SharedPreferrences儲存密碼登錄界面記住密碼功能
- Android實現(xiàn)登錄界面記住密碼的存儲
- Android SharedPreferences實現(xiàn)記住密碼和自動登錄界面
- Android實現(xiàn)帶有記住密碼功能的登陸界面
- Android通過記住密碼功能學習數(shù)據(jù)存儲類SharedPreferences詳解及實例
- Android實現(xiàn)登陸界面的記住密碼功能
相關文章
Android中協(xié)調滾動布局的實現(xiàn)代碼
這篇文章主要介紹了Android中協(xié)調滾動常用的布局實現(xiàn),類似這樣的協(xié)調滾動布局,當?shù)撞苛斜砘瑒拥臅r候,頂部的布局做響應的動作,我們都可以通過?AppBarLayout?和?MotionLayout?來實現(xiàn),本文通過實例代碼介紹的非常詳細,需要的朋友參考下吧2022-06-06
Android 自定義ProgressDialog進度條對話框用法詳解
ProgressDialog為進度對話框。android手機自帶的對話框顯得比較單一,我們可以通過ProgressDialog來自己定義對話框中將要顯示出什么東西2016-01-01
Android利用CircleImageView實現(xiàn)圓形頭像的方法
這篇文章主要介紹了Android利用CircleImageView實現(xiàn)圓形頭像的方法,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下2016-10-10
ContentProvider客戶端處理provider邏輯分析
這篇文章主要為大家介紹了ContentProvider客戶端處理provider邏輯分析,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪2022-10-10
Android開發(fā)方式之Java+html+javascript混合開發(fā)
這篇文章主要為大家詳細介紹了Android開發(fā)方式的其中一種Java+html+javascript混合開發(fā),感興趣的小伙伴們可以參考一下2016-06-06
Android SQLite事務處理結合Listview列表顯示功能示例
這篇文章主要介紹了Android SQLite事務處理結合Listview列表顯示功能,較為詳細的分析了Android使用sqlite數(shù)據(jù)庫進行事務操作并結合Listview進行列表顯示的相關操作技巧,需要的朋友可以參考下2017-07-07
android 使用OkHttp上傳多張圖片的實現(xiàn)代碼
這篇文章主要介紹了android 使用OkHttp上傳多張圖片的相關資料,本文通過實例代碼給大家介紹的非常詳細,具有一定的參考借鑒價值,需要的朋友可以參考下2018-07-07
Android View 完美實現(xiàn)EditText 在軟鍵盤上邊的示例
本篇文章主要介紹了Android View 完美實現(xiàn)EditText 在軟鍵盤上邊的示例,具有一定的參考價值,有興趣的可以了解一下2017-08-08

