TextField - 在用户开始键入文本之前显示提示

时间:2011-06-18 04:47:09

标签: blackberry textfield hint

我正在开发一个Blackberry应用程序。我想在用户开始输入之前在TextField中显示一个提示文本。当用户开始输入时它应该消失,当TextField中有0个字符时它应该显示出来。有人实现了吗?那么请分享。

3 个答案:

答案 0 :(得分:7)

protected void paint(Graphics g) 
{
    if(super.getText().length() == 0)
    {
        g.setColor(Color.GRAY);
        g.drawText("MMYY", 0, 0);
    }
    g.setColor(Color.BLACK);
    super.paint(g);
};

答案 1 :(得分:4)

这是我的尝试 - 它是一个完整的代码,您可以在JDE 6.x中运行它。

当您输入内容时,灰色字符串“搜索”将消失:

screenshot

border.png:

border.png

src \ mypackage \ MyEdit.java:

package mypackage;

import net.rim.device.api.system.*;
import net.rim.device.api.ui.*;
import net.rim.device.api.ui.component.*;
import net.rim.device.api.ui.container.*;
import net.rim.device.api.ui.decor.*;

public class MyEdit extends UiApplication {
    public static void main(String args[]) {
        MyEdit app = new MyEdit();
        app.enterEventDispatcher();
    }

    public MyEdit() {
        pushScreen(new MyScreen());
    }
} 

class MyScreen extends MainScreen {
    Border myBorder = BorderFactory.createBitmapBorder( 
        new XYEdges(20, 16, 27, 23), 
        Bitmap.getBitmapResource("border.png"));

    BasicEditField myField = new BasicEditField(TextField.NO_NEWLINE) {
        protected void paint(Graphics g) {
            if (getTextLength() == 0) {
                g.setColor(Color.LIGHTGRAY);
                g.drawText("Search", 0, 0);
            }

            g.setColor(Color.BLACK);
            super.paint(g);
        }
    };

    public MyScreen() {
        myField.setBorder(myBorder);
        setTitle(myField);
    }
}

答案 2 :(得分:2)

这是paint()方法中的实现

String test = super.getText();
        if ( test == null || test.length() < 1 ) {
            graphics.setColor( 0x00a0a0a0 );
            graphics.drawText(hint, 0, 0);
        }

这是源于peter_strange的来源 http://supportforums.blackberry.com/t5/Java-Development/Prompt-hint-place-holder-text-on-a-Numeric-Password-edit-field/m-p/990817#M151704