问题
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