Domanda

Il campo ObjectChoiceField soddisfa tutti i miei requisiti ma non è carino.

Questo è quello che ho:

String pets[] = {"Dog", "Cat", "Duck" };
ObjectChoiceField dd = new ObjectChoiceField("My Pet",pets,0,ObjectChoiceField.FIELD_LEFT);

Ma preferirei avere delle foto nel menu a discesa. La mia comprensione è che l'array di oggetti deve contenere oggetti che supportano il metodo toString. Dev'esserci un modo per farlo. Lo vedo in altre app, non riesco a trovare l'oggetto corretto nell'API.

Non deve essere un ObjectChoiceField.

È stato utile?

Soluzione

Vorrei utilizzare ButtonField e PopupScreen personalizzati. Due motivi:

  • nella progettazione mobile è meglio utilizzare lo schermo in modo efficace e il controllo a discesa del desktop classico sembra essere meno sufficiente del popup, almeno per elementi complessi (immagine + testo)
  • è più facile :)

alt text http://img405.imageshack.us/img405/3746/dropdown .jpg

DropDownItem:

class DropdownItem {
 Bitmap mBitmap;
 String mName;

 public DropdownItem(Bitmap bitmap, String name) {
  this.mBitmap = bitmap;
  this.mName = name;
 }
}

Campo personalizzato:

class BitmapButtonField extends ButtonField {
 protected DropdownItem mItem;
 boolean mTextItem;
 int mWidth;
 int mHeight;

 public BitmapButtonField(DropdownItem item, boolean textItem) {
  super(CONSUME_CLICK);
  mItem = item;
  mTextItem = textItem;
  mWidth = mItem.mBitmap.getWidth() + 6
    + (mTextItem ? getFont().getAdvance(mItem.mName) + 6 : 0);
  mHeight = mItem.mBitmap.getHeight() + 6;
  setMargin(0, 0, 0, 0);
  setPadding(0, 0, 0, 0);
  setBorder(BorderFactory.createSimpleBorder(new XYEdges(0, 0, 0, 0)));
  setBorder(VISUAL_STATE_ACTIVE, BorderFactory
    .createSimpleBorder(new XYEdges(0, 0, 0, 0)));
 }

 protected void paint(Graphics graphics) {
  int color = (getVisualState() == VISUAL_STATE_FOCUS) ? Color.LIGHTGREY
    : Color.DARKGRAY;
  graphics.setColor(color);
  graphics.drawRect(1, 1, mWidth - 2, mHeight - 2);
  graphics.drawBitmap(3, 3, mItem.mBitmap.getWidth(), mItem.mBitmap
    .getHeight(), mItem.mBitmap, 0, 0);
  if (mTextItem)
   graphics.drawText(mItem.mName, mItem.mBitmap.getWidth() + 6, 3);

 }

 public int getPreferredWidth() {
  return mWidth;
 }

 public int getPreferredHeight() {
  return mHeight;
 }

 protected void layout(int width, int height) {
  setExtent(mWidth, mHeight);
 }
}

Controllo a discesa stesso:

class DDImagesButton extends BitmapButtonField implements FieldChangeListener {
 DropdownItem[] mItems;
 int mIndex;

 public DDImagesButton(DropdownItem[] items) {
  super(items[0], false);
  mItems = items;
  updateIndex(0);
  setChangeListener(this);
 }

 protected void paint(Graphics graphics) {
  super.paint(graphics);

  int x = mItems[mIndex].mBitmap.getWidth() + 2;
  int y = 5;

  int y1 = y;
  int y2 = y + 10;
  int x1 = x;
  int x2 = x + 18;
  int[] xPts = new int[] { x1, x2, x1 + 9 };
  int[] yPts = new int[] { y1, y1, y2 };
  graphics.drawFilledPath(xPts, yPts, null, null);
 }

 public void fieldChanged(Field field, int context) {
  getScreen().getUiEngine().pushScreen(new DDImagesPopUp());
 }

 public void updateIndex(int index) {
  mIndex = index;
  mItem = mItems[mIndex];
  mWidth = mItem.mBitmap.getWidth() + 6 + 18 + 3;
  mHeight = mItem.mBitmap.getHeight() + 6;
  invalidate();
 }

 class DDImagesPopUp extends PopupScreen implements FieldChangeListener {

