您的位置:首页 > 移动开发 > Objective-C

改进: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画边框
    }
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: