首页 > 代码库 > android 简单文件操作
android 简单文件操作
1.布局
<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" 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="com.example.logindemo.MainActivity" > <TextView android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="请输入用户名" /> <EditText android:layout_width="fill_parent" android:layout_height="wrap_content" android:id="@+id/et_username" /> <TextView android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="请输入密码" /> <EditText android:inputType="textPassword" android:layout_width="fill_parent" android:layout_height="wrap_content" android:id="@+id/et_password" /> <RelativeLayout android:layout_width="fill_parent" android:layout_height="wrap_content" > <CheckBox android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/cb_remember" android:text="记住密码" android:checked="true" /> <Button android:onClick="login" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentRight="true" android:text="登录" android:id="@+id/btn_login" /> </RelativeLayout></LinearLayout>
2.MainActivity.java
package com.example.logindemo;import java.util.Map;import android.os.Bundle;import android.support.v7.app.ActionBarActivity;import android.text.TextUtils;import android.view.Menu;import android.view.MenuItem;import android.view.View;import android.widget.CheckBox;import android.widget.EditText;import android.widget.Toast;import com.example.logindemo.service.LoginService;public class MainActivity extends ActionBarActivity { private EditText et_username; private EditText et_pwd; private CheckBox cb_remember; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); et_username = (EditText)this.findViewById(R.id.et_username); et_pwd = (EditText)this.findViewById(R.id.et_password); cb_remember = (CheckBox)this.findViewById(R.id.cb_remember); Map<String,String> map = LoginService.getUserInfo(this); if(map!=null) { et_username.setText(map.get("username")); et_pwd.setText(map.get("password")); } } public void login(View v){ String username = et_username.getText().toString(); String pwd = et_pwd.getText().toString(); if(TextUtils.isEmpty(username) || TextUtils.isEmpty(pwd)){ Toast.makeText(MainActivity.this, "username or password is empty", Toast.LENGTH_SHORT).show(); return; } else { if(cb_remember.isChecked()) { boolean result = LoginService.saveUserInfo(this, username, pwd); if(result) { Toast.makeText(MainActivity.this, "save user info success", Toast.LENGTH_SHORT).show(); } } if("test".equals(username) && "123".equals(pwd)) { Toast.makeText(MainActivity.this, "login success", Toast.LENGTH_SHORT).show(); } else { Toast.makeText(MainActivity.this, "username or password is error", Toast.LENGTH_SHORT).show(); } } }}
3.service
package com.example.logindemo.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 { public static boolean saveUserInfo(Context context,String username,String pwd) { try { File file = new File(context.getFilesDir(),"info.txt"); FileOutputStream fos = new FileOutputStream(file); fos.write((username+"##"+pwd).getBytes()); fos.close(); return true; } catch(Exception e) { e.printStackTrace(); return false; } } public static Map<String,String> getUserInfo(Context context) { try { File file = new File(context.getFilesDir(),"info.txt"); FileInputStream fis = new FileInputStream(file); BufferedReader br = new BufferedReader(new InputStreamReader(fis)); String strs = br.readLine(); String arr[] = strs.split("##"); Map<String,String> map = new HashMap<String,String>(); map.put("username", arr[0]); map.put("password", arr[1]); return map; } catch(Exception e) { e.printStackTrace(); return null; } }}
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。