  public DDImagesPopUp() {
   super(
     new VerticalFieldManager(VERTICAL_SCROLL
       | VERTICAL_SCROLLBAR));
   for (int i = 0; i < mItems.length; i++) {
    BitmapButtonField button = new BitmapButtonField(mItems[i],
      true);
    add(button);
    button.setChangeListener(this);
   }
   setFieldWithFocus(getField(mIndex));
  }

protected boolean keyChar(char key, int status, int time) {
    if (Keypad.KEY_ESCAPE == key) {
        this.close();
        return true;
    } else
        return super.keyChar(key, status, time);
}

  public void fieldChanged(Field field, int context) {
   updateIndex(getFieldWithFocusIndex());
   close();
  }
 }
}

Esempio di utilizzo:

class Scr extends MainScreen {
 DDImagesButton ddImages1;
 DDImagesButton ddImages2;

 public Scr() {
  HorizontalFieldManager hfm = new HorizontalFieldManager();
  add(hfm);

  DropdownItem[] items = new DropdownItem[6];
  items[0] = new DropdownItem(Bitmap.getBitmapResource("1.png"),
    "Add Item");
  items[1] = new DropdownItem(Bitmap.getBitmapResource("2.png"),
    "Attachment");
  items[2] = new DropdownItem(Bitmap.getBitmapResource("3.png"), "Time");
  items[3] = new DropdownItem(Bitmap.getBitmapResource("4.png"), "User");
  items[4] = new DropdownItem(Bitmap.getBitmapResource("5.png"), "Group");
  items[5] = new DropdownItem(Bitmap.getBitmapResource("6.png"),
    "Information");
  ddImages1 = new DDImagesButton(items);
  hfm.add(ddImages1);

  ddImages2 = new DDImagesButton(items);
  hfm.add(ddImages2);
 }
}

Altri suggerimenti

Non ho familiarità con lo sviluppo di Blackberry ma credo che potresti sottoclassare ObjectChoiceField e sovrascrivere i metodi layout(int, int) e paint(Graphics).

E poi in drawImage(...) forse usare il metodo <=> dell'oggetto Graphics passato per disegnare l'immagine.

Solo un'ipotesi selvaggia

La mia risposta sarà sulla falsariga di risposta del jitter . L'idea generale per il tipo di personalizzazioni desiderate è quella di sostituire il comportamento predefinito dei componenti di base.

Supponiamo che le scelte che desideri visualizzare possano essere incapsulate da una classe denominata Choice dichiarata come segue:

private class Choice
{
  public Bitmap image;
  public String label;

  public Choice(String name)
  {
    this.image = Bitmap.getBitmapResource(name + ".png");
    this.label = name;
  }

  public String toString()
  {
    return this.label;
  }
}

quindi puoi dichiarare un'istanza ObjectListField come:

ObjectChoiceField choice = new ObjectChoiceField()
{      
  protected void paint(Graphics graphics)
  {
    // Get the current selected Choice
    Choice item = (Choice) this.getChoice(getSelectedIndex());

    int xOffset = 5; // 5 px padding on the left
    graphics.drawBitmap(xOffset, 0, 
                        item.image.getWidth(), 
                        item.image.getHeight(), 
                        item.image, 
                        0, 0);
    // Add text after the image and 10px padding.
    xOffset += item.image.getWidth() + 10; 
    graphics.drawText(item.label, xOfffset, 0);
  }            
};

imposta gli elementi scelti come:

choice.setChoices(new Choice[]{ new Choice("choice 1"), new Choice("choice 2")});

e quindi aggiungilo al tuo Screen (o FieldManager di tua scelta) utilizzando:

add(choice);

Non sono stato in grado di sovrascrivere le voci del menu a comparsa della selezione effettiva. Questo sembra chiamare il metodo toString() dei tuoi articoli scelti. Ecco perché ho ignorato l'implementazione predefinita di <=> nella classe <=> in modo da poter visualizzare i nomi logici in quel pop-up.

Se si tratta di un widget Java, è probabile che tu possa passare un semplice HTML come elementi da visualizzare nel campo di scelta (o che toString () restituisca un semplice HTML). Quindi, in tal caso e si passa un URL immagine / percorso relativo, l'immagine deve essere visualizzata. AFAIK che funzionerebbe almeno in Swing, ad esempio ...

quot &; Lt &; html gt &; Cane & Lt; img src = " dog.png " > < / Html gt &; Quot &;

(spazi aggiunti per la visualizzazione del codice in anteprima)

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top