Android开发之完成登陆界面的数据保存回显操作实例
作者:sgx425021234 发布时间:2022-07-05 19:13:40
标签:Android,登陆
本文实例讲述了Android开发之完成登陆界面的数据保存回显操作。分享给大家供大家参考,具体如下:
LoginActivity.java:
package com.example.login;
import java.util.Map;
import android.app.Activity;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.Menu;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.Toast;
import com.example.login.service.FileService;
public class LoginActivity extends Activity {
public EditText edit_name,edit_pass;
public Button btn_login;
public CheckBox box_remeber;
public FileService fileService;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_login);
fileService=new FileService(this);
edit_name=(EditText) findViewById(R.id.edit_name);
edit_pass=(EditText) findViewById(R.id.edit_pass);
btn_login=(Button) findViewById(R.id.btn_login);
box_remeber=(CheckBox) findViewById(R.id.cbx_remember);
btn_login.setOnClickListener(new MyOnClickListener());
Map<String, String> map=fileService.readFile("private.txt");
if(map!=null){
edit_name.setText(map.get("name"));
edit_pass.setText(map.get("pass"));
}
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.login, menu);
return true;
}
class MyOnClickListener implements View.OnClickListener{
@Override
public void onClick(View v) {
int id=v.getId();
switch (id) {
case R.id.btn_login:
String name=edit_name.getText().toString();
String pass=edit_pass.getText().toString();
if(TextUtils.isEmpty(name)){
Toast.makeText(LoginActivity.this, "用户名不能为空", Toast.LENGTH_SHORT).show();
return;
}else if(TextUtils.isEmpty(pass)){
Toast.makeText(LoginActivity.this, "密码不能为空", Toast.LENGTH_SHORT).show();
return;
}else{
if(box_remeber.isChecked()){
LoginActivity.this.fileService.saveToRom(name, pass, "private.txt");
Toast.makeText(LoginActivity.this, "用户名和密码已保存", Toast.LENGTH_SHORT).show();
}else{
Toast.makeText(LoginActivity.this, "用户名和密码不需要保存", Toast.LENGTH_SHORT).show();
}
}
break;
default:
break;
}
/*if(id==btn_login.getId()){
String name=edit_name.getText().toString();
String pass=edit_pass.getText().toString();
if(TextUtils.isEmpty(name)){
Toast.makeText(LoginActivity.this, "用户名不能为空", Toast.LENGTH_SHORT).show();
return;
}else if(TextUtils.isEmpty(pass)){
Toast.makeText(LoginActivity.this, "密码不能为空", Toast.LENGTH_SHORT).show();
return;
}else{
if(box_remeber.isChecked()){
LoginActivity.this.fileService.saveToRom(name, pass, "private.txt");
Toast.makeText(LoginActivity.this, "用户名和密码已保存", Toast.LENGTH_SHORT).show();
}else{
Toast.makeText(LoginActivity.this, "用户名和密码不需要保存", Toast.LENGTH_SHORT).show();
}
}
}*/
}
}
}
FileService.java:
package com.example.login.service;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.util.HashMap;
import java.util.Map;
import com.example.login.utils.StreamTools;
import android.content.Context;
public class FileService {
public Context context;
public FileService(Context context) {
this.context = context;
}
public boolean saveToRom(String name,String pass,String fileName){
try{
FileOutputStream fos=context.openFileOutput(fileName, Context.MODE_PRIVATE);
String result=name+":"+pass;
fos.write(result.getBytes());
fos.flush();
fos.close();
}catch(Exception e){
e.printStackTrace();
return false;
}
return true;
}
public Map<String,String> readFile(String fileName){
Map<String,String> map=null;
try{
FileInputStream fis=context.openFileInput(fileName);
String value=StreamTools.getValue(fis);
String values[]=value.split(":");
if(values.length>0){
map=new HashMap<String, String>();
map.put("name", values[0]);
map.put("pass", values[1]);
}
}catch(Exception e){
e.printStackTrace();
}
return map;
}
}
StreamTools.java:
package com.example.login.utils;
import java.io.ByteArrayOutputStream;
import java.io.FileInputStream;
public class StreamTools {
public static String getValue(FileInputStream fis) throws Exception{
ByteArrayOutputStream stream=new ByteArrayOutputStream();
byte[] buffer=new byte[1024];
int length=-1;
while((length=fis.read(buffer))!=-1){
stream.write(buffer,0,length);
}
stream.flush();
stream.close();
String value=stream.toString();
return value;
}
}
login_activity.xml:
<RelativeLayout 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:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".LoginActivity" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:orientation="vertical" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<TextView
android:id="@+id/view_name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/text_name" />
<EditText
android:id="@+id/edit_name"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:ems="10"
android:inputType="textPersonName">
<requestFocus />
</EditText>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<TextView
android:id="@+id/view_pass"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/text_pass" />
<EditText
android:id="@+id/edit_pass"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:ems="10"
android:inputType="textPassword" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<Button
android:id="@+id/btn_login"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="0.17"
android:text="@string/text_login" />
<CheckBox
android:id="@+id/cbx_remember"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="80dp"
android:text="@string/text_rember" />
</LinearLayout>
</LinearLayout>
</RelativeLayout>
String.xml:
<?xmlversion="1.0"encoding="utf-8"?>
<resources>
<stringname="app_name">login</string>
<stringname="action_settings">Settings</string>
<stringname="hello_world">Login</string>
<stringname="text_name">用户名:</string>
<stringname="text_pass">密码:</string>
<stringname="text_login">登陆</string>
<stringname="text_rember">记住密码</string>
</resources>
希望本文所述对大家Android程序设计有所帮助。


