1. publicclassDataActivityextendsActivity{
  2. privateEditTextfilenameText;
  3. privateEditTextcontentText;
  4. privateTextViewresultView;
  5. privatestaticfinalStringTAG="DataActivity";
  6. /**Calledwhentheactivityisfirstcreated.*/
  7. @Override
  8. publicvoidonCreate(BundlesavedInstanceState){
  9. super.onCreate(savedInstanceState);
  10. setContentView(R.layout.main);
  11. filenameText=(EditText)this.findViewById(R.id.filename);
  12. contentText=(EditText)this.findViewById(R.id.content);
  13. resultView=(TextView)this.findViewById(R.id.result);
  14. Stringfilename=filenameText.getText().toString();
  15. Buttonbutton=(Button)this.findViewById(R.id.button);
  16. ButtonshowButton=(Button)this.findViewById(R.id.showButton);
  17. button.setOnClickListener(listener);
  18. showButton.setOnClickListener(listener);
  19. }
  20. privateView.OnClickListenerlistener=newView.OnClickListener(){
  21. @Override
  22. publicvoidonClick(Viewv){
  23. Buttonbutton=(Button)v;
  24. Stringfilename=filenameText.getText().toString();
  25. switch(button.getId()){
  26. caseR.id.button://如果是保存按钮
  27. intresId=R.string.success;
  28. Stringcontent=contentText.getText().toString();
  29. try{
  30. OutputStreamoutStream=DataActivity.this.openFileOutput(filename,Context.MODE_WORLD_WRITEABLE+Context.MODE_WORLD_READABLE);
  31. //四中操作模式
  32. //Context.MODE_PRIVATE=0覆盖、私有
  33. //Context.MODE_APPEND=32768追加、私有
  34. //Context.MODE_WORLD_READABLE=1其他的程序可以访问
  35. //Context.MODE_WORLD_WRITEABLE=2
  36. try{
  37. FileService.save(outStream,content);//保存文件
  38. }catch(Exceptione){
  39. Log.e(TAG,e.toString());
  40. resId=R.string.error;
  41. }
  42. }catch(FileNotFoundExceptione){
  43. Log.e(TAG,e.toString());
  44. resId=R.string.error;
  45. }
  46. Toast.makeText(DataActivity.this,resId,Toast.LENGTH_LONG).show();
  47. break;
  48. caseR.id.showButton://如果是显示按钮
  49. try{
  50. InputStreaminStream=DataActivity.this.openFileInput(filename);
  51. Stringtext=FileService.read(inStream);
  52. resultView.setText(text);
  53. }catch(Exceptione){
  54. Log.e(TAG,e.toString());
  55. resId=R.string.error;
  56. Toast.makeText(DataActivity.this,"读取失败",Toast.LENGTH_LONG).show();
  57. }
  58. break;
  59. }
  60. }
  61. };
  62. }

本文出自 “阿凡达” 博客,请务必保留此出处http://shamrock.blog.51cto.com/2079212/702531

更多相关文章

  1. Android自定义对话框的使用
  2. Android自定义对话框的使用
  3. Android自定义对话框的使用
  4. 小编程(三):用户登录注册界面开发及用户信息管理案例代码详解
  5. 我的Android进阶之旅------>Android视频录制小例子
  6. Android回调——Android模块化编程
  7. Android修行之路——Dialog
  8. Android(安卓)按钮添加单击事件
  9. Android官方入门文档[6]添加Action按钮

随机推荐

  1. Android检测系统中是否存在某进程
  2. 分享:android之hardwareAccelerated你不知
  3. android   点击屏幕让软件盘消失
  4. Android xmpp 资源列表
  5. Android 之如何删除eclipse自动生成的//T
  6. android - Jni 接口函数表
  7. 彻底解决android读取txt乱码终极方案
  8. Android获得手机分辨率
  9. Android读取SD卡.TXT文本内容
  10. Android Studio Smali 调试