android版txt电子阅读器移动开发

闲来自己动手开发了个电子阅读器,算是功能比较简单,阅读txt文档,可以添加书签,搜索。包括:1、可以打开本地文件夹去找到要的书;2、设定背景颜色,字体等等。

这里简要的介绍一下其中的核心代码:

(1)自动滚屏:

 

[java][/java] view plaincopy

  1. Handler autoScrollHandler = new Handler()
  2.     {
  3.         @Override
  4.         public void handleMessage(Message msg) {
  5.             super.handleMessage(msg);
  6.             switch(msg.what)
  7.             {
  8.             case BEGIN_SCROLL:
[java][/java] view plaincopy

  1.                               //需要判断是否已经滚动到最后了
  2.         if( tvMain.getScrollY()>= tvMain.getLineCount()*tvMain.getLineHeight()-tvMain.getHeight())
  3.         {
  4.             tvMain.scrollTo(0, tvMain.getLineCount()*tvMain.getLineHeight()-tvMain.getHeight());
  5.             autoScrollHandler.sendEmptyMessage(STOP_SCROLL);
  6.         }
  7.         else
  8.         {
  9.             tvMain.scrollTo(0, tvMain.getScrollY() + STOP_SCROLL);
  10.             autoScrollHandler.sendEmptyMessageDelayed(BEGIN_SCROLL, 100);
  11.         }
  12.         break;
  13.     case END_SCROLL:
  14.         autoScrollHandler.removeMessages(END_SCROLL);
  15.         autoScrollHandler.removeMessages(BEGIN_SCROLL);
  16.     case STOP_SCROLL:
  17.         autoScrollHandler.removeMessages(BEGIN_SCROLL);
  18.         autoScrollHandler.removeMessages(STOP_SCROLL);
  19.         break;
  20.     }
  21. }
  22. ;

(2)读书过程中,可以选中进行操作:发短信、打电话

 

 

[java][/java] view plaincopy

  1.        /**
  2.  * 用户选择了文本之后,让用户选择要发短信还是打电话
  3.  */
  4. @Override
  5. protected Dialog onCreateDialog(int id)
  6. {
  7.     if( id ==  DIALOG_AFTER_SELECTION)
  8.     {
  9.         return new AlertDialog.Builder(ReadBookActivity.this)
  10.             .setIcon(android.R.drawable.ic_dialog_info)
  11.             .setMessage(“您想用选定的文本:”)
  12.             .setPositiveButton(“发送短信”, new android.content.DialogInterface.OnClickListener() {
  13.                 @Override
  14.                 public void onClick(DialogInterface dialog, int which) {
  15.                     Uri smsUri=Uri.parse(“smsto://”);
  16.                     Intent smsIntent = new Intent(Intent.ACTION_SENDTO, smsUri);
  17.                     ClipboardManager cm=(ClipboardManager)getSystemService(Context.CLIPBOARD_SERVICE);
  18.                     cm.setText(strSelection);
  19.                     startActivity(smsIntent);
  20.                     Toast.makeText(ReadBookActivity.this, “短信内容已复制到剪贴板”,
  21.                                Toast.LENGTH_LONG).show();
  22.                     tvMain.clearSelection();
  23.                 }
  24.             })
  25.             .setNegativeButton(“拨打电话”, new android.content.DialogInterface.OnClickListener(){
  26.                 @Override
  27.                 public void onClick(DialogInterface dialog, int which) {
  28.                     if( PhoneNumberUtils.isGlobalPhoneNumber(strSelection))
  29.                     {
  30.                         Intent phoneIntent=new Intent(Intent.ACTION_CALL,Uri.parse(“tel://”+strSelection));
  31.                         startActivity(phoneIntent);
  32.                     }
  33.                     else
  34.                     {
  35.                         Toast.makeText(ReadBookActivity.this, “非法的电话号码”,
  36.                                 Toast.LENGTH_LONG).show();
  37.                     }
  38.                     tvMain.clearSelection();
  39.                 }
  40.             })
  41.             .setNegativeButton(“取消选择”, new android.content.DialogInterface.OnClickListener() {
  42.                 @Override
  43.                 public void onClick(DialogInterface dialog, int which) {
  44.                     tvMain.clearSelection();
  45.                 }
  46.             })
  47.             .create();
  48.     }
  49.     return null;
  50. }

 

(3)遍历文件夹查找对应的文件夹或者文件

 

[java][/java] view plaincopy

  1. /**
  2.  * 列出当前目录下的文件
  3.  * @param currDict
  4.  */
  5. private void listCurrDictFiles(File currDict)
  6. {
  7.     fileAdapter.clearItems();
  8.     fileAdapter.notifyDataSetChanged();
  9.     fileListview.postInvalidate();
  10.     if( !currDict.getPath().equals(“/sdcard”))
  11.     {
  12.         FileListItem item=new FileListItem();
  13.         item.name=”返回上级”;
  14.         item.type=FileType_Folder;
  15.         fileAdapter.addItem(item);
  16.     }
  17.     for(File f:currDict.listFiles() )
  18.     {
  19.         FileListItem item=new FileListItem();
  20.         item.name=f.getName();
  21.         if( f.isDirectory() && f.isHidden()==false)
  22.         {
  23.             item.type=FileType_Folder;
  24.             item.dirName=f.getPath();
  25.             fileAdapter.addItem(item);
  26.         }
  27.         if( checkExt(item.name.toLowerCase()) )
  28.         {
  29.             item.type=FileType_Txt;
  30.             item.dirName=f.getPath();
  31.             fileAdapter.addItem(item);
  32.         }
  33.     }
  34.     fileAdapter.notifyDataSetChanged();
  35.     fileListview.postInvalidate();
  36. }

(4)自定义的文本阅读器控件,核心代码:

 

 

[java][/java] view plaincopy

  1. @Override
  2. public boolean onTouchEvent(MotionEvent event)
  3. {
  4.     if( bIsBeginSelecting )
  5.     {
  6.         int action=event.getAction();
  7.         Layout layout=getLayout();
  8.         switch(action)
  9.         {
  10.         case MotionEvent.ACTION_DOWN:
  11.             line=layout.getLineForVertical(getScrollY()+(int)event.getY());
  12.             off=layout.getOffsetForHorizontal(line, (event.getX()));
  13.             Selection.setSelection(getEditableText(), off);
  14.             break;
  15.         case MotionEvent.ACTION_UP:
  16.         case MotionEvent.ACTION_MOVE:
  17.             line = layout.getLineForVertical(getScrollY()+(int)event.getY());
  18.             int curOff = layout.getOffsetForHorizontal(line, (int)event.getX());
  19.             if (curOff > off)
  20.                 Selection.setSelection(getEditableText(), off, curOff);
  21.             else
  22.                 Selection.setSelection(getEditableText(), curOff, off);
  23.         }
  24.         return true;
  25.     }
  26.     else
  27.     {
  28.         super.onTouchEvent(event);
  29.         return true;
  30.     }
  31. }

详细的代码放在这里了http://download.csdn.net/detail/shensens/5603061,需要的朋友可以去下载,免费。

标签