編輯:關於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;
}
}
在Android系統中,一個Activity對應一個應用程序窗口,任何一個Activity的啟動都是由AMS服務和應用程序進程相互配合來完成的。AMS服務統一調度系統中所
Activity中四大啟動模式 在AndroidManifest.xml中 ,有一個默認的activity 在它裡面可以設置activity啟動模式, android
前面的文章已經實現相關的布局,本文接著進行相關的功能實現讀取系統聯系人當點擊“選擇聯系人”按鈕後,彈出聯系人列表,讀取系統聯系人分如下幾個步驟:系統聯系人提供了一個內容提
iMAG是一個非常簡潔高效的移動跨平台開發框架,開發一次可以同時兼容Android和iOS平台,有點兒Web開發基礎就能很快上手。當前移動端跨平台開發的框架有很多,但用i