Improved: How to-create a custom field using attributes of the other UI objects

Source: Internet
Author: User
Tags border color gettext

Original function: A multiline input field with a fixed height and width border that can be scrolled vertically.

Improvement: The input box draws a rounded border, with a different border color for focus and non-focus states.

Original: How to-create a custom field using attributes of the 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 of our object
public TextBoxField (int width, int height) {
This call to super would help keep the object
Super (Manager.no_vertical_scroll);
Managerwidth = width;
Managerheight = height;

VFM'll allow scrolling within the object
Verticalfieldmanager VFM = new Verticalfieldmanager (manager.vertical_scroll);

EditField = new EditField () {
public void Paint (Graphics g) {
This invalidation'll 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 ());

Draw the rounded corners of different colors according to focus or not
int oldcolour = G.getcolor ();
if (This.isfocus ()) {
G.setcolor (color.red); Border color of onfocus
G.drawroundrect (0, 0, getwidth (), GetHeight (), 15, 15);
} else {
G.setcolor (Color.gray); Border color of Onunfocus
G.drawroundrect (0, 0, getwidth (), GetHeight (), 15, 15);
}
G.setcolor (Oldcolour);
}

If This call to Sublayout is made by the system then
Both parameters would is 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'll force the height of the object
    & nbsp;  //where the above super.sublayout () call to
       //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 Draw Border
}

    protected void Onunfocus () {
     super.onunfocus ();
     invalidate ();    //force Paint Draw Border
    
}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.