📄 customfieldcomposer.java
字号:
package com.jmobilecore.ui;
import com.jmobilecore.ui.core.AbstractComposer;
import javax.microedition.lcdui.Font;
abstract public class CustomFieldComposer implements AbstractComposer {
protected TextBlockComposer[] textBlocks;
protected int curBlock = 0;
public CustomFieldComposer(TextBlockComposer[] textBlocks) {
this.textBlocks = textBlocks;
}
public void clear() {
for (int i = 0; i < textBlocks.length; i++) {
textBlocks[i].clear();
}
}
/**
* Get maximum lenght of the text which can be entered in TextFieldComposer
*
* @return maxSize max length of the text
*/
public int getMaxSize() {
int size = 0;
for (int i = 0; i < textBlocks.length; i++) {
size += textBlocks[i].getMaxSize();
}
return size;
}
/**
* Moves the caret one position left
*
* @return <code>true</code> if the moving is possible/done, <code>false</code> otherwise
*/
public boolean caretLeft() {
if (textBlocks[curBlock].caretLeft()) {
if (textBlocks[curBlock].getCaretPosition() >= 0) {
return true;
}
}
boolean isComplete = textBlocks[curBlock].isComplete();
if (!isComplete) return true;
if (curBlock == 0) return true;
curBlock--;
return textBlocks[curBlock].setCaretPosition(textBlocks[curBlock].getText().length() - 1);
}
/**
* Moves the caret one position right
*
* @return <code>true</code> if the moving is possible/done, <code>false</code> otherwise
*/
public boolean caretRight() {
// if (curBlock<0 && curBlock>=textBlocks.length) return false;
if (textBlocks[curBlock].caretRight()) {
if (textBlocks[curBlock].getCaretPosition() < textBlocks[curBlock].getMaxSize()) {
return true;
}
}
boolean isComplete = textBlocks[curBlock].isComplete();
if (!isComplete) return true;
if (curBlock == textBlocks.length - 1) return true;
curBlock++;
return textBlocks[curBlock].setCaretPosition(0);
}
/**
* Deletes previous symbol, moves cursor one position left
*
* @return <code>true</code> if the delete and moving is possible/done, <code>false</code> otherwise
*/
public boolean backspace() {
if (!caretLeft()) return false;
return deleteChar();
}
/**
* Deletes current symbol
*
* @return <code>true</code> if the delete possible/done, <code>false</code> otherwise
*/
public boolean deleteChar() {
return textBlocks[curBlock].deleteChar();
}
/**
* Adds new symbol or replaces the current one
*
* @param ch new symbol
* @param insertMode if <code>true</code> then new symbol will be added, otherwise the current symbol
* will be replaced
* @return <code>true</code> if the add/replace is possible, <code>false</code> otherwise
*/
public boolean addChar(char ch, boolean insertMode) {
return textBlocks[curBlock].addChar(ch, insertMode);
}
/**
* Returns the text that is presented by this text component.
*/
public char[] getChars() {
return getText().toCharArray();
}
/**
* Returns the text that is presented by this text component.
*/
public String getText() {
StringBuffer rslt = new StringBuffer();
for (int i = 0; i < textBlocks.length; i++) {
rslt.append(textBlocks[i].getChars());
}
return new String(rslt);
}
/**
* Returns the cursor offset (in pixels) for the composed string
*
* @param font the current font.
* @param echoChar the echo character, not used if equal to <code>0</code>
*/
public int getCursorOffset(Font font, char echoChar) {
char[] text = getChars();
int caretPosition = getCaretPosition();
int offset;
if (echoChar == 0) {
offset = font.charsWidth(text, 0, caretPosition);
} else {
offset = font.charWidth(echoChar) * caretPosition;
}
return (offset < 0) ? 0 : offset;
}
public int getCurrentLength() {
int len = 0;
for (int i = 0; i < textBlocks.length; i++) {
len += textBlocks[i].getCurrentLength();
}
return len;
}
public int getCaretPosition() {
int caretPosition = textBlocks[curBlock].getCaretPosition();
for (int i = 0; i < curBlock; i++) {
caretPosition += textBlocks[i].getCurrentLength();
}
return caretPosition;
}
public int getCurrentBlock() {
return curBlock;
}
public boolean setCaretPosition(int caretPosition) {
if (caretPosition < 0) {
return false;
}
int cp = caretPosition;
int cl;
for (int i = 0; i < textBlocks.length; i++) {
cl = textBlocks[i].getCurrentLength();
if (cp < cl) {
textBlocks[i].setCaretPosition(cp);
curBlock = i;
return true;
}
cp -= cl;
}
return false;
}
public boolean isComplete() {
for (int i = 0; i < textBlocks.length; i++) {
if (!textBlocks[i].isComplete()) {
return false;
}
}
return true;
}
/**
* Default destructor. Helps VM to perform garbage collection
*/
public void destructor() {
for (int i = 0; i < textBlocks.length; i++) {
textBlocks[i] = null;
}
textBlocks = null;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -