我正在使用此链接 Place holder text on a AutoCompleteField in blackberry
我希望在用户在文本字段上输入任何内容时过滤列表。当前它会过滤字符串数组的第一个文本。例如,如果写入" f"过滤"周五"但我希望它应该过滤用括号
写的任何文字例如,如果我选择" M" 。如果显示"星期三 - (MAN)"
String[] days = {"Monday(TAS)","Tuesday-(PAQ)","Wednesday-(MAN)",
"Thursday","Friday","Saturday","Sunday(I_)"};
CustomAutoCompleteField.java
package mypackage;
import net.rim.device.api.collection.util.BasicFilteredList;
import net.rim.device.api.collection.util.BasicFilteredListResult;
import net.rim.device.api.system.Application;
import net.rim.device.api.ui.Color;
import net.rim.device.api.ui.Graphics;
import net.rim.device.api.ui.component.AutoCompleteField;
import net.rim.device.api.ui.component.ListField;
class CustomAutoCompleteField extends AutoCompleteField {
private int yOffset = 0;
private int xOffset = 0;
public CustomAutoCompleteField(BasicFilteredList filteredList) {
super(filteredList);
}
protected void paint(Graphics g) {
super.paint(g);
if (xOffset == 0) {
// initialize text offsets once
xOffset = getEditField().getContentLeft();
yOffset = getEditField().getContentTop();
}
String text = getEditField().getText();
if (text == null || text.length() == 0) {
int oldColor = g.getColor();
g.setColor(Color.GRAY);
g.drawText("enter text", xOffset, yOffset);
g.setColor(oldColor);
}
}
public void onSelect(Object selection, int SELECT_TRACKWHEEL_CLICK) {
ListField _list = getListField();
if (_list.getSelectedIndex() > -1) {
final String selectedText = getEditField().getText();
if(selectedText!=null){
final BasicFilteredListResult result = (BasicFilteredListResult) selection;
Application.getApplication().invokeLater(new Runnable() {
public void run() {
selectedText.setText(result._object.toString());
}
});
// selectedText.setText(result._object.toString());
}
}
}
protected void sublayout(int maxWidth, int maxHeight) {
// TODO Auto-generated method stub
super.sublayout(250, 250);
}
}
myscreen.js
public final class MyScreen extends MainScreen
{
/**
* Creates a new MyScreen object
*/
public MyScreen()
{
// Set the displayed title of the screen
BasicFilteredList filterList = new BasicFilteredList();
String[] days = {"Monday(TAS)","Tuesday-(PAQ)","Wednesday-(MAN)",
"Thursday","Friday","Saturday","Sunday(I_)"};
filterList.addDataSet(1,days,"days",BasicFilteredList.COMPARISON_IGNORE_CASE);
HorizontalFieldManager hr =new HorizontalFieldManager();
LabelField userName= new LabelField("hjsdhas");
hr.add(userName);
CustomAutoCompleteField autoCompleteField = new CustomAutoCompleteField(filterList);
hr.add(autoCompleteField);
add(hr);
}
}
我也尝试第二种方式。我想用"而不是"但显示文字" days"
package mypackage;
import net.rim.device.api.collection.util.BasicFilteredList;
import net.rim.device.api.system.Bitmap;
import net.rim.device.api.ui.Color;
import net.rim.device.api.ui.Graphics;
import net.rim.device.api.ui.XYEdges;
import net.rim.device.api.ui.component.AutoCompleteField;
import net.rim.device.api.ui.component.BasicEditField;
import net.rim.device.api.ui.component.LabelField;
import net.rim.device.api.ui.component.TextField;
import net.rim.device.api.ui.container.HorizontalFieldManager;
import net.rim.device.api.ui.container.MainScreen;
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;
/**
* A class extending the MainScreen class, which provides default standard
* behavior for BlackBerry GUI applications.
*/
public final class MyScreen extends MainScreen
{
/**
* Creates a new MyScreen object
*/
public static BasicFilteredList filterList;
private int yOffset = 0;
private int xOffset = 0;
public MyScreen()
{
BasicFilteredList filterList = new BasicFilteredList();
String[] days = {"Monday-(wfc)","Tuesday-(afc)","Wednesday-(pqr)",
"Thursday-(wfc)","Friday-(wfc)","Saturday-(wfc)","Sunday-(wfc)"};
String[] not = {"wfc","afc","pqr",
"pqr","wfc","wfc","wfc"};
filterList.addDataSet(1,not,"days",BasicFilteredList.COMPARISON_IGNORE_CASE);
HorizontalFieldManager hr=new HorizontalFieldManager();
LabelField l =new LabelField("User Name");
AutoCompleteField autoCompleteField = new AutoCompleteField(filterList){
protected void paint(Graphics g) {
super.paint(g);
if (xOffset == 0) {
// initialize text offsets once
xOffset = getEditField().getContentLeft();
yOffset = getEditField().getContentTop();
}
String text = getEditField().getText();
if (text == null || text.length() == 0) {
int oldColor = g.getColor();
g.setColor(Color.GRAY);
g.drawText("enter text", xOffset, yOffset);
g.setColor(oldColor);
}
}
protected void sublayout(int maxWidth, int maxHeight) {
// TODO Auto-generated method stub
super.sublayout(200, 150);
}
};
hr.add(l);
hr.add(autoCompleteField);
add(hr);
}
}