有边界的多行线文本框在黑莓

时间:2011-08-16 06:45:18

标签: blackberry

我是黑莓新手,这是我的第一个问题。我正在为此做一个示例应用程序我需要多行textbox.i用Google搜索,我找到了下面的代码。

VerticalFieldManager vfm =new VerticalFieldManager(Manager.VERTICAL_SCROLL)
            {
                public void paint(Graphics g) 
                             {

                          g.drawRoundRect(0, 0,getWidth(), getHeight(),12,12);
                       super.paint(g);
                    }
                public void sublayout(int width, int height)
                              {
                      if (managerWidth == 0) {
                        managerWidth = width;
                      }
                      if (managerHeight == 0) {
                        managerHeight = height;
                      }
                      super.sublayout(managerWidth, managerHeight);
                      setExtent(managerWidth,managerHeight);
                    }

            };

              editField = new EditField(){
                public void paint(Graphics g) {
                getManager().invalidate();
                super.paint(g);
              }
            };

            vfm.add(editField);
                               vfm.setBackground(BackgroundFactory.createSolidBackground(Color.WHITE));
            add(vfm);

它工作正常,如果文本小于字段高度,如果文本大于字段高度。文本正在越过边界。我如何解决这个问题。请帮助我。

3 个答案:

答案 0 :(得分:0)

您需要实现自己的自定义字段。

可以在此处找到文档:"Create a custom field"

答案 1 :(得分:0)

您可以使用Blackberry API中的EditField并使用:

yourEditField.setBorder(yourBorder);

然后你会有一个带边框的编辑字段,它接受几行

答案 2 :(得分:0)

package nativeapp;
import net.rim.device.api.system.Bitmap;
import net.rim.device.api.system.Display;
import net.rim.device.api.ui.Color;
import net.rim.device.api.ui.Field;
import net.rim.device.api.ui.FieldChangeListener;
import net.rim.device.api.ui.Graphics;
import net.rim.device.api.ui.UiApplication;
import net.rim.device.api.ui.XYEdges;
import net.rim.device.api.ui.component.BitmapField;
import net.rim.device.api.ui.component.ButtonField;
import net.rim.device.api.ui.component.CheckboxField;
import net.rim.device.api.ui.component.Dialog;
import net.rim.device.api.ui.component.LabelField;
import net.rim.device.api.ui.container.HorizontalFieldManager;
import net.rim.device.api.ui.container.MainScreen;
import net.rim.device.api.ui.container.VerticalFieldManager;
import net.rim.device.api.ui.decor.Background;
import net.rim.device.api.ui.decor.BackgroundFactory;
import net.rim.device.api.ui.decor.Border;
import net.rim.device.api.ui.decor.BorderFactory;

public class LoginScreen extends MainScreen {

    /**
     * Login page
     */

    private ButtonField btnLogin;
    //private EditField txtUserID;
     CustomTextBox txtUserID;
    CustomTextBox txtPassword;
    // private PasswordEditField txtPassword;
    private CheckboxField Chkbox;
    private LabelField RememberMe;

    public LoginScreen() {
        super(NO_VERTICAL_SCROLL);

        txtUserID = new CustomTextBox() {

            protected void paint(Graphics g) {
                // Border roundedBorder = BorderFactory
                // .createRoundedBorder(new XYEdges(9, 9, 9, 9),
                // Color.GRAY, Border.STYLE_SOLID);
                // setBorder(roundedBorder);
                // setBackground(BackgroundFactory
                // .createSolidBackground(Color.WHITE));

                if (getText().length() == 0) {
                    g.setColor(Color.GRAY);
                    g.drawText("UserName", 0, 0);

                }
                g.setColor(Color.BLACK);
                //g.drawRect(0, 0, (int) (Display.getWidth() / 1.5),
                        //txtUserID.getHeight()/* (int)(Display.getHeight()/9) */);
                super.paint(g);
            }
        };

        txtPassword = new CustomTextBox() {
            protected void paint(Graphics g) {
                Border roundedBorder = BorderFactory
                        .createRoundedBorder(new XYEdges(9, 9, 9, 9),
                                Color.GRAY, Border.STYLE_SOLID);
                setBorder(roundedBorder);
                setBackground(BackgroundFactory
                        .createSolidBackground(Color.WHITE));
                if (getText().length() == 0) {
                    g.setColor(Color.GRAY);
                    g.drawText("txtPassword", 0, 0);
                }
                g.setColor(Color.BLACK);
                super.paint(g);
            }
        };

        // btnLogin = new ButtonField("SignIn");
        btnLogin = new ButtonField("Sign In", Field.FIELD_HCENTER);

        Chkbox = new CheckboxField();
        // Chkbox.setMargin(0, 0, 0, 30);
        RememberMe = new LabelField("Remember Me");
        // RememberMe.setMargin(10, 0, 0, 0);

        FieldChangeListener login = new FieldChangeListener() {

            public void fieldChanged(Field field, int context) {
                String uname = txtUserID.getText().toString().trim();
                String pword = txtPassword.getText().toString().trim();
                if (!uname.equalsIgnoreCase("")
                        && !uname.equalsIgnoreCase(null)
                        && !pword.equalsIgnoreCase("")
                        && !pword.equalsIgnoreCase(null)) {
                    boolean lgnStatus = (new httpClient()).loginStatus(uname,
                            pword, DataURL.smartURLloginchk);
                    if (lgnStatus) {
                        PersistentsStore ps = new PersistentsStore();
                        ps.setObject(DataURL.UserName, uname);
                        ps.setObject(DataURL.PassWord, pword);
                        UiApplication.getUiApplication().popScreen(
                                LoginScreen.this);
                        UiApplication.getUiApplication().pushScreen(
                                new WelcomeScreen());
                    } else {
                        Dialog.inform("Invalid UserID or Password");
                    }

                } else {
                    Dialog.inform("UserID and Password shouldnot be Empty");
                }

            }
        };

        FieldChangeListener cancel = new FieldChangeListener() {

            public void fieldChanged(Field field, int context) {

            }
        };

        btnLogin.setChangeListener(login);
        // btnCancel.setChangeListener(cancel);
        setLoginScreen();
    }

