为TextBox组件创建简单的剪贴板
2008-01-05 10:00:13 来源:WEB开发网核心提示:在Core J2ME Technology and MIDP一书中许多的可以在多个多行编辑组件(multiple TextBox)中共享剪贴板数据的例子,注重:这个例子基于MIDP与CLDC 1.0.3版本文得到作者John W. MUChow的答应,为TextBox组件创建简单的剪贴板,部分引用了Core J2ME
在Core J2ME Technology and MIDP一书中许多的可以在多个多行编辑组件(multiple TextBox)中共享剪贴板数据的例子。
注重:这个例子基于MIDP与CLDC 1.0.3版
本文得到作者John W. MUChow的答应,部分引用了Core J2ME Technology and MIDP, Sun Microsystems与PRentice Hall出版社 2002 Sun Microsystems Inc, 的内容。
源代码:
[code]/*--------------------------------------------------
* SimpleClipBoard.java
*
* Example from the book: Core J2ME Technology
* Copyright John W. Muchow http://www.CoreJ2ME.com
* You may use/modify for any non-commercial purpose
*-------------------------------------------------*/
import javax.microedition.midlet.*;
import javax.microedition.lcdui.*;
public class SimpleClipBoard extends MIDlet implements CommandListener
{
private Display display; // 对显示对象的引用Reference to Display object
private TextBox tbClip; // 主TextBox组件Main textbox
private Command cmExit; // 退出命令Command to exit
private Command cmStartMark; // 开始标记命令Command to start marking a block
private Command cmCopy; // 复制剪贴板命令Command to copy to clipboard
private Command cmPaste; // 粘贴剪贴板命令Command to paste into textbox
private int beginOffset = 0; // 复制的开始索引The start index of copy
private char[] clipBoard = null; // 剪贴板The clipboard
private int clipBoardChars = 0; // 记录剪贴板中的字符Number of chars in clipboard
public SimpleClipBoard()
{
display = Display.getDisplay(this);
// Create the Commands. Notice the priorities assigned
cmExit = new Command("Exit", Command.EXIT, 1);
cmStartMark = new Command("Mark", Command.SCREEN, 2);
cmCopy = new Command("Copy", Command.SCREEN, 3);
cmPaste = new Command("Paste", Command.SCREEN, 4);
tbClip = new TextBox("Clip Board", "Tee to grn", 15, TextField.ANY);
tbClip.addCommand(cmExit);
tbClip.addCommand(cmStartMark);
tbClip.addCommand(cmCopy);
tbClip.addCommand(cmPaste);
tbClip.setCommandListener(this);
// Allocate a clipboard big enough to hold the entire textbox
clipBoard = new char[tbClip.getMaxSize()];
}
public void startApp()
{
display.setCurrent(tbClip);
}
public void pauseApp()
{
}
public void destroyApp(boolean unconditional)
{
}
public void commandAction(Command c, Displayable s)
{
if (c == cmStartMark)
{
beginOffset = tbClip.getCaretPosition();
}
else if (c == cmCopy && (tbClip.getCaretPosition() > beginOffset))
{
// Allocate an array to hold the current textbox contents
char[] chr = new char[tbClip.size()];
// Get the current textbox contents
tbClip.getChars(chr);
// The count of characters in the clipboard
clipBoardChars = tbClip.getCaretPosition() - beginOffset;
// Copy the text into the clipboard
// arraycopy(source, sourceindex, dest, destindex, count)
System.arraycopy(chr, beginOffset, clipBoard, 0, clipBoardChars);
}
else if (c == cmPaste)
{
// Make sure the paste will not overrun the textbox length.
if ((tbClip.size() + clipBoardChars) <= tbClip.getMaxSize())
tbClip.insert(clipBoard, 0, clipBoardChars, tbClip.getCaretPosition());
}
else if (c == cmExit)
{
destroyApp(false);
notifyDestroyed();
}
}
}
[]
- ››创建基于PPTP的站点到站点VPN连接:ISA2006系列之...
- ››创建基于L2TP的站点到站点的VPN连接:ISA2006系列...
- ››创建一个Twisted Reactor TCP服务器
- ››组件对象模型与ATL实现
- ››组件对象模型与ATL实现补遗
- ››创建Windows Mobile上兼容性好的UI 程序
- ››创建android的Service
- ››创建远古部落环境与原住民角色
- ››创建并扩展Apache Wicket Web应用
- ››创建不在任务条中显示窗口按钮的应用
- ››创建 Android 文件系统(Root file system)
- ››创建 Android 系统步骤简介
赞助商链接