猜你喜欢
- LayoutInflater.inflate源码详解LayoutInflater的inflate方法相信大家都不陌生,在Fragment的o
- 三种定义数组的格式如下:int[] arr1=new int[10];int[] arr2={1,2,3,6};int[] arr3=new
- 本文实例为大家分享了Android开发之自定义闹钟实现,供大家参考,具体内容如下闹钟时间设置及显示闹钟的布局很简单,就是一个简单时间设置,所
- spring data jpa @query返回map踩坑记录最近用spring data jpa,网上看的一些教程,有的说是返回的Obje
- 1.获取签名与模板进入阿里云平台,进入短信服务模块,在以下位置添加签名和模板(格式一定按照要求填写 审批的比较严格)2.编写模板与签名的枚举
- 对单机服务做接口限流的处理方案简单说就是设定某个接口一定时间只接受固定次数的请求,比如/add接口1秒最多接收100次请求,多的直接拒绝,这
- 简介在文章《GraalVM和Spring Native尝鲜,一步步让Springboot启动飞起来,66ms完成启动》中,我们介绍了如何使用
- 长久以来统领javaee领域的脚手架以spring struts2 mybatis/hib
- Excel表格中,由于各种数据的复杂性,可能存在单元格中的数据字号大小、数据内容长度不一而出现,列宽过宽、过窄或者行高过大、过小的问题。常见
- 本文实例为大家分享了C#实现文件上传与下载的具体代码,供大家参考,具体内容如下C#实现文件上传代码: public ActionResult
- 关于《JavaCV的摄像头实战》系列《JavaCV的摄像头实战》顾名思义,是使用JavaCV框架对摄像头进行各种处理的实战集合,这是欣宸作为
- java 8的新特性之一就是lambda表达式,parallelStream()都说性能会比较高,现一探究竟。话不多说,上代码: @Test
- 一、题目描述二、思路语法基础:StringBuilder 类似列表,可以更改元素。package Practice;public class
- 1.什么是Ribbon目前主流的负载均衡方案分为以下两种:(1)集中式负载均衡:在消费者和服务提供者中间使用独立的代理方式进行负载,有硬件的
- 某些Google Play服务(例如Google登录和App Invites)要求我们提供签名证书的SHA-1,以便google paly为
- 相对布局要比前面讲的线性布局和表格布局要灵活一些,所以平常用得也是比较多的。相对布局控件的位置是与其周围控件的位置相关的,从名字可以看出来,
- 理解枚举类型枚举类型是Java 5中新增特性的一部分,它是一种特殊的数据类型,之所以特殊是因为它既是一种类(class)类型却又比类类型多了
- 基石想要盖起高楼的话肯定要打好基石,同样,想要学好 Compose 动画也需要一些“基石”,一起来看
- 前言我又搞回笃NET啦!java之路真是命运多舛,好事多磨。不过,也许我已经进入无招胜有招,博取众家之长、融会贯通的地步了。对于WebApi
- LottieLottie 是 Airbnb 开源的一个动画项目,它支持 iOS, mac OS Android RN,由于某些复杂动画的实现