    private void setLoginScreen() {

        VerticalFieldManager vfmForCenterDisplay = new VerticalFieldManager(
                Field.FIELD_HCENTER | Field.FIELD_VCENTER | Field.USE_ALL_WIDTH
                        | Field.USE_ALL_HEIGHT) {
            protected void sublayout(int maxWidth, int maxHeight) {
                super.sublayout(maxWidth, maxHeight);

            }
        };

        Background background = BackgroundFactory.createBitmapBackground(Bitmap
                .getBitmapResource("bground.png"));
        vfmForCenterDisplay.setBackground(background);

        int topSpace = (Display.getHeight() / 4);
        vfmForCenterDisplay.setPadding(topSpace, 0, 0, 0);

        VerticalFieldManager vfmBasicInfoWithBorder1 = new VerticalFieldManager(
                FIELD_HCENTER | FIELD_VCENTER) {
            protected void sublayout(int maxWidth, int maxHeight) {
                super.sublayout(250, 95);
            }
        };

        BitmapField bitmapField = new BitmapField(
                Bitmap.getBitmapResource("loginbg_01.png")) {
            protected void layout(int width, int height) {
                setExtent(250, 95);
            }
        };

        vfmBasicInfoWithBorder1.add(bitmapField);

        VerticalFieldManager vfmBasicInfoWithBorder2 = new VerticalFieldManager(
                FIELD_HCENTER | FIELD_VCENTER) {
            protected void sublayout(int maxWidth, int maxHeight) {
                super.sublayout(250, 95);
            }

        };
        vfmBasicInfoWithBorder2.setBackground(BackgroundFactory
                .createBitmapBackground(Bitmap
                        .getBitmapResource("loginbg_02.jpg")));

        VerticalFieldManager vfmBasicInfoWithBorder3 = new VerticalFieldManager(
                FIELD_HCENTER | FIELD_VCENTER) {
            protected void sublayout(int maxWidth, int maxHeight) {
                super.sublayout(250, 95);
                setExtent(250, 95);
            }
        };

        vfmBasicInfoWithBorder3.setBackground(BackgroundFactory
                .createBitmapBackground(Bitmap
                        .getBitmapResource("loginbg_03.png")));
        vfmBasicInfoWithBorder3.add(btnLogin);
        // btnLogin.setMargin(0, 0, 0, 0);

        // HorizontalFieldManager hfm1 = new HorizontalFieldManager(
        // Field.FIELD_HCENTER);

        HorizontalFieldManager hfm2 = new HorizontalFieldManager(
                Field.FIELD_VCENTER);
        hfm2.add(txtUserID);
        // txtUserID.setMargin(0, 0, 0, 0);

        HorizontalFieldManager hfm3 = new HorizontalFieldManager(FIELD_HCENTER
                & FIELD_VCENTER);
        hfm3.add(txtPassword);
        // txtPassword.setMargin(0, 0, 0, 10);

        HorizontalFieldManager hfm4 = new HorizontalFieldManager(
                Field.FIELD_HCENTER);
        hfm4.add(Chkbox);
        Chkbox.setMargin(0, 0, 0, 10);
        hfm4.add(RememberMe);
        RememberMe.setMargin(0, 0, 0, 10);

        // HorizontalFieldManager hfm5 = new
        // HorizontalFieldManager(FIELD_HCENTER
        // & FIELD_VCENTER) {
        // protected void sublayout(int maxWidth, int maxHeight) {
        // super.sublayout(250, 95);
        // }
        // };
        // hfm5.add(btnLogin);
        // btnLogin.setMargin(0, 0, 0, 0);
        // hfm5.add(btnCancel);
        // hfm5.setPadding(new XYEdges(0, 0, 0, ((250 - (70 + 75)) / 2)));

        // btnCancel.setMargin(0, 0, 0, 15);

        // vfmBasicInfoWithBorder1.add(hfm1);
        vfmBasicInfoWithBorder2.add(hfm2);
        vfmBasicInfoWithBorder2.add(hfm3);
        vfmBasicInfoWithBorder3.add(hfm4);
        // vfmBasicInfoWithBorder3.add(hfm5);

        vfmForCenterDisplay.add(vfmBasicInfoWithBorder1);
        vfmForCenterDisplay.add(vfmBasicInfoWithBorder2);
        vfmForCenterDisplay.add(vfmBasicInfoWithBorder3);
        add(vfmForCenterDisplay);
    }

}