Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲
 Android教程網 >> Android技術 >> 關於Android編程 >> android版txt電子閱讀器(一)

android版txt電子閱讀器(一)

編輯:關於Android編程

 閒來自己動手開發了個電子閱讀器,算是功能比較簡單,閱讀txt文檔,可以添加書簽,搜索。包括:1、可以打開本地文件夾去找到要的書;2、設定背景顏色,字體等等。

    這裡簡要的介紹一下其中的核心代碼:

    (1)自動滾屏:


[java]
Handler autoScrollHandler = new Handler() 
    { 
        @Override 
        public void handleMessage(Message msg) { 
            super.handleMessage(msg); 
            switch(msg.what) 
            { 
            case BEGIN_SCROLL: 

Handler autoScrollHandler = new Handler()
 {
  @Override
  public void handleMessage(Message msg) {
   super.handleMessage(msg);
   switch(msg.what)
   {
   case BEGIN_SCROLL:[java] view plaincopyprint?                              //需要判斷是否已經滾動到最後了  
        if( tvMain.getScrollY()>= tvMain.getLineCount()*tvMain.getLineHeight()-tvMain.getHeight()) 
        { 
            tvMain.scrollTo(0, tvMain.getLineCount()*tvMain.getLineHeight()-tvMain.getHeight()); 
            autoScrollHandler.sendEmptyMessage(STOP_SCROLL); 
        } 
        else 
        { 
            tvMain.scrollTo(0, tvMain.getScrollY() + STOP_SCROLL); 
            autoScrollHandler.sendEmptyMessageDelayed(BEGIN_SCROLL, 100); 
        } 
        break; 
    case END_SCROLL: 
        autoScrollHandler.removeMessages(END_SCROLL); 
        autoScrollHandler.removeMessages(BEGIN_SCROLL); 
    case STOP_SCROLL: 
        autoScrollHandler.removeMessages(BEGIN_SCROLL); 
        autoScrollHandler.removeMessages(STOP_SCROLL); 
        break; 
    } 

                                //需要判斷是否已經滾動到最後了
    if( tvMain.getScrollY()>= tvMain.getLineCount()*tvMain.getLineHeight()-tvMain.getHeight())
    {
     tvMain.scrollTo(0, tvMain.getLineCount()*tvMain.getLineHeight()-tvMain.getHeight());
     autoScrollHandler.sendEmptyMessage(STOP_SCROLL);
    }
    else
    {
     tvMain.scrollTo(0, tvMain.getScrollY() + STOP_SCROLL);
     autoScrollHandler.sendEmptyMessageDelayed(BEGIN_SCROLL, 100);
    }
    break;
   case END_SCROLL:
    autoScrollHandler.removeMessages(END_SCROLL);
    autoScrollHandler.removeMessages(BEGIN_SCROLL);
   case STOP_SCROLL:
    autoScrollHandler.removeMessages(BEGIN_SCROLL);
    autoScrollHandler.removeMessages(STOP_SCROLL);
    break;
   }
  }
 };
    (2)讀書過程中,可以選中進行操作:發短信、打電話


[java]
      /**
 * 用戶選擇了文本之後,讓用戶選擇要發短信還是打電話
 */ 
@Override  
protected Dialog onCreateDialog(int id) 

    if( id ==  DIALOG_AFTER_SELECTION) 
    { 
        return new AlertDialog.Builder(ReadBookActivity.this) 
            .setIcon(android.R.drawable.ic_dialog_info) 
            .setMessage("您想用選定的文本:") 
            .setPositiveButton("發送短信", new android.content.DialogInterface.OnClickListener() { 
                @Override 
                public void onClick(DialogInterface dialog, int which) { 
                    Uri smsUri=Uri.parse("smsto://"); 
                    Intent smsIntent = new Intent(Intent.ACTION_SENDTO, smsUri); 
                     
                    ClipboardManager cm=(ClipboardManager)getSystemService(Context.CLIPBOARD_SERVICE); 
                    cm.setText(strSelection); 
                    startActivity(smsIntent); 
                     
                    Toast.makeText(ReadBookActivity.this, "短信內容已復制到剪貼板",  
                               Toast.LENGTH_LONG).show(); 
                        
                    tvMain.clearSelection(); 
                } 
            }) 
            .setNegativeButton("撥打電話", new android.content.DialogInterface.OnClickListener(){ 
                @Override 
                public void onClick(DialogInterface dialog, int which) { 
                    if( PhoneNumberUtils.isGlobalPhoneNumber(strSelection)) 
                    { 
                        Intent phoneIntent=new Intent(Intent.ACTION_CALL,Uri.parse("tel://"+strSelection)); 
                        startActivity(phoneIntent); 
                    } 
                    else 
                    { 
                        Toast.makeText(ReadBookActivity.this, "非法的電話號碼", 
                                Toast.LENGTH_LONG).show(); 
                    } 
                    tvMain.clearSelection(); 
                } 
            }) 
            .setNegativeButton("取消選擇", new android.content.DialogInterface.OnClickListener() { 
                @Override 
                public void onClick(DialogInterface dialog, int which) { 
                    tvMain.clearSelection(); 
                } 
            }) 
            .create(); 
    } 
    return null; 

        /**
  * 用戶選擇了文本之後,讓用戶選擇要發短信還是打電話
  */
 @Override
 protected Dialog onCreateDialog(int id)
 {
  if( id ==  DIALOG_AFTER_SELECTION)
  {
   return new AlertDialog.Builder(ReadBookActivity.this)
    .setIcon(android.R.drawable.ic_dialog_info)
    .setMessage("您想用選定的文本:")
    .setPositiveButton("發送短信", new android.content.DialogInterface.OnClickListener() {
     @Override
     public void onClick(DialogInterface dialog, int which) {
      Uri smsUri=Uri.parse("smsto://");
      Intent smsIntent = new Intent(Intent.ACTION_SENDTO, smsUri);
      
      ClipboardManager cm=(ClipboardManager)getSystemService(Context.CLIPBOARD_SERVICE);
      cm.setText(strSelection);
      startActivity(smsIntent);
      
      Toast.makeText(ReadBookActivity.this, "短信內容已復制到剪貼板",
                                Toast.LENGTH_LONG).show();
                    
                     tvMain.clearSelection();
     }
    })
    .setNegativeButton("撥打電話", new android.content.DialogInterface.OnClickListener(){
     @Override
     public void onClick(DialogInterface dialog, int which) {
      if( PhoneNumberUtils.isGlobalPhoneNumber(strSelection))
      {
       Intent phoneIntent=new Intent(Intent.ACTION_CALL,Uri.parse("tel://"+strSelection));
       startActivity(phoneIntent);
      }
      else
      {
       Toast.makeText(ReadBookActivity.this, "非法的電話號碼",
                        Toast.LENGTH_LONG).show();
      }
      tvMain.clearSelection();
     }
    })
    .setNegativeButton("取消選擇", new android.content.DialogInterface.OnClickListener() {
     @Override
     public void onClick(DialogInterface dialog, int which) {
      tvMain.clearSelection();
     }
    })
    .create();
  }
  return null;
 }

 

     (3)遍歷文件夾查找對應的文件夾或者文件


[java]
/**
 * 列出當前目錄下的文件
 * @param currDict
 */ 
private void listCurrDictFiles(File currDict) 

    fileAdapter.clearItems(); 
    fileAdapter.notifyDataSetChanged(); 
    fileListview.postInvalidate(); 
     
    if( !currDict.getPath().equals("/sdcard")) 
    { 
        FileListItem item=new FileListItem(); 
        item.name="返回上級"; 
        item.type=FileType_Folder; 
        fileAdapter.addItem(item); 
    } 
    for(File f:currDict.listFiles() ) 
    { 
        FileListItem item=new FileListItem(); 
        item.name=f.getName(); 
        if( f.isDirectory() && f.isHidden()==false) 
        { 
            item.type=FileType_Folder; 
            item.dirName=f.getPath(); 
            fileAdapter.addItem(item); 
        } 
        if( checkExt(item.name.toLowerCase()) ) 
        { 
            item.type=FileType_Txt; 
            item.dirName=f.getPath(); 
            fileAdapter.addItem(item); 
        } 
    } 
     
    fileAdapter.notifyDataSetChanged(); 
    fileListview.postInvalidate(); 

 /**
  * 列出當前目錄下的文件
  * @param currDict
  */
 private void listCurrDictFiles(File currDict)
 {
  fileAdapter.clearItems();
  fileAdapter.notifyDataSetChanged();
  fileListview.postInvalidate();
  
  if( !currDict.getPath().equals("/sdcard"))
  {
   FileListItem item=new FileListItem();
   item.name="返回上級";
   item.type=FileType_Folder;
   fileAdapter.addItem(item);
  }
  for(File f:currDict.listFiles() )
  {
   FileListItem item=new FileListItem();
   item.name=f.getName();
   if( f.isDirectory() && f.isHidden()==false)
   {
    item.type=FileType_Folder;
    item.dirName=f.getPath();
    fileAdapter.addItem(item);
   }
   if( checkExt(item.name.toLowerCase()) )
   {
    item.type=FileType_Txt;
    item.dirName=f.getPath();
    fileAdapter.addItem(item);
   }
  }
  
  fileAdapter.notifyDataSetChanged();
  fileListview.postInvalidate();
 }
      (4)自定義的文本閱讀器控件,核心代碼:


[java]
@Override 
public boolean onTouchEvent(MotionEvent event) 

    if( bIsBeginSelecting ) 
    { 
        int action=event.getAction(); 
        Layout layout=getLayout(); 
        switch(action) 
        { 
        case MotionEvent.ACTION_DOWN: 
            line=layout.getLineForVertical(getScrollY()+(int)event.getY()); 
            off=layout.getOffsetForHorizontal(line, (event.getX())); 
            Selection.setSelection(getEditableText(), off); 
            break; 
        case MotionEvent.ACTION_UP: 
        case MotionEvent.ACTION_MOVE: 
            line = layout.getLineForVertical(getScrollY()+(int)event.getY());     
            int curOff = layout.getOffsetForHorizontal(line, (int)event.getX()); 
            if (curOff > off) 
                Selection.setSelection(getEditableText(), off, curOff); 
            else 
                Selection.setSelection(getEditableText(), curOff, off); 
        } 
        return true; 
    } 
    else 
    { 
        super.onTouchEvent(event); 
        return true; 
    } 

    @Override
    public boolean onTouchEvent(MotionEvent event)
    {
     if( bIsBeginSelecting )
     {
      int action=event.getAction();
      Layout layout=getLayout();
      switch(action)
      {
      case MotionEvent.ACTION_DOWN:
       line=layout.getLineForVertical(getScrollY()+(int)event.getY());
       off=layout.getOffsetForHorizontal(line, (event.getX()));
       Selection.setSelection(getEditableText(), off);
       break;
      case MotionEvent.ACTION_UP:
      case MotionEvent.ACTION_MOVE:
       line = layout.getLineForVertical(getScrollY()+(int)event.getY());   
                int curOff = layout.getOffsetForHorizontal(line, (int)event.getX());
                if (curOff > off)
                 Selection.setSelection(getEditableText(), off, curOff);
                else
                 Selection.setSelection(getEditableText(), curOff, off);
      }
      return true;
     }
     else
     {
      super.onTouchEvent(event);
      return true;
     }
    }


 

  1. 上一頁:
  2. 下一頁:
熱門文章
閱讀排行版
Copyright © Android教程網 All Rights Reserved