zoukankan      html  css  js  c++  java
  • 保存文件到手机内存

    1.编写布局文件

    <?xml version="1.0" encoding="utf-8"?>

    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"

        android:layout_width="fill_parent"

        android:layout_height="fill_parent"

        android:orientation="vertical" >

        <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" >

            <requestFocus />

        </EditText>

        <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="wrap_content"

            android:layout_height="wrap_content" >

            <CheckBox

                android:id="@+id/cb_remember_psw"

                android:layout_width="wrap_content"

                android:layout_height="wrap_content"

                android:checked="true"

                android:text="记住密码" />

            <Button

                android:onClick="login"

                android:layout_alignParentRight="true"

                android:id="@+id/login"

                android:layout_width="wrap_content"

                android:layout_height="wrap_content"

                android:text="登录" />

        </RelativeLayout>

    </LinearLayout>

    2.编写Activity

    public class LoginUIActivity extends Activity {

    private EditText et_username = null;

    private EditText et_password = null;

    private CheckBox cb_remeber_password = null;

    @Override

    public void onCreate(Bundle savedInstanceState) {

    super.onCreate(savedInstanceState);

    setContentView(R.layout.main);

    this.et_username = (EditText) this.findViewById(R.id.et_username);

    this.et_password = (EditText) this.findViewById(R.id.et_password);

    this.cb_remeber_password = (CheckBox) this

    .findViewById(R.id.cb_remember_psw);

    HashMap<String, String> info = LoginService.getInfo(this);

    if(info != null) {

    this.et_username.setText(info.get("username"));

    this.et_password.setText(info.get("password"));

    }

    }

    public void login(View view) {

    String username = this.et_username.getText().toString().trim();

    String password = this.et_password.getText().toString().trim();

    if (TextUtils.isEmpty(username) || TextUtils.isEmpty(password)) {

    Toast.makeText(this, "用户名或密码不能为空", 0).show();

    } else {

    if (this.cb_remeber_password.isChecked()) {

    boolean result = LoginService.saveInfo(this, username, password);

    if(result) {

    Toast.makeText(this, "保存密码成功", 0).show();

    }

    }

    if ("weijie".equals(username) && "123".equals(password)) {

    Toast.makeText(this, "登录成功", 0).show();

    } else {

    Toast.makeText(this, "登录失败", 0).show();

    }

    }

    }

    }

    3.编写业务类

    public class LoginService {

    public static boolean saveInfo(Context context, String username,

    String password) {

     //getFileDir : /data/data/包名/files

    //getCacheDir : /data/data/包名/cache

    File file = new File(context.getFilesDir(), "info.txt");

    try {

    FileOutputStream fos = new FileOutputStream(file);

    fos.write((username + "=" + password).getBytes());

    fos.flush();

    fos.close();

    return true;

    } catch (Exception e) {

    e.printStackTrace();

    return false;

    }

    }

    public static HashMap<String, String> getInfo(Context context) {

    File file = new File(context.getFilesDir(), "info.txt");

    try {

    FileInputStream fis = new FileInputStream(file);

    BufferedReader br = new BufferedReader(new InputStreamReader(fis));

    String[] result = br.readLine().split("=");

    HashMap<String, String> map = new HashMap<String, String>();

    map.put("username", result[0]);

    map.put("password", result[1]);

    br.close();

    return map;

    } catch (Exception e) {

    Toast.makeText(context, "无法读取用户信息", 0).show();

    }

    return null;

    }

    }

  • 相关阅读:
    java的基本数据类型有八种:
    BZOJ 1003 [ZJOI2006]物流运输trans
    BZOJ 1002 [FJOI2007]轮状病毒
    BZOJ 1001 [BeiJing2006]狼抓兔子
    FFT修正
    主席树修正
    COJ 0995 WZJ的数据结构(负五)区间操作
    COJ 1010 WZJ的数据结构(十) 线段树区间操作
    COJ 0999 WZJ的数据结构(负一)
    AC自动机修正
  • 原文地址:https://www.cnblogs.com/freenovo/p/4469841.html
Copyright © 2011-2022 走看看