最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

Android實現(xiàn)記住用戶名和密碼功能

 更新時間:2016年05月26日 15:35:04   作者:GXSeveryday  
登陸界面創(chuàng)建一個復選按鈕,通過按鈕選取來進行事件處理。若按鈕選中記住賬號和密碼的信息,本文教大家如何使用Android實現(xiàn)記住用戶名和密碼功能,感興趣的小伙伴們可以參考一下

Android 實現(xiàn)記住用戶名和密碼的功能是通過SharedPreference 存儲來實現(xiàn)的。創(chuàng)建一個復選按鈕,通過按鈕的否選取來進行事件處理。若按鈕選中存儲賬號和密碼的信息。若按鈕沒有選中,則清空賬號和密碼的信息。

結果演示:

源代碼下載地址:

https://github.com/GXS1225/Android————-.git

分析

(1)判斷是否輸入了賬號和密碼

 if(name.trim().equals("")){
    Toast.makeText(this, "請您輸入用戶名!", Toast.LENGTH_SHORT).show();
    return;
   }
   if(pswd.trim().equals("")){
    Toast.makeText(this, "請您輸入密碼!", Toast.LENGTH_SHORT).show();
    return;
   }

(2)在layout_main.xml定義一個 CheckBox,進行事件處理

//通過
boolean CheckBoxLogin = checkbox.isChecked();
   //按鈕被選中,下次進入時會顯示賬號和密碼       
   if (CheckBoxLogin)
   {
     Editor editor = sp.edit();
     editor.putString("uname", name);
     editor.putString("upswd", pswd);
     editor.putBoolean("auto", true);
     editor.commit();
   }
   //按鈕被選中,清空賬號和密碼,下次進入時會顯示賬號和密碼    
   else   
   { 
    Editor editor = sp.edit();
    editor.putString("uname", null);
    editor.putString("upswd", null);
    editor.putBoolean("auto", false);
    editor.commit();
    }

(3) SharedPreference 的存儲實現(xiàn)

//先定義
SharedPreferences sp = null; 

sp = this.getSharedPreferences("userinfo", Context.MODE_PRIVATE);
//對uname 和 upswd 的操作
 if (sp.getBoolean("checkboxBoolean", false))
   {
    uname.setText(sp.getString("uname", null));
    upswd.setText(sp.getString("upswd", null)); 
    checkboxButton.setChecked(true);

   }

(4)跳轉到Content.java界面

//Intent跳轉
Intent intent = new Intent(Welcome.this,Content.class);
startActivity(intent);
finish();

步驟:

先寫一個登陸的界面: layout_main.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
 android:layout_width="match_parent"
 android:layout_height="match_parent"
 android:orientation="vertical" 
 android:background="#ADD8E6">

 <RelativeLayout
  android:id="@+id/login_div"
  android:layout_width="fill_parent"
  android:layout_height="221dp"
  android:layout_margin="15dip"
  android:background="@drawable/btn_bg"
  android:padding="15dip" >

  <TextView
   android:id="@+id/login_user_input"
   android:layout_width="wrap_content"
   android:layout_height="wrap_content"
   android:layout_alignParentTop="true"
   android:layout_margin="5dp"
   android:text="@string/user"
   android:textSize="16dp"
   android:typeface="sans" />

  <EditText
   android:id="@+id/user_input"
   android:layout_width="fill_parent"
   android:layout_height="wrap_content"
   android:layout_below="@id/login_user_input"
   android:background="@android:drawable/editbox_background"
   android:inputType="text"
   android:singleLine="true" />

  <TextView
   android:id="@+id/login_pass_input"
   android:layout_width="wrap_content"
   android:layout_height="wrap_content"
   android:layout_below="@id/user_input"
   android:layout_margin="5dp"
   android:text="@string/pass"
   android:textSize="16dp"
   android:typeface="sans" />

  <EditText
   android:id="@+id/pass_input"
   android:layout_width="fill_parent"
   android:layout_height="wrap_content"
   android:layout_below="@id/login_pass_input"
   android:background="@android:drawable/editbox_background"
   android:inputType="textPassword"
   android:singleLine="true" />

  <CheckBox
   android:id="@+id/checkBoxLogin"
   android:layout_width="wrap_content"
   android:layout_height="wrap_content"
   android:layout_alignLeft="@+id/pass_input"
   android:layout_alignParentBottom="true"
   android:text="@string/no_user" />

  <Button
   android:id="@+id/new_user"
   android:layout_width="wrap_content"
   android:layout_height="wrap_content"
   android:layout_alignParentBottom="true"
   android:layout_alignRight="@+id/pass_input"
   android:layout_marginRight="28dp"
   android:onClick="To_Title"
   android:text="@string/new_user" />

 </RelativeLayout>

  <LinearLayout
   android:layout_width="match_parent"
   android:layout_height="match_parent"
   android:orientation="vertical" >

   <TextView
    android:layout_width="402dp"
    android:layout_height="51dp"
    android:layout_marginLeft="50dp"
    android:background="@drawable/gxs_ziti" />

   <Button
    android:layout_width="120dp"
    android:layout_height="120dp"
    android:onClick="To_fruist"
    android:background="@drawable/gxs2" 
    android:layout_marginLeft="80dp"
    />



  </LinearLayout>

