标签:android
设计界面:
<LinearLayoutxmlns: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="wrap_content"
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="wrap_content"
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="fill_parent"
android:layout_height="wrap_content"
>
<CheckBox
android:id="@+id/cb_rember_pwd"
android:checked="true"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="记住密码"
/>
<Button
android:layout_alignParentRight="true"
android:onClick="login"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="登陆"
></Button>
</RelativeLayout>
</LinearLayout>
编写数据处理方法:
package com.cloud.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
*/
public static boolean saveUserInfo(Context context,String username,String password){
try {
//File file=newFile("/data/data/com.cloud.login/info.txt");
//context.getFilesDir();//返回一个目录
File file=newFile(context.getFilesDir(),"info.txt");
FileOutputStreamfos=new FileOutputStream(file);
fos.write((username+"##"+password).getBytes());
fos.close();
return true;
} catch (Exception e) {
// TODOAuto-generated catch block
e.printStackTrace();
return false;
}
}
//这里可以设置文件的访问权限
/*
public static boolean saveUserInfo(Context context, Stringusername,String password,
int mode){
try {
//File file=newFile("/data/data/com.cloud.login/info.txt");
//context.getFilesDir();//返回一个目录
FileOutputStream fos=null;
//=context.openFileOutput("private.txt",context.MODE_PRIVATE);
//File file=newFile(context.getFilesDir(),"info.txt");
//FileOutputStream fos=newFileOutputStream(file);
switch (mode) {
case 1:
fos=context.openFileOutput("private.txt",context.MODE_PRIVATE);
break;
case 2:
fos=context.openFileOutput("private.txt",context.MODE_WORLD_READABLE);
break;
case 3:
fos=context.openFileOutput("private.txt",context.MODE_WORLD_WRITEABLE);
break;
case 4:
fos=context.openFileOutput("private.txt",context.MODE_WORLD_READABLE+
context.MODE_WORLD_WRITEABLE);
break;
}
fos.write((username+"##"+password).getBytes());
fos.close();
return true;
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
return false;
}
}
*/
/**
* 获取保存的数据
* @param context
* @return
*/
publicstatic Map<String,String> getSaveUserInfo(Context context){
Filefile=new File(context.getFilesDir(),"info.txt");
try{
FileInputStreamfis=new FileInputStream(file);
BufferedReaderbr=new BufferedReader(new InputStreamReader(fis));
Stringstr=br.readLine();
String[]infos=str.split("##");
Map<String,String>map=new HashMap<String, String>();
map.put("username",infos[0]);
map.put("password",infos[1]);
returnmap;
}catch (Exception e) {
//TODO Auto-generated catch block
e.printStackTrace();
returnnull;
}
}
}
MainActivity.java代码
package com.cloud.login;
import java.util.Map;
import com.cloud.login.Service.LoginService;
import android.os.Bundle;
import android.app.Activity;
import android.text.TextUtils;
import android.util.Log;
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;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//必须在布局文件下面初始化控件
et_username=(EditText) findViewById(R.id.et_username);
et_password=(EditText) findViewById(R.id.et_password);
cb=(CheckBox) findViewById(R.id.cb_rember_pwd);
//检查是否有保存用户名和密码,如果有这回显
Map<String, String> map=LoginService.getSaveUserInfo(this);
if(map!=null){
et_username.setText(map.get("username"));
et_password.setText(map.get("password"));
}
}
//登陆按钮的点击事件
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, "需要保存用户名和密码!");
/*
//判断访问类型
booleanresult=false;
intid=rg_mode.getCheckedRadioButtonId();
switch(id) {
caseR.id.rd_private:
result=LoginService.saveUserInfo(this,username, password,1);
break;
caseR.id.rd_readable:
result=LoginService.saveUserInfo(this,username, password,2);
break;
caseR.id.rd_writeable:
result=LoginService.saveUserInfo(this,username, password,3);
break;
caseR.id.rd_public:
result=LoginService.saveUserInfo(this,username, password,4);
break;
default:
break;
}
*/
boolean result=LoginService.saveUserInfo(this,username,password);
if(result){
Toast.makeText(this, "信息保存成功!", Toast.LENGTH_SHORT).show();
}
}
//验证用户名和密码是否正确
if("spring".equals(username)&&"123".equals(password)){
Toast.makeText(this, "登陆成功!", Toast.LENGTH_SHORT).show();
}else{
Toast.makeText(this, "登陆失败!", Toast.LENGTH_SHORT).show();
}
}
}
}
版权声明:博主原创文章,转载请说明出处。http://blog.csdn.net/dzy21
标签:android
原文地址:http://blog.csdn.net/dzy21/article/details/47378313