android版txt电子阅读器(一)

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

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

    (1)自动滚屏:

Handler autoScrollHandler = new Handler()
    {
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            switch(msg.what)
            {
            case BEGIN_SCROLL:
                                //需要判断是否已经滚动到最后了
                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)读书过程中,可以选中进行操作:发短信、打电话

        /**
     * 用户选择了文本之后,让用户选择要发短信还是打电话
     */
    @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)遍历文件夹查找对应的文件夹或者文件

    /**
     * 列出当前目录下的文件
     * @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)自定义的文本阅读器控件,核心代码:

    @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;
        }
    }

源码:http://download.csdn.net/detail/javadxz/6847169

郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。