</LinearLayout>

Welcome.java

package com.gxs.login;
import com.example.login.R;
import com.gxs.listview.*;
import android.os.Bundle;
import android.preference.Preference;
import android.app.Activity;
import android.app.SearchManager.OnCancelListener;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.util.Log;
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 Welcome extends Activity implements OnClickListener{
 private EditText uname = null;
 private EditText upswd = null;
 private CheckBox checkboxButton = null;
 private Button login = null;
 SharedPreferences sp = null; 
 @Override
 protected void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  setContentView(R.layout.layout_main);
  sp = this.getSharedPreferences("userinfo", Context.MODE_PRIVATE);
  init();

 }

 public void init()
 {
  uname = (EditText) findViewById(R.id.user_input);
  upswd = (EditText) findViewById(R.id.pass_input);
  checkboxButton = (CheckBox) findViewById(R.id.checkBoxLogin);
  login = (Button) findViewById(R.id.new_user);
  if (sp.getBoolean("checkboxBoolean", false))
   {
    uname.setText(sp.getString("uname", null));
    upswd.setText(sp.getString("upswd", null)); 
    checkboxButton.setChecked(true);

   }
  login.setOnClickListener(this);
 }

 @Override
 public void onClick(View v) {
  if (v == login){
   String name = uname.getText().toString();
   String pswd = upswd.getText().toString();
   if(name.trim().equals("")){
    Toast.makeText(this, 
    "請您輸入用戶名!", Toast.LENGTH_SHORT).show();
    return;
   }
   if(pswd.trim().equals("")){
    Toast.makeText(this, 
    "請您輸入密碼!", Toast.LENGTH_SHORT).show();
    return;
   }
   boolean CheckBoxLogin = checkboxButton.isChecked();
   if (CheckBoxLogin)
   {
     Editor editor = sp.edit();
     editor.putString("uname", name);
     editor.putString("upswd", pswd);
     editor.putBoolean("checkboxBoolean", true);
     editor.commit();
   }
   else
   { 
    Editor editor = sp.edit();
    editor.putString("uname", null);
    editor.putString("upswd", null);
    editor.putBoolean("checkboxBoolean", false);
    editor.commit();
    }
   //Intent跳轉
   Intent intent=new Intent(Welcome.this,Content.class);
   startActivity(intent);
   finish();
  }
 }

}

Content.java

package com.gxs.listview;
import java.util.List;
import com.example.login.R;
import com.gxs.*;
import android.app.Activity;
import android.os.Bundle;
import android.widget.ArrayAdapter;
import android.widget.ListView;
public class Content extends Activity{
 private ListView listview_fruits; 
 @Override
 protected void onCreate(Bundle savedInstanceState) {
  // TODO Auto-generated method stub
  super.onCreate(savedInstanceState);
  setContentView(R.layout.content);

 }
}

content.xml

<LinearLayout 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=".Welcome" 
 >

 <TextView
  android:id="@+id/textView1"
  android:layout_width="wrap_content"
  android:layout_height="wrap_content"
  android:text="內容"
  android:textAppearance="?android:attr/textAppearanceLarge" />

