目的:打开文件,查看文件内容,情况内容,追加内容。
1.创建android project
2.activity内容:
package com.android;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;
public class Activity12 extends Activity implements OnClickListener{
private EditText inputEt;//编辑框
private Button saveBtn;//保存 按钮
private String input_text;//输入的字符串
private OutputStream os;
private TextView showText; //显示读取的内容
private Button openTxtBtn; //open file button
private Button cleanTxtBtn;//clean file
private String text_output;
private InputStream is;
private byte[] b;
private final String FILE = "filetext.txt";
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.show);
}
/**
* 设置显示view
* @param layoutId
*/
private void setLayout(int layoutId) {
setContentView(layoutId);
}
/**
* 各种布局下,得到view
* @param mainOpen
*/
private void initUI(String mainOpen) {
if (mainOpen.equals("main")) {
inputEt = (EditText)findViewById(R.id.edit_txt);
saveBtn = (Button)findViewById(R.id.save);
} else if (mainOpen.equals("open")) {
showText = (TextView)findViewById(R.id.showTxt);
openTxtBtn = (Button)findViewById(R.id.openTxtB);
cleanTxtBtn = (Button)findViewById(R.id.cleanBtn);
}
}
/**
* 不同布局下添加自己的Event
* @param str
*/
private void addEvent(String str) {
if (str.equals("main")) {
saveBtn.setOnClickListener(this);
} else if (str.equals("open")) {
openTxtBtn.setOnClickListener(this);
cleanTxtBtn.setOnClickListener(this);
}
}
/* (non-Javadoc)
* @see android.view.View.OnClickListener#onClick(android.view.View)
*/
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
switch (v.getId()) {
case R.id.save:{
input_text = inputEt.getText().toString();
try {
os = this.openFileOutput(FILE, MODE_PRIVATE);
os.write(input_text.getBytes());
} catch(FileNotFoundException e) {
toast(getString(R.string.fileCloseError) + e);
} catch(IOException e) {
toast("文件写入失败" + e);
} finally {
try {
os.close();
} catch(Exception e) {
toast(getString(R.string.fileCloseError) + e);
}
}
inputEt.setText("");
toast("文件保存成功!!!查看请点击Menu");
}
break;
case R.id.openTxtB: {
toast("文件打开");
try {
is = this.openFileInput(FILE);
//is = getResources().openRawResource(R.raw.filetext);
b = new byte[1024];
int length = is.read(b);
text_output = new String(b);
setTitle("文件字数 " + length);
showText.setText(text_output);
} catch(FileNotFoundException e) {
toast("文件打开失败" + e);
} catch(IOException e) {
toast("文件读取失败" + e);
} finally {
try {
is.close();
} catch(Exception e) {
toast(getString(R.string.fileCloseError) + e);
}
}
}
break;
case R.id.cleanBtn: {
showText.setText("");
toast(getString(R.string.clean));
}
break;
default:
break;
}
}
/* 增加menu
* @see android.app.Activity#onCreateOptionsMenu(android.view.Menu)
*/
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// TODO Auto-generated method stub
menu.add(0, 1, 1, "Edit").setIcon(R.drawable.edit);
menu.add(0, 2, 2, "Open").setIcon(R.drawable.open);
menu.add(0, 3, 3, "Exit").setIcon(R.drawable.exit);
return super.onCreateOptionsMenu(menu);
}
/* 点击menu item
* @see android.app.Activity#onOptionsItemSelected(android.view.MenuItem)
*/
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// TODO Auto-generated method stub
switch (item.getItemId()) {
case 1:
setLayout(R.layout.main);
initUI("main");
addEvent("main");
setTitle("请输入追加的内容:");
toast("编辑文件");
break;
case 2:
setLayout(R.layout.open);
initUI("open");
addEvent("open");
toast("打开文件");
break;
case 3:
finish();
toast("Byebye");
break;
default:
break;
}
return super.onOptionsItemSelected(item);
}
/**
* 提示
* @param str 内容
*/
private void toast(String str) {
Toast.makeText(this, str, Toast.LENGTH_SHORT).show();
}
}
3.布局文件
main.xml 追加文件内容填写页,一个editText 一个button
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent" android:background="@color/hui" > <EditText android:id="@+id/edit_txt" android:layout_width="350px" android:layout_height="350px" /> <Button android:layout_height="wrap_content" android:id="@+id/save" android:text="保存" android:layout_width="80px" /> </LinearLayout>
show.xml 首页
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent" android:background="@color/hui" > <TextView android:id="@+id/ss" android:layout_width="wrap_content" android:layout_height="wrap_content" android:textSize="30px" android:text="粗糙文件编辑,请点击Menu" /> </LinearLayout>
open.xml 操作文件
<?xml version="1.0" encoding="utf-8"?> <AbsoluteLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent" android:id="@+id/openlayout" android:background="@color/hui" > <TextView android:id="@+id/showTxt" android:layout_width="314px" android:layout_height="373px" android:layout_x="3px" android:layout_y="3px" /> <Button android:layout_height="wrap_content" android:id="@+id/openTxtB" android:text="打开" android:layout_width="80px" android:layout_x="2px" android:layout_y="378px" /> <Button android:layout_height="wrap_content" android:id="@+id/cleanBtn" android:text="清空" android:layout_width="80px" android:layout_x="239px" android:layout_y="378px" /> </AbsoluteLayout>
4.string.xml
<?xml version="1.0" encoding="utf-8"?> <resources> <string name="app_name">12</string> <string name="fileCloseError">文件关闭失败</string> <string name="clean">清空</string> <color name = "hui">#FFFACD</color> </resources>
结果:
首页
点menu
点open
点edit
点exit
明天继续完善。美化。。。。
byebye!!!