安卓案例2:簡單登錄界面和保存信息

来源:https://www.cnblogs.com/xuyiqing/archive/2018/04/16/8859005.html
-Advertisement-
Play Games

界面效果: 佈局代碼: MainActivity: 讀寫文件工具類: ...


界面效果:

 

佈局代碼:

<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:orientation="vertical"
    tools:context=".MainActivity" >

    <EditText
        android:id="@+id/et_username"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:hint="請輸入用戶名" />

    <EditText
        android:id="@+id/et_userpassword"
        android:password="true"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:hint="請輸入密碼" />

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_marginTop="20dp" >

        <CheckBox
            android:id="@+id/cb_ischeck"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="記住用戶名密碼" />

        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentRight="true"
            android:onClick="login"
            android:text="登錄" />
    </RelativeLayout>

</LinearLayout>

 

 

MainActivity:

package com.dreamtech.login;

import java.util.Map;

import android.os.Bundle;
import android.app.Activity;
import android.text.TextUtils;
import android.view.View;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.Toast;

public class MainActivity extends Activity {

    private EditText et_name;
    private EditText et_password;
    private CheckBox cb_ischeck;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        et_name = (EditText) findViewById(R.id.et_username);
        et_password = (EditText) findViewById(R.id.et_userpassword);
        cb_ischeck = (CheckBox) findViewById(R.id.cb_ischeck);

        // 讀取已存的數據
        Map<String, String> maps = UserInfoUtils.readInfo(MainActivity.this);

        if (maps != null) {
            // 取出
            String name = maps.get("name");
            String pwd = maps.get("pwd");
            et_name.setText(name);
            et_password.setText(pwd);
        }
    }

    // 點擊事件
    public void login(View v) {
        String name = et_name.getText().toString().trim();
        String pwd = et_password.getText().toString().trim();
        // 判斷用戶名密碼是否為空
        if (TextUtils.isEmpty(name) || TextUtils.isEmpty(pwd)) {
            Toast.makeText(MainActivity.this, "用戶名或密碼不能為空", Toast.LENGTH_LONG)
                    .show();
        } else {
            // 這裡不做資料庫操作,只是簡單的登錄邏輯
            System.out.println("連接伺服器驗證");
            // 存儲在本地
            if (cb_ischeck.isChecked()) {
                boolean result = UserInfoUtils.saveInfo(MainActivity.this,
                        name, pwd);
                if (result) {
                    Toast.makeText(MainActivity.this, "保存成功!",
                            Toast.LENGTH_LONG).show();
                } else {
                    Toast.makeText(MainActivity.this, "保存失敗", Toast.LENGTH_LONG)
                            .show();
                }
            } else {
                Toast.makeText(MainActivity.this, "請勾選", Toast.LENGTH_LONG)
                        .show();
            }
        }

    }

}

 

 

讀寫文件工具類:

package com.dreamtech.login;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStreamReader;
import java.util.HashMap;
import java.util.Map;

import android.content.Context;

public class UserInfoUtils {
    // 保存數據工具類
    public static boolean saveInfo(Context context ,String username, String pwd) {
        try {
            String path = context.getFilesDir().getPath();
            String result = username + "&&" + pwd;
            File file = new File(path,"info.txt");
            FileOutputStream fos = new FileOutputStream(file);
            fos.write(result.getBytes());
            fos.close();
            return true;
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }
    }

