安卓案例2:簡單登入介面和儲存資訊

來源:互聯網
上載者:User

標籤:leo   adl   app   create   輸入   pat   bool   OLE   布局   

介面效果:

 

布局代碼:

<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;        }    }}

 

安卓案例2:簡單登入介面和儲存資訊

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.