安卓案例2:简单登录界面和保存信息
程序员文章站
2022-03-27 15:20:41
界面效果: 布局代码: 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; } } }
下一篇: 畜牲是不会下车的