gpt4 book ai didi

blackberry - ListField 中文本右侧的复选框

转载 作者:行者123 更新时间:2023-12-01 17:36:59 25 4
gpt4 key购买 nike

我想要在文本右侧带有图像和复选框的列表字段。我在左侧获得了图像,但无法在右侧获得复选框。如果可能,复选框应该是不可见的。应该只看到刻度线。我要实现

   Image text1 chechbox 
Image text2
Image text3 checkbox

所有列表项的图像应该相同。

public final class ListfieldDemo1 extends MainScreen

{

private Vector _listElements;
ListField list;
private ListField _checkList;

public ListfieldDemo1()
{
// Set the displayed title of the screen
super(Manager.NO_VERTICAL_SCROLL);
add(new LabelField("Fruits List", LabelField.FIELD_HCENTER));
add(new SeparatorField());
setTitle("ListField with images and checkbox");
_listElements = new Vector();
add(new SeparatorField());
list = new ListField();
ListCallback _callback = new ListCallback(this);
list.setCallback(_callback);
list.setSize(4);
int index = list.getSelectedIndex();

add(list);

createField();
}

protected void createField()
{
String itemOne = "Apple";
String itemTwo = "Blackberry";
String itemthree ="Grapes";
String itemfour = "Banana";
_listElements.addElement(itemOne);

_listElements.addElement(itemTwo);
_listElements.addElement(itemthree);
_listElements.addElement(itemfour);
reloadList();
}

private void reloadList() {

list.setSize(_listElements.size());
}
public boolean invokeAction(int action)
{
switch (action)
{
case ACTION_INVOKE: // Trackball click.
return true;
}
return super.invokeAction(action);

}
class ListCallback implements ListFieldCallback
{
private static final int LEFT_OFFSET = 10;
private static final int TOP_OFFSET = 10;
ListfieldDemo1 listfieldDemo1;

public ListCallback(ListfieldDemo1 listfieldDemo1)
{
this.listfieldDemo1 = listfieldDemo1;
}

public void drawListRow(ListField listField, Graphics g, int index,
int y, int w) {

String text = (String)_listElements.elementAt(index);
g.drawText(text, 60, y + 5, 0, w);
text = (String) _listElements.elementAt(index);
Bitmap bitm = Bitmap.getBitmapResource("bullet_arrow.png");
int xpos = LEFT_OFFSET;
int ypos = TOP_OFFSET + y;
w = bitm.getWidth();
int h = bitm.getHeight();

g.drawBitmap(xpos, ypos, w, h, bitm, 0, 0);

xpos = w + 20;


}

public Object get(ListField listField, int index) {
// TODO Auto-generated method stub
return null;
}

public int getPreferredWidth(ListField listField) {
// TODO Auto-generated method stub
return 0;
}

public int indexOfList(ListField listField, String prefix, int start) {
// TODO Auto-generated method stub
return 0;
}

}
}

提前致谢。

最佳答案

我想你想要像下图这样的东西:

List Test With Tick Mark

代码如下:

import java.util.Vector;
import net.rim.device.api.system.Bitmap;
import net.rim.device.api.system.Display;
import net.rim.device.api.ui.Field;
import net.rim.device.api.ui.Graphics;
import net.rim.device.api.ui.Manager;
import net.rim.device.api.ui.MenuItem;
import net.rim.device.api.ui.UiApplication;
import net.rim.device.api.ui.component.LabelField;
import net.rim.device.api.ui.component.ListField;
import net.rim.device.api.ui.component.ListFieldCallback;
import net.rim.device.api.ui.component.Menu;
import net.rim.device.api.ui.component.SeparatorField;
import net.rim.device.api.ui.container.MainScreen;

