第一步:建立一个新工程 :登陆界面
第二步:布局文件:
第三步:编辑逻辑类
package com.xuxin.test.filewriteread;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import android.support.v7.app.ActionBarActivity;
import android.support.v7.app.ActionBar;
import android.support.v4.app.Fragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;
import android.os.Build;
import android.provider.ContactsContract.CommonDataKinds.Note;
public class MainActivity extends ActionBarActivity implements OnClickListener {
EditText et;
Button bt;
private EditText inputArt;
private Button saveButton;
private String Text_of_input;
private OutputStream os;
private TextView showmyText;
private Button openTxt,cleanTxt;
private String Text_of_output;
private InputStream is;
private byte[] b;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
UIinit("main");
Logic("main");
}
/*
* 菜单栏选择操作(每次选择菜单后,对界面进行初始化和事件逻辑方法)
* */
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// TODO Auto-generated method stub
switch (item.getItemId()) {
case 1:
setLayoutShow(R.layout.activity_main);
UIinit("main");
Logic("main");
NoteDebug("编辑文件Layout");
break;
case 2:
setLayoutShow(R.layout.open);
UIinit("open");
Logic("open");
NoteDebug("打开文件Layout");
break;
case 3:
finish();
NoteDebug("Byebye");
break;
default:
break;
}
return super.onOptionsItemSelected(item);
}
private void setLayoutShow(int layoutID) {
// TODO Auto-generated method stub
setContentView(layoutID);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// TODO Auto-generated method stub
menu.add(0, 1, 1, "Edit").setIcon(R.drawable.ic_launcher);
menu.add(0, 2, 2, "Open").setIcon(R.drawable.ic_launcher);
menu.add(0, 3, 3, "Exit").setIcon(R.drawable.ic_launcher);
return super.onCreateOptionsMenu(menu);
}
/**
添加事件逻辑方法
*
* */
private void Logic(String string) {
// TODO Auto-generated method stub
if(string.equals("main")){
saveButton.setOnClickListener(this);
} else if(string.equals("open")) {
openTxt.setOnClickListener(this);
cleanTxt.setOnClickListener(this);
}
}
/**
初始化UI元素方法
*
* */
private void UIinit(String mainROopen) {
// TODO Auto-generated method stub
if(mainROopen.equals("main")){
inputArt = (EditText) findViewById(R.id.et);
saveButton= (Button) findViewById(R.id.bt);
}
else if(mainROopen.equals("open")){
showmyText= (TextView) findViewById(R.id.tv_open_show);
openTxt = (Button) findViewById(R.id.bt_open);
cleanTxt = (Button) findViewById(R.id.bt_clean);
}
}
/**
按钮操作处理
*
*/
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
switch (v.getId()) {
case R.id.bt:{
NoteDebug("文件保存");
Text_of_input = inputArt.getText().toString();
try {
os = this.openFileOutput("txtME", MODE_PRIVATE);
os.write(Text_of_input.getBytes());
} catch (FileNotFoundException e1) {
// TODO Auto-generated catch block
NoteDebug("文件关闭失败"+e1);;
}
catch (IOException e2) {
// TODO Auto-generated catch block
NoteDebug("文件写入错误"+e2);;
}
finally{
try {
os.close();
} catch (IOException e) {
// TODO Auto-generated catch block
NoteDebug("文件关闭失败"+e);
}
}
inputArt.setText("");
}
break;
case R.id.bt_open:{
NoteDebug("文件打开");
try {
is = this.openFileInput("txtME");
b = new byte[1024];
int length = is.read(b);
Text_of_output = new String(b);
setTitle("文件字数:"+length);
showmyText.setText(Text_of_output);
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
NoteDebug("文件打开失败"+e);
} catch (IOException e) {
// TODO Auto-generated catch block
NoteDebug("文件读取失败"+e);
}
finally{
try {
is.close();
} catch (IOException e) {
// TODO Auto-generated catch block
NoteDebug("文件关闭失败"+e);
}
}
}break;
case R.id.bt_clean:{
showmyText.setText("");
NoteDebug("清空");
}break;
default:
break;
}
}
private void NoteDebug(String showString) {
// TODO Auto-generated method stub
Toast.makeText(getBaseContext(), showString, Toast.LENGTH_SHORT).show();
}
}