Android_登入介面製作
阿新 • • 發佈:2020-12-06
1 <?xml version="1.0" encoding="utf-8"?> 2 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 3 android:layout_width="match_parent" 4 android:layout_height="match_parent" 5 android:background="#E6E6E6" 6 android:orientation="vertical" 7 android:padding="10dp"> 8 <ImageView 9 android:layout_width="70dp" 10 android:layout_height="70dp" 11 android:layout_centerHorizontal="true" 12 android:layout_gravity="center_horizontal" 13 android:layout_marginTop="30dp" 14 android:src="@mipmap/ic_launcher_round"/> 15 <LinearLayout 16 android:layout_width="match_parent" 17 android:layout_height="wrap_content" 18 android:layout_marginTop="15dp" 19 android:background="@android:color/white" 20 android:orientation="horizontal"> 21 <TextView 22 android:layout_width="wrap_content" 23 android:layout_height="wrap_content" 24 android:padding="10dp" 25 android:text="賬號:" 26 android:textColor="#000" 27 android:textSize="20sp" /> 28 <EditText 29 android:id="@+id/et_account" 30 android:layout_width="match_parent" 31 android:layout_height="wrap_content" 32 android:layout_marginLeft="5dp" 33 android:background="@null" 34 android:padding="10dp" /> 35 </LinearLayout> 36 <LinearLayout 37 android:layout_width="match_parent" 38 android:layout_height="wrap_content" 39 android:layout_marginTop="10dp" 40 android:background="@android:color/white" 41 android:orientation="horizontal"> 42 <TextView 43 android:id="@+id/tv_password" 44 android:layout_width="wrap_content" 45 android:layout_height="wrap_content" 46 android:padding="10dp" 47 android:text="密碼:" 48 android:textColor="#000" 49 android:textSize="20sp" /> 50 <EditText 51 android:id="@+id/et_password" 52 android:layout_width="match_parent" 53 android:layout_height="wrap_content" 54 android:layout_marginLeft="5dp" 55 android:background="@null" 56 android:inputType="textPassword" 57 android:padding="10dp" /> 58 </LinearLayout> 59 <Button 60 android:id="@+id/btn_login" 61 android:layout_width="match_parent" 62 android:layout_height="wrap_content" 63 android:layout_marginTop="25dp" 64 android:background="#3C8DC4" 65 android:text="登入" 66 android:textColor="@android:color/white" 67 android:textSize="20sp" /> 68 </LinearLayout>
1 package cn.itcast.saveqq; 2 3 import android.os.Bundle; 4 import android.support.v7.app.AppCompatActivity; 5 import android.text.TextUtils; 6 import android.view.View; 7 import android.widget.Button; 8 import android.widget.EditText; 9 import android.widget.Toast; 10 11 import java.util.Map; 12 13 public class MainActivity extends AppCompatActivity implements View.OnClickListener 14 { 15 private EditText et_account; //賬號輸入框 16 private EditText et_password; //密碼輸入框 17 private Button btn_login; //登入按鈕 18 @Override 19 protected void onCreate(Bundle savedInstanceState) { 20 super.onCreate(savedInstanceState); 21 setContentView(R.layout.activity_main); 22 initView(); 23 //通過工具類FileSaveQQ中的getUserInfo()方法獲取QQ賬號和密碼資訊 24 // Map<String, String> userInfo = FileSaveQQ.getUserInfo(this); 25 Map<String, String> userInfo = SPSaveQQ.getUserInfo(this); 26 if (userInfo != null) { 27 et_account.setText(userInfo.get("account")); //將獲取的賬號顯示到介面上 28 et_password.setText(userInfo.get("password")); //將獲取的密碼顯示到介面上 29 } 30 } 31 private void initView() { 32 et_account = (EditText) findViewById(R.id.et_account); 33 et_password = (EditText) findViewById(R.id.et_password); 34 btn_login = (Button) findViewById(R.id.btn_login); 35 //設定按鈕的點選監聽事件 36 btn_login.setOnClickListener(this); 37 } 38 @Override 39 public void onClick(View v) { 40 switch (v.getId()) { 41 case R.id.btn_login: 42 //當點選登入按鈕時,獲取介面上輸入的QQ賬號和密碼 43 String account = et_account.getText().toString().trim(); 44 String password = et_password.getText().toString(); 45 //檢驗輸入的賬號和密碼是否為空 46 if (TextUtils.isEmpty(account)) { 47 Toast.makeText(this, "請輸入QQ賬號", Toast.LENGTH_SHORT).show(); 48 return; 49 } 50 if (TextUtils.isEmpty(password)) { 51 Toast.makeText(this, "請輸入密碼", Toast.LENGTH_SHORT).show(); 52 return; 53 } 54 Toast.makeText(this, "登入成功", Toast.LENGTH_SHORT).show(); 55 //儲存使用者資訊 56 boolean isSaveSuccess = FileSaveQQ.saveUserInfo(this, account, 57 password); 58 /* boolean isSaveSuccess = SPSaveQQ.saveUserInfo(this, account, password);*/ 59 if (isSaveSuccess) { 60 Toast.makeText(this, "儲存成功", Toast.LENGTH_SHORT).show(); 61 } else { 62 Toast.makeText(this, "儲存失敗", Toast.LENGTH_SHORT).show(); 63 } 64 break; 65 } 66 } 67 }
1 package cn.itcast.saveqq; 2 3 import android.content.Context; 4 import android.util.Log; 5 6 import java.io.FileInputStream; 7 import java.io.FileOutputStream; 8 import java.io.IOException; 9 import java.util.HashMap; 10 import java.util.Map; 11 12 public class FileSaveQQ { 13 //儲存QQ賬號和登入密碼到data.txt檔案中 14 public static boolean saveUserInfo(Context context, String account, String 15 password) { 16 FileOutputStream fos = null; 17 try { 18 //獲取檔案的輸出流物件fos 19 fos = context.openFileOutput("data.txt", 20 Context.MODE_PRIVATE); 21 //將資料轉換為位元組碼的形式寫入data.txt檔案中 22 fos.write((account + ":" + password).getBytes()); 23 return true; 24 } catch (Exception e) { 25 e.printStackTrace(); 26 return false; 27 }finally { 28 try { 29 if(fos != null){ 30 fos.close(); 31 } 32 } catch (IOException e) { 33 e.printStackTrace(); 34 } 35 } 36 } 37 //從data.txt檔案中獲取儲存的QQ賬號和密碼 38 public static Map<String, String> getUserInfo(Context context) { 39 String content = ""; 40 FileInputStream fis = null; 41 try { 42 //獲取檔案的輸入流物件fis 43 fis = context.openFileInput("data.txt"); 44 //將輸入流物件中的資料轉換為位元組碼的形式 45 byte[] buffer = new byte[fis.available()]; 46 fis.read(buffer);//通過read()方法讀取位元組碼中的資料 47 content = new String(buffer); //將獲取的位元組碼轉換為字串 48 Map<String, String> userMap = new HashMap<String, String>(); 49 String[] infos = content.split(":");//將字串以“:”分隔後形成一個數組的形式 50 userMap.put("account", infos[0]); //將陣列中的第一個資料放入userMap集合中 51 userMap.put("password", infos[1]); //將陣列中的第二個資料放入userMap集合中 52 return userMap; 53 } catch (Exception e) { 54 e.printStackTrace(); 55 return null; 56 }finally { 57 try { 58 if(fis != null){ 59 fis.close(); 60 } 61 } catch (IOException e) { 62 e.printStackTrace(); 63 } 64 } 65 } 66 }
1 package cn.itcast.saveqq; 2 3 import android.content.Context; 4 import android.content.SharedPreferences; 5 6 import java.util.HashMap; 7 import java.util.Map; 8 9 public class SPSaveQQ{ 10 // 儲存QQ賬號和登入密碼到data.xml檔案中 11 public static boolean saveUserInfo(Context context, String account, 12 String password) { 13 SharedPreferences sp = context.getSharedPreferences("data", 14 Context.MODE_PRIVATE); 15 SharedPreferences.Editor edit = sp.edit(); 16 edit.putString("userName", account); 17 edit.putString("pwd", password); 18 edit.commit(); 19 return true; 20 } 21 //從data.xml檔案中獲取儲存的QQ賬號和密碼 22 public static Map<String, String> getUserInfo(Context context) { 23 SharedPreferences sp = context.getSharedPreferences("data", 24 Context.MODE_PRIVATE); 25 String account = sp.getString("userName", null); 26 String password = sp.getString("pwd", null); 27 Map<String, String> userMap = new HashMap<String, String>(); 28 userMap.put("account", account); 29 userMap.put("password", password); 30 return userMap; 31 } 32 }