安卓登錄案例
阿新 • • 發佈:2019-05-17
director output undle 指定 readline one fin color @override
package com.lidaochen.phonecall; import android.content.Intent; import android.net.Uri; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.text.TextUtils; import android.util.Log; import android.view.View; import android.widget.Button; import android.widget.CheckBox;import android.widget.EditText; import android.widget.TabHost; import android.widget.ThemedSpinnerAdapter; import android.widget.Toast; import java.util.Map; public class MainActivity extends AppCompatActivity{ private EditText et_userName; private EditText et_pwd; private CheckBox cb; @Overrideprotected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); // 1.獲取控件 et_userName = (EditText)findViewById(R.id.et_username); et_pwd = (EditText)findViewById(R.id.et_pwd); cb = (CheckBox)findViewById(R.id.cb);// 2.讀取數據 Map<String, String> maps = UserInfoUtils.readInfo(); if(maps != null) { // 把name和pwd讀取出來 String name = maps.get("name"); String pwd = maps.get("pwd"); // 把讀取出來的數據顯示到控件上 et_userName.setText(name); et_pwd.setText(pwd); } } // 按鈕點擊事件 public void login(View v) { // 獲取userName和pwd String name = et_userName.getText().toString().trim(); String pwd = et_pwd.getText().toString().trim(); // 判斷name和pwd是否為空 if(TextUtils.isEmpty(name) || TextUtils.isEmpty(pwd)) { Toast.makeText(MainActivity.this, "用戶名或密碼不能為空", Toast.LENGTH_LONG).show(); } else { System.out.println("登錄之後要幹的事情!"); if(cb.isChecked()) { // 把用戶名和密碼保存下來 boolean result = UserInfoUtils.saveInfo(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(); } } } }
1 package com.lidaochen.phonecall; 2 3 import android.graphics.MaskFilter; 4 5 import java.io.BufferedReader; 6 import java.io.File; 7 import java.io.FileInputStream; 8 import java.io.FileOutputStream; 9 import java.io.InputStreamReader; 10 import java.util.HashMap; 11 import java.util.Map; 12 13 public class UserInfoUtils { 14 // 保存用戶名和密碼的方法 15 public static boolean saveInfo(String username, String pwd) 16 { 17 try { 18 String result = username + "##" + pwd; 19 // 創建File類,指定我們要把數據存儲的位置 20 File file = new File("/data/data/com.lidonghao.phonecall/info.txt"); 21 // 創建一個文件輸出流 22 FileOutputStream fos = new FileOutputStream(file); 23 fos.write(result.getBytes()); 24 fos.close(); 25 return true; 26 } 27 catch (Exception e) 28 { 29 e.printStackTrace(); 30 return false; 31 } 32 } 33 34 // 讀取用戶的信息 35 public static Map<String, String> readInfo() 36 { 37 try{ 38 // 定義map 39 Map<String, String> maps = new HashMap<String, String>(); 40 File file = new File("/data/data/com.lidonghao.phonecall/info.txt"); 41 FileInputStream fis = new FileInputStream(file); 42 BufferedReader bufr = new BufferedReader(new InputStreamReader(fis)); 43 // 讀取數據 44 String content = bufr.readLine(); 45 // 切割字符串,封裝到map集合中 46 String[] splits = content.split("##"); 47 String name = splits[0]; 48 String pwd = splits[1]; 49 maps.put("name", name); 50 maps.put("pwd", pwd); 51 fis.close(); 52 return maps; 53 } 54 catch (Exception e) 55 { 56 e.printStackTrace(); 57 return null; 58 } 59 } 60 }
下面是使用上下文獲取常用目錄
package com.lidaochen.phonecall; import android.content.Intent; import android.net.Uri; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.text.TextUtils; import android.util.Log; import android.view.View; import android.widget.Button; import android.widget.CheckBox; import android.widget.EditText; import android.widget.TabHost; import android.widget.ThemedSpinnerAdapter; import android.widget.Toast; import java.util.Map; public class MainActivity extends AppCompatActivity{ private EditText et_userName; private EditText et_pwd; private CheckBox cb; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); // 1.獲取控件 et_userName = (EditText)findViewById(R.id.et_username); et_pwd = (EditText)findViewById(R.id.et_pwd); cb = (CheckBox)findViewById(R.id.cb); // 2.讀取數據 Map<String, String> maps = UserInfoUtils.readInfo(MainActivity.this); if(maps != null) { // 把name和pwd讀取出來 String name = maps.get("name"); String pwd = maps.get("pwd"); // 把讀取出來的數據顯示到控件上 et_userName.setText(name); et_pwd.setText(pwd); } } // 按鈕點擊事件 public void login(View v) { // 獲取userName和pwd String name = et_userName.getText().toString().trim(); String pwd = et_pwd.getText().toString().trim(); // 判斷name和pwd是否為空 if(TextUtils.isEmpty(name) || TextUtils.isEmpty(pwd)) { Toast.makeText(MainActivity.this, "用戶名或密碼不能為空", Toast.LENGTH_LONG).show(); } else { System.out.println("登錄之後要幹的事情!"); if(cb.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(); } } } }
1 package com.lidaochen.phonecall; 2 3 import android.content.Context; 4 import android.graphics.MaskFilter; 5 6 import java.io.BufferedReader; 7 import java.io.File; 8 import java.io.FileInputStream; 9 import java.io.FileOutputStream; 10 import java.io.InputStreamReader; 11 import java.util.HashMap; 12 import java.util.Map; 13 14 public class UserInfoUtils { 15 // 保存用戶名和密碼的方法 16 public static boolean saveInfo(Context context, String username, String pwd) 17 { 18 try { 19 String result = username + "##" + pwd; 20 // 通過上下文獲取FileOutputStream 21 FileOutputStream fos = context.openFileOutput("infoo.txt", 0); 22 fos.write(result.getBytes()); 23 fos.close(); 24 return true; 25 } 26 catch (Exception e) 27 { 28 e.printStackTrace(); 29 return false; 30 } 31 } 32 33 // 讀取用戶的信息 34 public static Map<String, String> readInfo(Context context) 35 { 36 try{ 37 // 定義map 38 Map<String, String> maps = new HashMap<String, String>(); 39 FileInputStream fis = context.openFileInput("infoo.txt"); 40 BufferedReader bufr = new BufferedReader(new InputStreamReader(fis)); 41 // 讀取數據 42 String content = bufr.readLine(); 43 // 切割字符串,封裝到map集合中 44 String[] splits = content.split("##"); 45 String name = splits[0]; 46 String pwd = splits[1]; 47 maps.put("name", name); 48 maps.put("pwd", pwd); 49 fis.close(); 50 return maps; 51 } 52 catch (Exception e) 53 { 54 e.printStackTrace(); 55 return null; 56 } 57 } 58 }
下面是保存數據到SD卡 (註意:保存到SD卡需要相應的權限,否則會保存失敗)
1 package com.liddaochen.phonecall; 2 3 import android.content.Intent; 4 import android.net.Uri; 5 import android.os.Environment; 6 import android.support.v7.app.AppCompatActivity; 7 import android.os.Bundle; 8 import android.text.TextUtils; 9 import android.util.Log; 10 import android.view.View; 11 import android.widget.Button; 12 import android.widget.CheckBox; 13 import android.widget.EditText; 14 import android.widget.TabHost; 15 import android.widget.ThemedSpinnerAdapter; 16 import android.widget.Toast; 17 18 import java.util.Map; 19 20 public class MainActivity extends AppCompatActivity{ 21 private EditText et_userName; 22 private EditText et_pwd; 23 private CheckBox cb; 24 @Override 25 protected void onCreate(Bundle savedInstanceState) { 26 super.onCreate(savedInstanceState); 27 setContentView(R.layout.activity_main); 28 // 1.獲取控件 29 et_userName = (EditText)findViewById(R.id.et_username); 30 et_pwd = (EditText)findViewById(R.id.et_pwd); 31 cb = (CheckBox)findViewById(R.id.cb); 32 // 2.讀取數據 33 Map<String, String> maps = UserInfoUtils.readInfo(); 34 if(maps != null) 35 { 36 // 把name和pwd讀取出來 37 String name = maps.get("name"); 38 String pwd = maps.get("pwd"); 39 // 把讀取出來的數據顯示到控件上 40 et_userName.setText(name); 41 et_pwd.setText(pwd); 42 } 43 } 44 // 按鈕點擊事件 45 public void login(View v) 46 { 47 // 獲取userName和pwd 48 String name = et_userName.getText().toString().trim(); 49 String pwd = et_pwd.getText().toString().trim(); 50 // 判斷name和pwd是否為空 51 if(TextUtils.isEmpty(name) || TextUtils.isEmpty(pwd)) 52 { 53 Toast.makeText(MainActivity.this, "用戶名或密碼不能為空", Toast.LENGTH_LONG).show(); 54 } 55 else 56 { 57 System.out.println("登錄之後要幹的事情!"); 58 if(cb.isChecked()) 59 { 60 // 將數據保存到SD卡需要判斷SD卡是否可用 61 if (Environment.MEDIA_MOUNTED.equals(Environment.getExternalStorageState())) 62 { 63 // 把用戶名和密碼保存下來 64 boolean result = UserInfoUtils.saveInfo(name, pwd); 65 if (result) 66 { 67 Toast.makeText(MainActivity.this, "保存成功!", Toast.LENGTH_LONG).show(); 68 } 69 else 70 { 71 Toast.makeText(MainActivity.this, "保存失敗!", Toast.LENGTH_LONG).show(); 72 } 73 } 74 else 75 { 76 Toast.makeText(MainActivity.this, "SD卡不可用!", Toast.LENGTH_LONG).show(); 77 } 78 } 79 else 80 { 81 Toast.makeText(MainActivity.this, "請勾選復選框", Toast.LENGTH_LONG).show(); 82 } 83 } 84 } 85 }
1 package com.lidaochen.phonecall; 2 3 import android.content.Context; 4 import android.graphics.MaskFilter; 5 import android.os.Environment; 6 7 import java.io.BufferedReader; 8 import java.io.File; 9 import java.io.FileInputStream; 10 import java.io.FileOutputStream; 11 import java.io.InputStreamReader; 12 import java.util.HashMap; 13 import java.util.Map; 14 15 public class UserInfoUtils { 16 // 保存用戶名和密碼的方法 17 public static boolean saveInfo(String username, String pwd) 18 { 19 try { 20 String result = username + "##" + pwd; 21 // 創建FILE類指定我們要保存的位置 22 String SDPath = Environment.getExternalStorageDirectory().getPath(); 23 File file = new File(SDPath, "hehe.txt"); 24 // 創建輸出流FileOutputStream 25 FileOutputStream fos = new FileOutputStream(file); 26 fos.write(result.getBytes()); 27 fos.close(); 28 return true; 29 } 30 catch (Exception e) 31 { 32 e.printStackTrace(); 33 return false; 34 } 35 } 36 37 // 讀取用戶的信息 38 public static Map<String, String> readInfo() 39 { 40 try{ 41 // 定義map 42 Map<String, String> maps = new HashMap<String, String>(); 43 String SDPath = Environment.getExternalStorageDirectory().getPath(); 44 File file = new File(SDPath, "hehe.txt"); 45 FileInputStream fis = new FileInputStream(file); 46 BufferedReader bufr = new BufferedReader(new InputStreamReader(fis)); 47 // 讀取數據 48 String content = bufr.readLine(); 49 // 切割字符串,封裝到map集合中 50 String[] splits = content.split("##"); 51 String name = splits[0]; 52 String pwd = splits[1]; 53 maps.put("name", name); 54 maps.put("pwd", pwd); 55 fis.close(); 56 return maps; 57 } 58 catch (Exception e) 59 { 60 e.printStackTrace(); 61 return null; 62 } 63 } 64 }
安卓登錄案例