改进:How To - Create a custom field using attributes of other UI objects

原有功能:有固定高度和宽度边框的多行输入域,可以纵向滚动。

改进:输入框画圆角边框,focus和非focus状态的边框颜色不同。

原文:How To - Create a custom field using attributes of other UI objects

import net.rim.device.api.ui.Color;
import net.rim.device.api.ui.Graphics;
import net.rim.device.api.ui.Manager;
import net.rim.device.api.ui.component.EditField;
import net.rim.device.api.ui.container.VerticalFieldManager;

/*
* TextBoxField.java
*/

//Extend VerticalFieldManager to help control scrolling and
//to set a fixed width/height for our field
public class TextBoxField extends VerticalFieldManager {
//Define some variables to be used
//in the class
private int managerWidth;
private int managerHeight;
private EditField editField;

//Pass in the fixed height and width for our object
public TextBoxField(int width, int height) {
//This call to super will help keep the object in place
super(Manager.NO_VERTICAL_SCROLL);
managerWidth = width;
managerHeight = height;

//vfm will allow scrolling within the object
VerticalFieldManager vfm = new VerticalFieldManager(Manager.VERTICAL_SCROLL);

editField = new EditField(){
public void paint(Graphics g) {
//This invalidation will help keep the border clean
//while scrolling
getManager().invalidate();
super.paint(g);
}
};

vfm.add(editField);
add(vfm);
}


public void paint(Graphics g) {
super.paint(g);
//Draw a rectangle around out TextBoxField
//g.drawRect(0, 0, getWidth(), getHeight());

//根据focus与否,Draw不同颜色的圆角的边框
int oldColour = g.getColor();
if (this.isFocus()) {
g.setColor(Color.RED);//onFocus的边框颜色
g.drawRoundRect(0, 0, getWidth(), getHeight(), 15, 15);
} else {
g.setColor(Color.GRAY);//onUnfocus的边框颜色
g.drawRoundRect(0, 0, getWidth(), getHeight(), 15, 15);
}
g.setColor(oldColour);
}

//If this call to sublayout was made by the system then
//both parameters would be passed with a value of 0.
//This check and adjustment keeps the fixed properties
//maintained.
public void sublayout(int width, int height) {

if (managerWidth == 0) {
managerWidth = width;
}
if (managerHeight == 0) {
managerHeight = height;
}
super.sublayout(managerWidth, managerHeight);
//Force the extent of our manager.
//This will force the height of the object
//where the above super.sublayout() call will
//set the width.
setExtent(managerWidth,managerHeight);
}


//The following two methods allows users of the
//TextBofField read and set its contents.
public String getText() {
return editField.getText();
}

public void setText(String text) {
editField.setText(text);
}

protected void onFocus(int direction) {
super.onFocus(direction);
editField.setCursorPosition(editField.getTextLength());
invalidate();//force paint画边框
}

protected void onUnfocus() {
super.onUnfocus();
invalidate();//force paint画边框
}
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值