Andriod Studio實(shí)現(xiàn)保存QQ密碼功能(案例代碼詳解)
對(duì)于QQ登錄時(shí)保存賬號(hào)和密碼的功能,不僅文件存儲(chǔ)能夠?qū)崿F(xiàn),SharePreferences同樣也可以實(shí)現(xiàn),而且SharedPreferences存取數(shù)據(jù)更加簡(jiǎn)單方便。因此可以用該方法實(shí)現(xiàn)保存Q密碼的案例,具體步驟如下:
創(chuàng)建布局類
<?xml version="1.0" encoding="utf-8"?> <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:paddingBottom="@dimen/activity_vertical_margin" android:paddingLeft="@dimen/activity_horizontal_margin" android:paddingRight="@dimen/activity_horizontal_margin" android:paddingTop="@dimen/activity_vertical_margin" tools:context="com.example.kh11.MainActivity"> <ImageView android:id="@+id/iv" android:layout_width="70dp" android:layout_height="70dp" android:layout_centerHorizontal="true" android:layout_marginTop="40dp" android:background="@drawable/touxiang"/> <LinearLayout android:id="@+id/ll_number" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_below="@+id/iv" android:layout_centerVertical="true" android:layout_marginBottom="5dp" android:layout_marginLeft="10dp" android:layout_marginRight="10dp" android:layout_marginTop="15dp" android:background="#ffffff"> <TextView android:id="@+id/tv_number" android:layout_width="wrap_content" android:layout_height="wrap_content" android:padding="10dp" android:text="賬號(hào):" android:textColor="#000" android:textSize="20sp"/> <EditText android:id="@+id/et_number" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_marginLeft="5dp" android:background="@null" android:padding="10dp"/> </LinearLayout> <LinearLayout android:id="@+id/ll_password" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_below="@+id/ll_number" android:layout_centerVertical="true" android:layout_marginLeft="10dp" android:layout_marginRight="10dp" android:background="#ffffff"> <TextView android:id="@+id/tv_password" android:layout_width="wrap_content" android:layout_height="wrap_content" android:padding="10dp" android:text="密碼:" android:textColor="#000" android:textSize="20sp"/> <EditText android:id="@+id/et_password" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_marginLeft="5dp" android:background="@null" android:inputType="textPassword" android:padding="10dp"/> </LinearLayout> <Button android:id="@+id/btn_login" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_below="@+id/ll_password" android:layout_marginLeft="10dp" android:layout_marginRight="10dp" android:layout_marginTop="50dp" android:background="#3C8DC4" android:text="登錄" android:textColor="#ffffff" android:textSize="20sp"/> </RelativeLayout>
創(chuàng)建工具類
package cn.itcast.saveqq; import android.content.Context; import android.content.SharedPreferences; import java.util.HashMap; import java.util.Map; public class SPSaveQQ { public static boolean saveUserInfo(Context context,String number,String password){ SharedPreferences sp = context.getSharedPreferences("data",Context.MODE_PRIVATE); SharedPreferences.Editor edit = sp.edit(); edit.putString("userName",number); edit.putString("pwd", password); edit.commit(); return true; } public static Map<String ,String> getUserInfo(Context context){ SharedPreferences sp = context.getSharedPreferences("data",Context.MODE_PRIVATE); String number = sp.getString("userName", null); String password = sp.getString("pwd", null); Map<String ,String > userMap = new HashMap<String,String>(); userMap.put("number",number); userMap.put("password",password); return userMap; } }
編寫(xiě)界面交互代碼
package com.example.kh11; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.text.TextUtils; import android.view.View; import android.widget.Button; import android.widget.EditText; import android.widget.Toast; import java.util.Map; import cn.itcast.saveqq.SPSaveQQ; public class MainActivity extends AppCompatActivity implements View.OnClickListener{ private EditText etNumber; private EditText etPassword; private Button btnLogin; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); //初始化界面 initView(); Map<String ,String > userInfo = SPSaveQQ.getUserInfo(this); if(userInfo != null){ etNumber.setText(userInfo.get("number")); etPassword.setText(userInfo.get("password")); } } private void initView(){ etNumber = (EditText) findViewById(R.id.et_number); etPassword = (EditText) findViewById(R.id.et_password); btnLogin = (Button) findViewById(R.id.btn_login); //設(shè)置按鈕的點(diǎn)擊事件 btnLogin.setOnClickListener(this); } @Override public void onClick(View v) { //當(dāng)單機(jī)登錄按鈕時(shí),獲取QQ賬號(hào)和密碼 String number = etNumber.getText().toString().trim(); String password = etPassword.getText().toString(); //檢驗(yàn)賬號(hào)和密碼是否正確 if(TextUtils.isEmpty(number)){ Toast.makeText(this,"請(qǐng)輸入QQ賬號(hào)",Toast.LENGTH_SHORT).show(); return; } if(TextUtils.isEmpty(password)){ Toast.makeText(this,"請(qǐng)輸入密碼",Toast.LENGTH_SHORT).show(); return; } //登陸成功 Toast.makeText(this,"登陸成功",Toast.LENGTH_SHORT).show(); //保存用戶信息 boolean isSaveSuccess = SPSaveQQ.saveUserInfo(this,number,password); if(isSaveSuccess){ Toast.makeText(this,"保存成功",Toast.LENGTH_SHORT).show(); }else{ Toast.makeText(this,"保存失敗",Toast.LENGTH_SHORT).show(); } } }
運(yùn)行程序
程序運(yùn)行成功后,在界面輸入賬號(hào)和密碼,單擊登錄按鈕,會(huì)彈出“登陸成功”和“保存成功”字樣,數(shù)據(jù)信息會(huì)保存在SharedPreferences中,可以在data.xml文件中查看保存的數(shù)據(jù)信息。
運(yùn)行結(jié)果如圖:
(這個(gè)上傳的圖片怎么改尺寸,真的太丑了。。。)
總結(jié)
到此這篇關(guān)于Andriod Studio實(shí)現(xiàn)保存QQ密碼功能的文章就介紹到這了,更多相關(guān)android studio 保存qq 密碼內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Android studio 下JNI編程實(shí)例并生成so庫(kù)的實(shí)現(xiàn)代碼
這篇文章主要介紹了Android studio 下JNI編程實(shí)例并生成so庫(kù),需要的朋友可以參考下2017-09-09android全屏去掉title欄的多種實(shí)現(xiàn)方法
android全屏去掉title欄包括以下幾個(gè)部分:實(shí)現(xiàn)應(yīng)用中的所有activity都全屏/實(shí)現(xiàn)單個(gè)activity全屏/實(shí)現(xiàn)單個(gè)activity去掉title欄/自定義標(biāo)題內(nèi)容/自定義標(biāo)題布局等等感興趣的可參考下啊2013-02-02Android實(shí)現(xiàn)多媒體之播放音樂(lè)
這篇文章主要為大家詳細(xì)介紹了Android實(shí)現(xiàn)多媒體之播放音樂(lè)的相關(guān)資料,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-02-02android中px和dp,px和sp之間的轉(zhuǎn)換方法
在Android開(kāi)發(fā)中dp和px,sp和px之間的轉(zhuǎn)換時(shí)必不可少的。下面腳本之家小編給大家?guī)?lái)了android中px和dp,px和sp之間的轉(zhuǎn)換方法,感興趣的朋友一起看看吧2018-06-06Android實(shí)現(xiàn)登錄注冊(cè)功能封裝
Android應(yīng)用軟件基本上都會(huì)用到登錄注冊(cè)功能,本篇文章主要介紹了Android登錄注冊(cè)功能封裝功能實(shí)現(xiàn),具有一定的參考價(jià)值,有興趣的可以了解一下。2017-01-01Android 中對(duì)JSON數(shù)據(jù)解析實(shí)例代碼
這篇文章主要介紹了Android 中對(duì)JSON數(shù)據(jù)解析實(shí)例代碼的相關(guān)資料,需要的朋友可以參考下2017-03-03Android中g(shù)ravity、layout_gravity、padding、margin的區(qū)別小結(jié)
這篇文章主要介紹了Android中g(shù)ravity、layout_gravity、padding、margin的區(qū)別小結(jié),需要的朋友可以參考下2014-08-08Android監(jiān)聽(tīng)ScrollView滑動(dòng)距離的簡(jiǎn)單處理
這篇文章主要為大家詳細(xì)介紹了Android監(jiān)聽(tīng)ScrollView滑動(dòng)距離的簡(jiǎn)單處理,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2022-02-02Android.mk文件中添加第三方j(luò)ar文件的方法
這篇文章主要介紹了Android.mk文件中添加第三方j(luò)ar文件及引用第三方j(luò)ar包的方法,需要的朋友可以參考下2018-01-01Android系統(tǒng)添加Linux驅(qū)動(dòng)
今天小編就為大家分享一篇關(guān)于Android系統(tǒng)添加Linux驅(qū)動(dòng)的文章,小編覺(jué)得內(nèi)容挺不錯(cuò)的,現(xiàn)在分享給大家,具有很好的參考價(jià)值,需要的朋友一起跟隨小編來(lái)看看吧2018-10-10