TextField - show hint before user starts typing text

亡梦爱人 提交于 2019-12-01 05:35:58

问题


I am developing an Blackberry application. I want to show a hint text in TextField before user starts typing.when user starts typing it should disappear and when there are 0 characters in TextField it should show up.Has anybody implemented this yet?then please share.


回答1:


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);
};



回答2:


Here is my try - it is a complete code, you can run it in JDE 6.x.

When you type something, the grey string "Search" will disappear:

The border.png:

The 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);
    }
}



回答3:


here is the implementation within paint() method

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

and here is the source thanks to peter_strange http://supportforums.blackberry.com/t5/Java-Development/Prompt-hint-place-holder-text-on-a-Numeric-Password-edit-field/m-p/990817#M151704



来源:https://stackoverflow.com/questions/6393951/textfield-show-hint-before-user-starts-typing-text

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!