這篇文章給大家介紹怎么在Android 中使用SharedPreferences實現(xiàn)保存登錄數(shù)據(jù)功能,內(nèi)容非常詳細,感興趣的小伙伴們可以參考借鑒,希望對大家能有所幫助。
創(chuàng)新互聯(lián)公司是一家專業(yè)提供曲麻萊企業(yè)網(wǎng)站建設,專注與成都網(wǎng)站建設、做網(wǎng)站、H5場景定制、小程序制作等業(yè)務。10年已為曲麻萊眾多企業(yè)、政府機構等服務。創(chuàng)新互聯(lián)專業(yè)網(wǎng)絡公司優(yōu)惠進行中。
1.activity_main.xml頁面存放所有的控件,我在每一行都使用了線性布局。
activity_main.xml頁面:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:layout_marginLeft="20dp" android:layout_marginRight="20dp" tools:context=".SecondActivity" > <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_marginTop="20dp" android:orientation="horizontal" > <TextView android:id="@+id/tvName" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="用戶名:" /> <EditText android:id="@+id/etInputName" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_weight="2" /> </LinearLayout> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_marginTop="60dp" android:orientation="horizontal" > <TextView android:id="@+id/tvPass" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="密 碼:" /> <EditText android:id="@+id/etInputPass" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_weight="2" /> </LinearLayout> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_marginTop="100dp" android:orientation="horizontal" > <CheckBox android:id="@+id/cbSave" android:layout_width="wrap_content" android:layout_height="wrap_content" android:checked="false" android:text="保存用戶名" /> </LinearLayout> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_marginTop="130dp" android:orientation="horizontal" > <Button android:id="@+id/btLogin" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_weight="1" android:text="登錄" /> <Button android:id="@+id/btCancel" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_weight="1" android:text="取消" /> </LinearLayout> </RelativeLayout>
2.MainActivity.java頁面處理登錄和保存數(shù)據(jù)。
MainActivity.java頁面:
package com.example.sharedpreferences;
import android.os.Bundle;
import android.app.Activity;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.view.Menu;
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 MainActivity extends Activity implements OnClickListener{
SharedPreferences pref;
Editor editor;
private EditText etInputName,etInputPass;
private CheckBox ckSave;
private Button btLogin,btCancel;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
/**
* 獲取控件id
*/
getId();
/**
* 保存數(shù)據(jù)
*/
saveData();
/**
* 綁定點擊事件
*/
bindClick();
}
/**
* 獲取控件id
*/
private void getId() {
etInputName=(EditText) findViewById(R.id.etInputName);
etInputPass=(EditText) findViewById(R.id.etInputPass);
ckSave=(CheckBox) findViewById(R.id.cbSave);
btLogin=(Button) findViewById(R.id.btLogin);
btCancel=(Button) findViewById(R.id.btCancel);
}
/**
* 保存數(shù)據(jù)
*/
private void saveData() {
//獲得SharedPreferences對象
pref=getSharedPreferences("userInfo",MODE_PRIVATE);//將內(nèi)容存放到名為userInfo的文檔內(nèi)
//獲得SharedPreferences.Editor對象
editor=pref.edit();
String name=pref.getString("userName","");//獲取用戶名
if(name.equals("")){//如果name為空,代表未選擇保存用戶名
ckSave.setChecked(false);//不勾選
}else{
ckSave.setChecked(true);
etInputName.setText(name);//將讀取到的name值賦值到EditText中
}
}
/**
* 綁定點擊事件
*/
private void bindClick() {
btLogin.setOnClickListener(this);
btCancel.setOnClickListener(this);
}
/**
* 按鈕點擊事件
*/
@Override
public void onClick(View view) {
switch (view.getId()) {
case R.id.btLogin:
String name=etInputName.getText().toString().trim();//獲取輸入的名字并去掉空格
String pass=etInputPass.getText().toString().trim();//獲取輸入的密碼并去掉空格
if("admin".equals(name)&&"123456".equals(pass)){
if(ckSave.isChecked()){//如果選擇保存用戶名
editor.putString("userName",name);
editor.commit();//提交數(shù)據(jù)
}else{//如果未選擇保存用戶名
editor.remove("userName");//刪除用戶名
editor.commit();//提交數(shù)據(jù)(每次更改都需要提交)
}
Toast.makeText(SecondActivity.this,"登錄成功",Toast.LENGTH_SHORT).show();
}else{
Toast.makeText(SecondActivity.this,"用戶名或密碼不正確",Toast.LENGTH_SHORT).show();
}
break;
case R.id.btCancel:
break;
}
}
}3.保存的文件目錄可以查看的到,點擊右上角進入,找到data->data->當前目錄的包名->shared-prefs->新建的文件名

4.另外,點擊右上角導出可以暫時保存到桌面,然后選擇打開方式可以查看里邊信息。
5.還有一點是,當程序在真機上運行時,file explore打不開data文件夾,根據(jù)網(wǎng)上的經(jīng)驗,真機先root,然后在手機上裝上R.E 管理器(或類似軟件),將/data/data的權限修改為可讀可寫可執(zhí)行,然后,就可以在eclipse中展開了。

6.SharedPreferences多用于配置信息或者內(nèi)容較少的數(shù)據(jù)的保存,當數(shù)據(jù)量復雜或者較大,還是需要使用數(shù)據(jù)庫。
關于怎么在Android 中使用SharedPreferences實現(xiàn)保存登錄數(shù)據(jù)功能就分享到這里了,希望以上內(nèi)容可以對大家有一定的幫助,可以學到更多知識。如果覺得文章不錯,可以把它分享出去讓更多的人看到。
網(wǎng)頁標題:怎么在Android中使用SharedPreferences實現(xiàn)保存登錄數(shù)據(jù)功能
當前URL:http://chinadenli.net/article28/iigijp.html
成都網(wǎng)站建設公司_創(chuàng)新互聯(lián),為您提供微信小程序、網(wǎng)站建設、App設計、網(wǎng)站營銷、外貿(mào)網(wǎng)站建設、網(wǎng)站收錄
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)