登录
来源:互联网 发布:游戏汉化器安卓软件 编辑:程序博客网 时间:2024/04/29 19:17
activity_main.xml
<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="match_parent" android:layout_height="wrap_content" android:text="请输入用户名" /> <EditText android:id="@+id/et_username" android:layout_width="match_parent" android:layout_height="wrap_content" /> <TextView android:layout_width="match_parent" android:layout_height="wrap_content" android:text="请输入密码" /> <EditText android:id="@+id/et_password" android:layout_width="match_parent" android:layout_height="wrap_content" android:inputType="textPassword" /> <RelativeLayout android:layout_width="match_parent" android:layout_height="wrap_content" > <CheckBox android:id="@+id/cb_remember_pwd" android:layout_width="wrap_content" android:layout_height="wrap_content" android:checked="true" android:text="记住密码" > </CheckBox> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentRight="true" android:onClick="login" android:text="登录" /> </RelativeLayout></LinearLayout>
package org.gentry.login;import java.io.File;import java.util.Map;import org.gentry.login.service.LoginService;import android.os.Bundle;import android.app.Activity;import android.text.TextUtils;import android.util.Log;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 static final String TAG = "MainActivity";private EditText et_username;private EditText et_password;private CheckBox cb;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);et_username = (EditText) findViewById(R.id.et_username); // 获取用户名的idet_password = (EditText) findViewById(R.id.et_password); // 获取密码的idcb = (CheckBox) findViewById(R.id.cb_remember_pwd); // 获取记住密码选项的id// 检查是否有保存的用户名密码数据,如果有回显出来Map<String, String> map = LoginService.getSavedUserInfo(this);if (map != null) {et_username.setText(map.get("username")); // 显示用户名的数据et_password.setText(map.get("password")); // 显示密码的数据}}/** * 登录 * * @param view */public void login(View view) {String username = et_username.getText().toString().trim(); // 得到用户名数据(去空格)String password = et_password.getText().toString().trim(); // 得到密码的数据(去空格)// 判定输入信息是否为空if (TextUtils.isEmpty(username) || TextUtils.isEmpty(password)) {Toast.makeText(this, "用户名或者密码不能为空", Toast.LENGTH_SHORT).show();} else {// 登录// 判断是否保存用户名密码if (cb.isChecked()) {Log.i(TAG, "需要保护用户名密码");// 保存用户名密码boolean result = LoginService.saveUserInfo(this, username,password);// 判断用户信息是否保存成功if (result) {Toast.makeText(this, "保存用户信息成功", Toast.LENGTH_SHORT).show();} else {Toast.makeText(this, "保存用户信息失败", Toast.LENGTH_SHORT).show();}}// 登录发送消息到服务器,服务区验证是否成功if ("zhangsan".equals(username) && "123".equals(password)) {Toast.makeText(this, "登录成功", Toast.LENGTH_SHORT).show();} else {Toast.makeText(this, "登录失败,用户名或者密码错误", Toast.LENGTH_SHORT).show();}}}}
LoginService.java
package org.gentry.login.service;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 LoginService {/** * 保存用户名和密码的业务方法 * * @param context * 上下文 * @param username * 用户名 * @param password * 密码 * @return true 保存成功 false 保存失败 */public static boolean saveUserInfo(Context context, String username,String password) {try {// 创建存储用户信息的文件// File file = new File("/data/data/org.gentry.login/info.txt");File file = new File(context.getFilesDir(), "info.txt");// context.getFilesDir()// 帮助我们返回一个目录/data/data/包名/files/FileOutputStream fos = new FileOutputStream(file); // 创建文件输出流fos.write((username + "##" + password).getBytes()); // 把获取到的用户信息通过文件输出流写到文件中fos.close(); // 关闭文件输出流return true;} catch (Exception e) {// TODO Auto-generated catch blocke.printStackTrace();return false;}}/** * 获取保存的数据(记住密码) * * @param context * 上下文 * @return Map集合 */public static Map<String, String> getSavedUserInfo(Context context) {File file = new File(context.getFilesDir(), "info.txt"); // 创建文件try {FileInputStream fis = new FileInputStream(file); // 创建文件输入流BufferedReader br = new BufferedReader(new InputStreamReader(fis)); // 从字符输入流中读取文本,缓冲各个字符,从而实现字符、数组和行的高效读取。String str = br.readLine(); // 读取文件中的一行// zhangsan##123String[] infos = str.split("##"); // 根据##拆分此字符串Map<String, String> map = new HashMap<String, String>(); // 创建HashMap类map.put("username", infos[0]); // infos[0]的值赋给usernamemap.put("password", infos[1]); // infos[1]的值赋给passwordreturn map;} catch (Exception e) {// TODO Auto-generated catch blocke.printStackTrace();return null;}}}
0 0
- 登录
- 登录
- 登录
- 登录
- 登录
- 登录
- 登录
- 登录
- 登录
- 登录
- 登录
- 登录
- 登录
- 登录
- 登录
- 登录
- 登录
- 登录超时重新登录
- Android_监听自身应用被卸载
- C++多线程编程入门
- UVa 10012 How Big Is It? 解题报告(枚举排列)
- BeagleBone Black 第二课:文件结构
- HttpClient发起POST请求
- 登录
- 企业开源】小米开源:站在巨人肩膀上的创新
- Longest Common Prefix
- c++之内存分配大解析
- 从B 树、B+ 树、B* 树谈到R 树
- POJ 1442 Black Box
- 虚拟机测试全表扫描成本
- spring3.2+hibernate4 事务管理(支持自定义部分开发和关闭事务)
- MFC导入ActiveX控件