内部存储文件(写)

为了后续项目使用,先把文件写的代码贴在这里,备用:

 

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:orientation="vertical"
    tools:context="com.swust.intern.MainActivity" >
 <!-- ~~~~~~~~~~~~~~~~~~~~~ 第一行 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~ -->
 <LinearLayout 
     android:layout_width="match_parent"
    android:layout_height="50dp"
     >
      <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="账    号:" />
     <EditText 
        android:hint="输入用户名"
        android:id="@+id/et_name"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        />
     
 </LinearLayout>
  <!-- ~~~~~~~~~~~~~~~~~~~~~ 第二行密码是  密文显示 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~ -->
    <LinearLayout
    android:layout_width="match_parent"
    android:layout_height="50dp"
         >
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="密    码:" />
    <EditText 
        android:hint="输入密码"
        android:id="@+id/et_pwd"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:inputType="textPassword"
        />
     </LinearLayout>
     <!-- ~~~~~~~~~~~~~~~~~~~~~ 第三行:相对布局 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~ -->
     <!-- 复选框是相对于RelativeLayout的垂直居中 -->
    <RelativeLayout
    android:layout_width="match_parent"
    android:layout_height="50dp"
         >
    <CheckBox
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="记住用户名和密码" 
         android:id="@+id/cb"
        android:layout_centerVertical="true"/>
    <Button 
        android:text="登  陆"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="right"
        android:layout_alignParentRight="true"
        android:onClick="login"
        />
     </RelativeLayout>
</LinearLayout>

后台逻辑代码:

MainActivity.java代码:

package com.swust.intern;

import java.io.File;
import java.io.FileOutputStream;

import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.Toast;

public class MainActivity extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    }

    public void login(View v){
        EditText et_name = (EditText) findViewById(R.id.et_name);
        EditText et_pwd  = (EditText) findViewById(R.id.et_pwd);
        CheckBox cb      = (CheckBox)findViewById(R.id.cb);
        String name=et_name.getText().toString();
        String pwd =et_pwd.getText().toString();
        //判读复选框是否被勾选
        if(cb.isChecked()){
        //"data/data/com.swust.intern"是内部存储空间路径
            File file =new File("data/data/com.swust.intern/info.txt");
            FileOutputStream fos;
            try{
                fos = new FileOutputStream(file);
                //加“##”是为了读取时好分割
                fos.write((name + "##" +pwd).getBytes());
                fos.close();
            }catch (Exception e){
                e.printStackTrace();
            }
        }
        //System.out.println("登陆成功");
        //创建吐司对话框
        /* 第一个参数上context,而activity本来就是context的子类,所以直接填this*/
        /*第三个参数为持续显示时间,只有LENGTH_SHORT(2S)和LENGTH_LONG(5s)两种选择*/
        /*Toast t= Toast.makeText(this, "登陆成功", Toast.LENGTH_SHORT);
        //显示吐司对话框
        t.show();*/
        
        Toast.makeText(this, "登陆成功", Toast.LENGTH_SHORT).show();
    }
}

效果是在这里查看并导出:

posted @ 2015-10-14 10:22  平常心,平常心  阅读(200)  评论(0编辑  收藏  举报