    public static Map<String, String> readInfo(Context context) {
        try {
            String path = context.getFilesDir().getPath();
            Map<String, String> maps = new HashMap<String, String>();
            File file = new File(path,"info.txt");
            FileInputStream fis = new FileInputStream(file);
            BufferedReader bufr = new BufferedReader(new InputStreamReader(fis));
            String content = bufr.readLine();
            String[] splits = content.split("&&");
            String name = splits[0];
            String pwd = splits[1];
            maps.put("name", name);
            maps.put("pwd", pwd);
            fis.close();
            return maps;
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }
}

 


您的分享是我們最大的動力!

-Advertisement-
Play Games
更多相關文章
  • 一、概述 分片是一種在多台機器上分配數據的方法。MongoDB使用分片來支持具有非常大的數據集和高吞吐量操作。有兩種解決系統增長的方法:垂直擴展和水平擴展。 垂直擴展涉及增加單個伺服器的容量,例如使用更強大的CPU,增加更多RAM或增加存儲空間量等。介於硬體成本和硬體性能單機器能支持的併發訪問和存儲 ...
  • 1.以前在本地設置sql庫密碼,就是在本地新建資料庫的時候就輸入,怎麼也鏈接不上,原來是新建資料庫的時候不能輸入密碼,需要在內部修改。 2. 打開mysql user表 3. 打開mysql user表,一看結構就明白了 4.用mysql 語句修改 use mysql; update user se ...
  • 程式連接orarle報ORA-12505錯誤 一、異常{ ORA-12505, TNS:listener does not currently know of SID given in connect descriptor The Connection descriptor used by the ...
  • 目前本人在看《SQL Server性能調優實戰》 ,以下內容是本人看本書的筆記 資料庫性能取決於各方面綜合因素: 硬體,操作系統,軟體 硬體:記憶體,CPU,磁碟 當伺服器的物理記憶體不足時,會產生大量的磁碟I/O,給磁碟帶來壓力; 當記憶體不足時,一些占用CPU資源較多的對象可能就無法被正常緩存在記憶體中 ...
  • 影響性能的幾個方面 伺服器 硬體 軟體 資料庫 資料庫存儲引擎的選擇(插件式存儲引擎) 資料庫參數配置(影響遠遠大於前面幾個影響) 資料庫結構設計和SQL語句 硬體 軟體 資料庫存儲引擎的選擇(插件式存儲引擎) 資料庫參數配置(影響遠遠大於前面幾個影響) 資料庫結構設計和SQL語句 一、伺服器 服務 ...
  • 1.命令行進入安裝mysql的bin目錄下;2.執行mysqld --skip-grant-tables命令跳過登錄驗證;3.執行msyql命令無需密碼登錄;4.執行show databases;命令查詢所有資料庫;5.使用名稱為“mysql”的資料庫(use mysql;);6.執行show ta ...
  • CREATE FUNCTION getweekdayCount ( @month varchar(10)) RETURNS INT AS BEGIN--declare @month varchar(10)--set @month='201802' 通過月份得到本月有幾天周末DECLARE @DAYC ...
  • 在Android手機上, 在某個程式里,通過按Menu鍵,一般都會打開這個程式的設置,而在iOS里,系統提供了一個很好的保存程式設置的機制。就是使用Settings Bundle。 在按了HOME鍵的情況下,在第一頁的圖標中找到設置,會看到程式的設置都在這裡。那如何添加自己的程式的設置項呢? 1、添 ...
一周排行
    -Advertisement-
    Play Games
  • 移動開發(一):使用.NET MAUI開發第一個安卓APP 對於工作多年的C#程式員來說,近來想嘗試開發一款安卓APP,考慮了很久最終選擇使用.NET MAUI這個微軟官方的框架來嘗試體驗開發安卓APP,畢竟是使用Visual Studio開發工具,使用起來也比較的順手,結合微軟官方的教程進行了安卓 ...
  • 前言 QuestPDF 是一個開源 .NET 庫,用於生成 PDF 文檔。使用了C# Fluent API方式可簡化開發、減少錯誤並提高工作效率。利用它可以輕鬆生成 PDF 報告、發票、導出文件等。 項目介紹 QuestPDF 是一個革命性的開源 .NET 庫,它徹底改變了我們生成 PDF 文檔的方 ...
  • 項目地址 項目後端地址: https://github.com/ZyPLJ/ZYTteeHole 項目前端頁面地址: ZyPLJ/TreeHoleVue (github.com) https://github.com/ZyPLJ/TreeHoleVue 目前項目測試訪問地址: http://tree ...
  • 話不多說,直接開乾 一.下載 1.官方鏈接下載: https://www.microsoft.com/zh-cn/sql-server/sql-server-downloads 2.在下載目錄中找到下麵這個小的安裝包 SQL2022-SSEI-Dev.exe,運行開始下載SQL server; 二. ...
  • 前言 隨著物聯網(IoT)技術的迅猛發展,MQTT(消息隊列遙測傳輸)協議憑藉其輕量級和高效性,已成為眾多物聯網應用的首選通信標準。 MQTTnet 作為一個高性能的 .NET 開源庫,為 .NET 平臺上的 MQTT 客戶端與伺服器開發提供了強大的支持。 本文將全面介紹 MQTTnet 的核心功能 ...
  • Serilog支持多種接收器用於日誌存儲,增強器用於添加屬性,LogContext管理動態屬性,支持多種輸出格式包括純文本、JSON及ExpressionTemplate。還提供了自定義格式化選項,適用於不同需求。 ...
  • 目錄簡介獲取 HTML 文檔解析 HTML 文檔測試參考文章 簡介 動態內容網站使用 JavaScript 腳本動態檢索和渲染數據,爬取信息時需要模擬瀏覽器行為,否則獲取到的源碼基本是空的。 本文使用的爬取步驟如下: 使用 Selenium 獲取渲染後的 HTML 文檔 使用 HtmlAgility ...
  • 1.前言 什麼是熱更新 游戲或者軟體更新時,無需重新下載客戶端進行安裝,而是在應用程式啟動的情況下,在內部進行資源或者代碼更新 Unity目前常用熱更新解決方案 HybridCLR,Xlua,ILRuntime等 Unity目前常用資源管理解決方案 AssetBundles,Addressable, ...
  • 本文章主要是在C# ASP.NET Core Web API框架實現向手機發送驗證碼簡訊功能。這裡我選擇是一個互億無線簡訊驗證碼平臺,其實像阿裡雲,騰訊雲上面也可以。 首先我們先去 互億無線 https://www.ihuyi.com/api/sms.html 去註冊一個賬號 註冊完成賬號後,它會送 ...
  • 通過以下方式可以高效,並保證數據同步的可靠性 1.API設計 使用RESTful設計,確保API端點明確,並使用適當的HTTP方法(如POST用於創建,PUT用於更新)。 設計清晰的請求和響應模型,以確保客戶端能夠理解預期格式。 2.數據驗證 在伺服器端進行嚴格的數據驗證,確保接收到的數據符合預期格 ...