Android :内部文件中写操作
来源:互联网 发布:美国人常用的软件 编辑:程序博客网 时间:2024/05/17 08:04
在内部文件存储的写操作中与java中文件读写类似,都需要用到文件的输入输出流。
界面设计
布局代码
<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" android:background="#99ccff" > <EditText android:layout_width="match_parent" android:layout_height="wrap_content" android:hint="请输入用户名" android:id="@+id/et_username" /> <EditText android:layout_width="match_parent" android:layout_height="wrap_content" android:hint="请输入密码" android:password="true" android:id="@+id/et_password" /> <RelativeLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="horizontal" > <CheckBox android:id="@+id/cb" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="记住用户名和密码" android:layout_centerVertical="true" /> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="登录" android:layout_alignParentRight="true" android:onClick="login" /> </RelativeLayout> <LinearLayout android:layout_gravity="bottom" android:layout_width="match_parent" android:layout_height="match_parent" > <ImageView android:layout_width="match_parent" android:layout_height="match_parent" android:layout_gravity="bottom" android:src="@drawable/nv" /> </LinearLayout> </LinearLayout>
main Activity代码
package com.example.instorage;import java.io.File;import java.io.FileNotFoundException;import java.io.FileOutputStream;import java.io.IOException;import android.app.Activity;import android.os.Bundle;import android.view.View;import android.widget.CheckBox;import android.widget.Checkable;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.instorage); } public void login (View v){ EditText et_username=(EditText) findViewById(R.id.et_username); EditText et_password=(EditText) findViewById(R.id.et_password); String name=et_username.getText().toString(); String password=et_password.getText().toString(); CheckBox cb=(CheckBox) findViewById(R.id.cb); //判断选框有没有被勾选,调用isChecked() if(cb.isChecked()){ //这就是内部存储空间的路径 //先进行部署,之后通过 Window-show View-other-File Explorer-data/data下找到相应包名的文件 File file=new File("data/data/com.example.instorage/info.txt"); FileOutputStream os; try { os = new FileOutputStream(file); String buffer=name+"##"+password; os.write(buffer.getBytes()); os.close();//记得关闭文件 } catch (FileNotFoundException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } Toast.makeText(MainActivity.this,"登陆成功",Toast.LENGTH_SHORT).show(); }}
学习注意点
1、在布局文件中的几种布局方式的运用
2、布局文件中check Box的使用方法
3、Button的onClick属性直接使用时需要在main activity中写入此方法
4、main activity中对check box勾选框的检测,isChecked()
5、main activity中文件的输入输出流
0 0
- Android :内部文件中写操作
- android中写文件到内部存储中
- android 向内部存储写文件
- Android 文件操作 读/写
- Android中外部,内部文件读写
- Android 内部存储中读写文件
- 向android中写文件
- Android中操作文件
- 往内部存储写文件
- Android文件读写操作(assets 文件、 raw文件、内部存储文件、外部存储文件)
- android 内部文件读取
- android 内部文件读取
- Android 内部文件存储
- 文件操作 写文件
- C实现文件内部操作
- eclipse中如何查看一个android模拟器的内部文件
- Android中读写文件操作
- Android写bitmap对象到文件中
- day08
- Leetcode: Maximum Gap
- Redhat7安装Madagascar简易教程
- android viewinject 反射 注解 IOC
- 对自己在社会中位置的认识
- Android :内部文件中写操作
- 数组及多维数组
- JSP 笔记
- MongoDB的基本使用(二)
- jQuery自定义动画
- Google APAC 2016 University Graduates Test Round A
- mac pro genymotion 无法启动
- linux系统关闭ipv6
- mysql前缀索引