Android入门之login设计
效果图:
MainActivity.java
package jk.quickpay.login; import jk.quickpay.login.FileService;
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; public class MainActivity extends Activity { public EditText edit_name, edit_pass;
public Button btn_login;
public CheckBox box_remeber;
public FileService fileService; protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
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"));
}
} public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.main, menu);
return true;
} class MyOnClickListener implements View.OnClickListener { 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(MainActivity.this, "用户名不能为空",
Toast.LENGTH_SHORT).show();
return;
} else if (TextUtils.isEmpty(pass)) {
Toast.makeText(MainActivity.this, "密码不能为空",
Toast.LENGTH_SHORT).show();
return;
} else {
if (box_remeber.isChecked()) {
MainActivity.this.fileService.saveToRom(name, pass,
"private.txt");
Toast.makeText(MainActivity.this, "用户名和密码已保存",
Toast.LENGTH_SHORT).show();
} else {
Toast.makeText(MainActivity.this, "用户名和密码不需要保存",
Toast.LENGTH_SHORT).show();
}
}
break;
default:
break;
}
}
}
}
FileService.java
package jk.quickpay.login; import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.util.HashMap;
import java.util.Map; import jk.quickpay.login.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 jk.quickpay.login; 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;
}
}
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: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_account"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/account" /> <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/password" /> <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"
android:orientation="horizontal"> <Button
android:id="@+id/btn_login"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="0.1"
android:text="@string/login" />
<Button
android:id="@+id/btn_reg"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="0.1"
android:text="@string/register" />
<Button
android:id="@+id/btn_out"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="0.1"
android:text="@string/quit" /> </LinearLayout> <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>
Android入门之login设计的更多相关文章
- Android基础-系统架构分析,环境搭建,下载Android Studio,AndroidDevTools,Git使用教程,Github入门,界面设计介绍
系统架构分析 Android体系结构 安卓结构有四大层,五个部分,Android分四层为: 应用层(Applications),应用框架层(Application Framework),系统运行层(L ...
- android 入门 005(登录记住)
android 入门 005(登录记住) <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android ...
- Android入门教程之我见
真正的从安卓入门学习到实际工作也差不多一年时间了,也做了几个项目.在这期间经历了一开始学习Android的基本知识后仍旧无从下手,不知道如何开始开发一个app,到现在也开始学会注意Android架构的 ...
- zxing学习笔记 android入门
对于刚开始学习android开发的童鞋们来说,若有一个简单而又全面的android工程能来剖析,那就是再好不过了,zxing就是不错得例子. zxing的源码可以到google code上下载, ...
- [转]Android App整体架构设计的思考
1. 架构设计的目的 对程序进行架构设计的原因,归根到底是为了提高生产力.通过设计使程序模块化,做到模块内部的高聚合和模块之间的低耦合.这样做的好处是使得程序在开发的过程中,开发人员只需要专注于一点, ...
- Android入门(十二)SQLite事务、升级数据库
原文链接:http://www.orlion.ga/610/ 一.事务 SQLite支持事务,看一下Android如何使用事务:比如 Book表中的数据都已经很老了,现在准备全部废弃掉替换成新数据,可 ...
- Android App的架构设计:从VM、MVC、MVP到MVVM
随着Android应用开发规模的扩大,客户端业务逻辑也越来越复杂,已然不是简单的数据展示了.如同后端开发遇到瓶颈时采用的组件拆分思想,客户端也需要进行架构设计,拆分视图和数据,解除模块之间的耦合,提高 ...
- 【转】Xamarin.Android 入门之:Xamarin+vs2015 环境搭建
Xamarin.Android 入门之:Xamarin+vs2015 环境搭建 一.前言 此篇博客主要写了如何使用搭建xamarin开发的环境,防止我自己万一哪天电脑重装系统了,可以直接看这篇博客 ...
- android 入门 006(sqlite增删改查)
android 入门 006(sqlite增删改查) package cn.rfvip.feb_14_2_sqlite; import android.content.Context; import ...
随机推荐
- HttpURLConnection中使用代理(Proxy)及其验证(Authentication)
HttpURLConnection中使用代理(Proxy)及其验证(Authentication) 使用Java的HttpURLConnection类可以实现HttpClient的功能,而不需要依赖任 ...
- 在 Visual Studio 调试器中指定符号 (.pdb) 和源文件
查找并指定符号文件和源文件:指定符号加载行为.使用符号和源服务器上:加载符号自动或在要求. 内容 查找符号 (.pdb) 文件 查找源文件 查找符号 (.pdb) 文件 说明 在之前的 Vis ...
- CEGUI 输入法窗口实现
游戏中经常要输入汉字,但当我们游戏没有自己实现输入法窗口时,windows会使用用户安装的输入法,但这个输入法窗口只会显示在游戏窗口外头,而且当我们游戏全屏时(真全屏,不是那种窗口式的假全屏),屏幕上 ...
- Swift - final关键字的介绍,以及使用场景
final关键字在大多数的编程语言中都存在,表示不允许对其修饰的内容进行继承或者重新操作.Swift中,final关键字可以在class.func和var前修饰. 通常大家都认为使用final可以更好 ...
- 1352 - Colored Cubes (枚举方法)
There are several colored cubes. All of them are of the same size but they may be colored differentl ...
- Qt控件精讲一:按钮
原地址:http://blog.csdn.net/yuxikuo_1/article/details/17397109 Qt Creater提供6种Button控件.如图1. Button控件介绍 控 ...
- BaseActivity--上门啦
package com.fwpt.activity; import com.fwpt.entity.RyUserInfo; import com.fwpt.entity.SmlaUserinfo; i ...
- [每日一题] OCP1z0-047 :2013-08-28 DELETE..........................................................160
转载请注明出处:http://blog.csdn.net/guoyjoe/article/details/10475707 正确答案:ACD 根据题库,操作如下: A答案能删除: oe@OCM> ...
- UVA 10494-If We Were a Child Again(一流的塔尔苏斯)
Problem C If We Were a Child Again Input: standard input Output: standard output seconds "Oo ...
- kernel hexdump分析
驱动调试中,很多时候是二进制的,这个时候hexdump就是个非常有用的工具了. 不要再自己去实现类似的功能,kernel代码里面就有: 参考: kernel/lib/hexdump.c // 0Xxx ...