改进:How To - Create a custom field using attributes of other UI objects
2010-09-26 14:20
423 查看
原有功能:有固定高度和宽度边框的多行输入域,可以纵向滚动。
改进:输入框画圆角边框,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画边框
}
}
改进:输入框画圆角边框,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画边框
}
}
相关文章推荐
- How To - Create a custom field using attributes of other UI objects
- How to: Create Custom Configuration Sections Using ConfigurationSection
- Change language of UI using custom control in SharePoint 2010
- 【转】How to use LINQ methods to compare objects of custom types
- Beginning Guide With Controlling XML Serialization Using Attributes(XmlSerializaiton of Array)
- How to generate self-signed certificate using keytool command of JDK?
- Siebel Tools: Create Custom MiniButton Using Tools
- Data Structures and Other Objects Using C++ (Chapter 1) 学习笔记三
- The basics of using ui-router with AngularJS
- Using a Custom Action to Create a Database During Installation
- Breaking Tight Coupling by using Mock Objects(Chapter 4 of Python Testing Beginner's Guide)
- Create a Simple Custom Action Using Visual Studio 2010
- Real-time tracking of multiple objects using adaptive correlation filters with complex constraints
- Siebel Tools: Create Custom MiniButton Using Tools
- Microsoft Office Project 2007 使用指南(3) - Create a custom field
- Using a Custom Action to Create a Database During Installation
- stringByReplacingPercentEscapesUsingEncoding attributesOfItemAtPath
- Create custom field type
- Tailor Your DataGrid Apps Using Table Style and Custom Column Style Objects
- Using a Custom Action to Create a Database During Installation