</LinearLayout>

更多內容請參考專題:Android密碼使用教程

以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。

相關文章

  • Android實現(xiàn)菜單關聯(lián)activity的方法示例

    Android實現(xiàn)菜單關聯(lián)activity的方法示例

    這篇文章主要介紹了Android實現(xiàn)菜單關聯(lián)activity的方法,涉及Android使用Intent實現(xiàn)菜單關聯(lián)activity相關操作技巧,需要的朋友可以參考下
    2019-03-03
  • 基于Flutter實現(xiàn)轉場動效的示例代碼

    基于Flutter實現(xiàn)轉場動效的示例代碼

    動畫經常會用于場景切換,比如滑動,縮放,尺寸變化。Flutter?提供了Transition系列的動畫組件,可以讓場景轉換動畫變得更加簡單。本文整理了常用的Transition組件的應用,需要的可以參考一下
    2022-05-05
  • 詳解android特性之CoordinatorLayout用法探析實例

    詳解android特性之CoordinatorLayout用法探析實例

    本篇文章主要介紹了android特性之CoordinatorLayout用法探析實例,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2018-02-02
  • 深入淺析Android Fragment(下篇)

    深入淺析Android Fragment(下篇)

    本篇文章給大家介紹如何管理Fragment回退棧,F(xiàn)ragment如何與Activity交互,F(xiàn)ragment與Activity交互的最佳實踐,沒有視圖的Fragment的用處,使用Fragment創(chuàng)建對話框,如何與ActionBar,MenuItem集成,對Android Fragment感興趣的朋友可以參考下本篇文章
    2015-10-10
  • Android中利用SurfaceView制作抽獎轉盤的全流程攻略

    Android中利用SurfaceView制作抽獎轉盤的全流程攻略

    這篇文章主要介紹了Android中利用SurfaceView制作抽獎轉盤的全流程,從圖案的繪制到轉盤的控制再到布局,真的非常全面,需要的朋友可以參考下
    2016-04-04
  • Android報錯Didn‘t?find?class?“android.view.x“問題解決原理剖析

    Android報錯Didn‘t?find?class?“android.view.x“問題解決原理剖析

    這篇文章主要為大家介紹了Android報錯Didn‘t?find?class?“android.view.x“問題解決及原理剖析,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪
    2023-03-03
  • Android onClick按鈕單擊事件的四種常用寫法

    Android onClick按鈕單擊事件的四種常用寫法

    本文主要介紹了Android onClick按鈕單擊事件的四種常用寫法,具有很好的參考價值。下面跟著小編一起來看下吧
    2017-03-03
  • Android Kotlin 基本數據類型詳解

    Android Kotlin 基本數據類型詳解

    Kotlin是一種靜態(tài)類型語言,適用于Android開發(fā),Kotlin的基本數據類型包括數值類型、字符類型、布爾類型和數組類型,本文介紹Android Kotlin 基本數據類型,感興趣的朋友一起看看吧
    2025-03-03
  • Android開發(fā)常見問題總結

    Android開發(fā)常見問題總結

    這篇文章主要介紹了Android開發(fā)常見問題,總結分析了諸如界面設計、多媒體調用、圖片、動畫操作等開發(fā)中常見的問題解決方法與相關注意事項,需要的朋友可以參考下
    2016-08-08
  • Android開發(fā)之Activity詳解

    Android開發(fā)之Activity詳解

    本文是翻譯的官方文檔的內容,看起來可能會有些生硬,但是內容很有用,給大家一個參考,希望對大家學習有所幫助。
    2016-06-06

最新評論

灵寿县| 潮州市| 宜州市| 公安县| 西和县| 仪征市| 宁安市| 衢州市| 陆河县| 白朗县| 吉木乃县| 龙川县| 中牟县| 台山市| 南溪县| 三台县| 益阳市| 宁安市| 民丰县| 德保县| 葫芦岛市| 普兰店市| 文安县| 江津市| 大石桥市| 长顺县| 晋中市| 永兴县| 四平市| 独山县| 台东县| 林州市| 出国| 安吉县| 刚察县| 光泽县| 彝良县| 鲜城| 辽中县| 开鲁县| 张北县|