public final class ListDemoScreen extends MainScreen {

private Vector _listElements;

ListField list;
private ListField _checkList;
private MenuItem _toggleItem;

public ListDemoScreen() {
super(Manager.NO_VERTICAL_SCROLL);
// Set the displayed title of the screen
setTitle("List Demo");
add(new LabelField("Fruits List", LabelField.FIELD_HCENTER));
add(new SeparatorField());

_listElements = new Vector();
add(new SeparatorField());
list = new ListField();
ListCallback _callback = new ListCallback(this);

list.setCallback(_callback);
list.setSize(4);

add(list);

createField();

}

protected void createField() {
ChecklistData itemOneCheckList = new ChecklistData("Apple", false);
ChecklistData itemTwoCheckList = new ChecklistData("Blackberry", false);
ChecklistData itemThreeCheckList = new ChecklistData("Grapes", false);
ChecklistData itemFourCheckList = new ChecklistData("Banana", false);

_listElements.addElement(itemOneCheckList);
_listElements.addElement(itemTwoCheckList);
_listElements.addElement(itemThreeCheckList);
_listElements.addElement(itemFourCheckList);
reloadList();

}

private void reloadList() {
list.setSize(_listElements.size());
}

public boolean invokeAction(int action) {
switch (action) {
case ACTION_INVOKE: // Trackball click.
int index = list.getSelectedIndex();
ChecklistData data = (ChecklistData) _listElements.elementAt(index);
data.toggleChecked();
_listElements.setElementAt(data, index);
list.invalidate(index);
return true; // We've consumed the event.
}
return super.invokeAction(action);

}

class ListCallback implements ListFieldCallback {
ListDemoScreen listDemoScreen;

public ListCallback(ListDemoScreen listDemoScreen) {
this.listDemoScreen = listDemoScreen;

}

public void drawListRow(ListField list, Graphics g, int index, int y,
int w) {

ChecklistData data = (ChecklistData) _listElements.elementAt(index);

Bitmap bitmapImage = null;
Bitmap bitmapTick = null;
int widthImage=0;
int heightImage=list.getRowHeight(index);
int widthTick=0;
int heightTick=list.getRowHeight(index);;
int maxHeight = list.getRowHeight(index);

int xpos=0;
int ypos=y;
bitmapImage = Bitmap.getBitmapResource("earth-icon.png");
bitmapTick = Bitmap.getBitmapResource("ok-icon.png");
if(bitmapImage != null && bitmapTick != null) {
widthImage = bitmapImage.getWidth();
widthTick = bitmapTick.getWidth();
heightImage = bitmapImage.getHeight();
heightTick = bitmapTick.getHeight();
maxHeight = (heightTick > heightImage) ? heightTick : heightImage;

list.setRowHeight(index, maxHeight);

g.drawBitmap( xpos, ypos+(list.getRowHeight(index)-heightImage)/2, widthImage, heightImage, bitmapImage, 0, 0 );
if(data.isChecked()) {
g.drawBitmap( getWidth()-widthTick-2, ypos+(list.getRowHeight(index)-heightTick)/2, widthTick, heightTick, bitmapTick, 0, 0 );
}
}

ChecklistData currentRow = (ChecklistData) this.get(list, index);

StringBuffer rowString = new StringBuffer();

rowString.append(currentRow.getStringVal());
// Draw the text.
g.drawText(rowString.toString(), xpos + widthImage, y + (list.getRowHeight(index)-getFont().getHeight())/2, 0, -1);
}

public Object get(ListField list, int index) {
return _listElements.elementAt(index);
}

public int indexOfList(ListField list, String prefix, int string) {
return _listElements.indexOf(prefix, string);
}

public int getPreferredWidth(ListField list) {
return Display.getWidth();
}

}

private class ChecklistData {
private String _stringVal;
private boolean _checked;


ChecklistData(String stringVal, boolean checked) {
_stringVal = stringVal;
_checked = checked;
}

private String getStringVal() {
return _stringVal;
}

private boolean isChecked() {
return _checked;
}


// Toggle the checked status.
private void toggleChecked() {
_checked = !_checked;
}

}


protected void makeMenu(Menu menu, int instance) {
Field focus = UiApplication.getUiApplication().getActiveScreen()
.getLeafFieldWithFocus();
if (focus == _checkList) {
menu.add(_toggleItem);
}

super.makeMenu(menu, instance);
}

}

earth-icon.png 使用您自己的图标ok-icon.png (勾选标记)。

关于blackberry - ListField 中文本右侧的复选框,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10517065/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com