android版txt电子阅读器(一)
闲来自己动手开发了个电子阅读器,算是功能比较简单,阅读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.DialogInte易做图ce.OnClickListener() {
@Override
public void onClick(DialogInte易做图ce 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 andr
补充:移动开发 , Android ,