做一個簡單登入介面
阿新 • • 發佈:2019-01-08
先寫好登入介面的佈局,記住要寫一個Checkbox。
介面格式如下圖:<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" > <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="請輸入賬號" /> <EditText android:id="@+id/ed_zhanghao" android:layout_width="match_parent" android:layout_height="wrap_content"/> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="請輸入密碼" /> <EditText android:id="@+id/ed_mima" android:inputType="textPassword" android:layout_width="match_parent" android:layout_height="wrap_content"/> <RelativeLayout android:layout_width="match_parent" android:layout_height="wrap_content"> <CheckBox android:id="@+id/cb" android:layout_width="wrap_content" android:layout_height="wrap_content" android:checked="true" android:text="記住密碼"/> <Button android:onClick="denglu" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentRight="true" android:text="登入"/> </RelativeLayout> </LinearLayout>
再寫一個儲存賬號密碼的工具類,儲存路勁可以寫在手機或者SD卡里:
最後在寫主程式碼實現登入功能:package com.xh.tx.denglu.service; import java.io.BufferedReader; import java.io.File; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.IOException; import java.io.InputStreamReader; import java.util.HashMap; import java.util.Map; import android.content.Context; import android.os.Environment; public class Servicein { public static boolean serviceinfo(Context context,String zhanghao,String mima){ try { //File f= new File("data/data/com.xh.tx.denglu/info.txt"); //context.getFilesDir();幫助我們返回一個目錄,data/data/包名/files/ //context.getCacheDir();這個是快取目錄 //Environment.getExternalStorageState();獲取SD卡是什麼樣的狀態 //String s=Environment.MEDIA_MOUNTED;獲取SD卡的插入狀態 //Environment.getExternalStorageDirectory();返回一個SD卡的路勁; File f= new File(context.getFilesDir(),"info.txt"); FileOutputStream fs=new FileOutputStream(f); fs.write((zhanghao+"#####"+mima).getBytes()); fs.close(); return true; } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); return false; } } public static Map<String,String> getinfo(Context context){ File f= new File(context.getFilesDir(),"info.txt"); try { BufferedReader br= new BufferedReader(new InputStreamReader(new FileInputStream(f))); String s=br.readLine(); String[] srinfo=s.split("#####"); Map<String,String> map=new HashMap<String, String>(); map.put("zhanghao",srinfo[0]); map.put("mima",srinfo[1]); return map; } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); return null; } } }
這樣就可以實現一個簡單的登入介面了package com.xh.tx.denglu; import java.util.Map; import com.xh.tx.denglu.service.Servicein; import android.os.Bundle; import android.app.Activity; import android.app.Service; import android.text.TextUtils; import android.view.Menu; import android.view.View; import android.widget.CheckBox; import android.widget.EditText; import android.widget.Toast; public class MainActivity extends Activity { private EditText ed_zhanghao; private EditText ed_mima; private CheckBox cb; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); ed_zhanghao = (EditText) findViewById(R.id.ed_zhanghao); ed_mima = (EditText) findViewById(R.id.ed_mima); cb = (CheckBox) findViewById(R.id.cb); //檢查是否有賬號密碼,如果有,需要回顯 Map<String, String> map=Servicein.getinfo(this); if(map!=null){ ed_zhanghao.setText(map.get("zhanghao")); ed_mima.setText(map.get("mima")); } } public void denglu(View v){ String zhanghao = ed_zhanghao.getText().toString().trim(); String mima = ed_mima.getText().toString().trim(); if(TextUtils.isEmpty(zhanghao)||TextUtils.isEmpty(mima)){ Toast.makeText(this, "賬號或者密碼不能為空", Toast.LENGTH_SHORT).show(); }else{ if(cb.isChecked()){ boolean result=Servicein.serviceinfo(this,zhanghao,mima); if(result){ Toast.makeText(this, "儲存成功", Toast.LENGTH_SHORT).show(); } } if("zhangsan".equals(zhanghao)&&"123456".equals(mima)){ Toast.makeText(this, "登陸成功", Toast.LENGTH_SHORT).show(); }else{ Toast.makeText(this, "登入失敗", Toast.LENGTH_SHORT).show(); } } } }