布局
线性布局
- 有一个布局方向,水平或者竖直
- 在竖直布局下,左对齐、右对齐,水平居中生效
- 在水平布局下,顶部对齐、底部对齐、竖直居中生效
- 权重:按比例分配屏幕的剩余宽度或者高度
相对布局
- 组件默认位置都是左上角,组件之间可以重叠
- 可以相对于父元素上下左右对齐,相对于父元素,水平居中、竖直居中、水平竖直同时居中
- 可以相对于其他组件上下左右对齐
- 可以布局于其他组件的上方、下方、左边、右边
帧布局
- 组件默认位置都是左上角,组件之间可以重叠
- 可以设置上下左右对齐,水平竖直居中,设置方式与线性布局一样
表格布局
- 每有一个TableRow子节点表示一行,该子节点的每一个子节点都表示一列
- TableLayout的一级子节点默认宽都是匹配父元素
- TableRow的子节点默认宽高都是包裹内容
在内部存储中写文件
MainActivity.java
package com.dasudian.savepass;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.Toast;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
public class MainActivity extends AppCompatActivity {
EditText et_name;
EditText et_pass;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
et_name = (EditText) findViewById(R.id.et_name);
et_pass = (EditText) findViewById(R.id.et_pass);
readAcount();
}
// 读取保存的账号和密码
private void readAcount() {
File file = new File(getFilesDir(), "info.txt");
// 第一次打开app时,没有这个文件,所以需要先判断
if(file.exists()) {
try {
FileInputStream fis = new FileInputStream(file);
// 把字节流转换成为字符流
BufferedReader br = new BufferedReader(new InputStreamReader(fis));
// 读取文件里面的用户名和密码
String text = br.readLine();
String[] s = text.split("##");
et_name.setText(s[0]);
et_pass.setText(s[1]);
} catch (Exception e) {
e.printStackTrace();
}
}
}
public void login(View v) {
String name = et_name.getText().toString();
String pass = et_pass.getText().toString();
CheckBox cb_remember = (CheckBox) findViewById(R.id.cb_remember);
if (cb_remember.isChecked()) {
// 安装包下面的info.txt文件里面存储用户名和密码,不需要权限
// getFilesDie返回files目录路劲,该对象的路劲是:data/data/com.dasudian.savepass/files
File file = new File(getFilesDir(), "info.txt");
// getCacheDir返回缓存目录路劲,路劲为:data/data/com.dasudian.savepass/cache
// File file = File(getCacheDir(), "info.txt");
FileOutputStream fos = null;
try {
fos = new FileOutputStream(file);
fos.write((name + "##" + pass).getBytes());
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
fos.close();
} catch (IOException e) {
}
}
}
System.out.println("登录成功");
Toast.makeText(this, "登录成功", Toast.LENGTH_SHORT).show();
}
}
布局文件
<?xml version="1.0" encoding="utf-8"?>
<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.dasudian.savepass.MainActivity">
<EditText
android:id="@+id/et_name"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="请输入账号" />
<EditText
android:id="@+id/et_pass"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:inputType="textPassword"
android:hint="请输入密码" />
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<CheckBox
android:id="@+id/cb_remember"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:text="记住用户名和密码" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:text="登录"
android:onClick="login"/>
</RelativeLayout>
</LinearLayout>
在外部存储中读写文件
manifest
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.dasudian.savepass">
<!--写外部sdcard权限-->
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
布局文件
<?xml version="1.0" encoding="utf-8"?>
<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.dasudian.savepass.MainActivity">
<EditText
android:id="@+id/et_name"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="请输入账号" />
<EditText
android:id="@+id/et_pass"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:inputType="textPassword"
android:hint="请输入密码" />
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<CheckBox
android:id="@+id/cb_remember"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:text="记住用户名和密码" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:text="登录"
android:onClick="login"/>
</RelativeLayout>
</LinearLayout>
MainActivity.java
package com.dasudian.savepass;
import android.os.Bundle;
import android.os.Environment;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.Toast;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
public class MainActivity extends AppCompatActivity {
EditText et_name;
EditText et_pass;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
et_name = (EditText) findViewById(R.id.et_name);
et_pass = (EditText) findViewById(R.id.et_pass);
readAcount();
}
// 读取保存的账号和密码
private void readAcount() {
File file = new File(Environment.getExternalStorageDirectory(), "info.txt");
// 第一次打开app时,没有这个文件,所以需要先判断
if(file.exists()) {
try {
FileInputStream fis = new FileInputStream(file);
// 把字节流转换成为字符流
BufferedReader br = new BufferedReader(new InputStreamReader(fis));
// 读取文件里面的用户名和密码
String text = br.readLine();
String[] s = text.split("##");
et_name.setText(s[0]);
et_pass.setText(s[1]);
} catch (Exception e) {
e.printStackTrace();
}
}
}
public void login(View v) {
String name = et_name.getText().toString();
String pass = et_pass.getText().toString();
CheckBox cb_remember = (CheckBox) findViewById(R.id.cb_remember);
if (cb_remember.isChecked()) {
// 获取外部sdcard目录路劲,需要申请写sdcard权限
File file = new File(Environment.getExternalStorageDirectory(), "info.txt");
FileOutputStream fos = null;
try {
fos = new FileOutputStream(file);
fos.write((name + "##" + pass).getBytes());
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
fos.close();
} catch (IOException e) {
}
}
}
System.out.println("登录成功");
Toast.makeText(this, "登录成功", Toast.LENGTH_SHORT).show();
}
}
检测sdcard状态
//MEDIA_REMOVED:没有sdcard
//MEDIA_UNMOUNTED:sdcard没有挂载
//MEDIA_CHECKING:正在检测sdcard状态
//MEDIA_MOUNTED:sdcard挂载了并且可用
Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)
获取sdcard剩余空间
File path = Environment.getExternalStorageDirectory();
StatFs stat = new StatFs(path.getPath());
long blockSize;
long totalBlocks;
long availableBlocks;
// 高版本的方法
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR2) {
blockSize = stat.getBlockSizeLong();
totalBlocks = stat.getBlockCountLong();
availableBlocks = stat.getAvailableBlocksLong();
} else {
blockSize = stat.getBlockSize();
totalBlocks = stat.getBlockCount();
availableBlocks = stat.getAvailableBlocks();
}
TextView tv = (TextView) findViewById(R.id.tv);
tv.setText(formatSize(availableBlocks * blockSize));
private String formatSize(long size) {
return Formatter.formatFileSize(this, size);
}
文件访问权限
// 私有的
FileOutputStream fos = openFileOutput("info1.txt", MODE_PRIVATE);
fos.write("xxx".getBytes());
fos.close();
// 全局可读写,其它app也可以访问这个文件
FileOutputStream fos = openFileOutput("info2.txt", MODE_WORLD_READABLE | MODE_WORLD_WRITEABLE);
fos.write("ohohoho".getBytes());
fos.close();
SharedPreference
// 写数据到SharedPreferences,该数据会在包的相应目录下保存一个xml文件
SharedPreferences sp = getSharedPreferences("info", MODE_PRIVATE);
Editor ed = sp.edit();
ed.putString("name", name);
ed.putString("pass", pass);
ed.commit();
//读数据
SharedPreferences sp = getSharedPreferences("info", MODE_PRIVATE);
String name = sp.getString("name", "");
String pass = sp.getString("pass", "");
et_name.setText(name);
et_pass.setText(pass);
XmlSerializer生成xml文件
// 得到序列化对象
XmlSerializer xs = Xml.newSerializer();
// xml文件保存的路劲
File file = new File("sdcard/sms2.xml");
FileOutputStream fos = new FileOutputStream(file);
//enconding:XML文件的编码格式
xs.setOutput(fos, "utf-8");
//3.开始序列化
//enconding:第一行中enconding属性的值
xs.startDocument("utf-8", true);
xs.startTag(null, "message");
for (Message sms : smsList) {
xs.startTag(null, "sms");
xs.startTag(null, "body");
xs.text(sms.getBody() + "<body>");
xs.endTag(null, "body");
xs.startTag(null, "date");
xs.text(sms.getDate());
xs.endTag(null, "date");
xs.startTag(null, "type");
xs.text(sms.getType());
xs.endTag(null, "type");
xs.startTag(null, "address");
xs.text(sms.getAddress());
xs.endTag(null, "address");
xs.endTag(null, "sms");
}
xs.endTag(null, "message");
//结束序列化
xs.endDocument();
pull解析xml文件
public void click(View v){
//获取输入文件
InputStream is = getClassLoader().getResourceAsStream("weather.xml");
XmlPullParser xp = Xml.newPullParser();
try {
xp.setInput(is, "gbk");
int type = xp.getEventType();
City city = null;
while(type != XmlPullParser.END_DOCUMENT){
switch (type) {
case XmlPullParser.START_TAG:
if("weather".equals(xp.getName())){
cityList = new ArrayList<City>();
}
else if("city".equals(xp.getName())){
city = new City();
}
else if("name".equals(xp.getName())){
String name = xp.nextText();
city.setName(name);
}
else if("temp".equals(xp.getName())){
String temp = xp.nextText();
city.setTemp(temp);
}
else if("pm".equals(xp.getName())){
String pm = xp.nextText();
city.setPm(pm);
}
break;
case XmlPullParser.END_TAG:
if("city".equals(xp.getName())){
cityList.add(city);
}
break;
}
type = xp.next();
}
for (City c : cityList) {
System.out.println(c.toString());
}
} catch (Exception e) {
e.printStackTrace();
}
}