2012-07-04 117 views
2

Sample img大家好我基本上是Android開發者。我是黑莓發展的新手。我需要使用圖像按鈕創建自定義文本框。黑莓的CustomTextbox

在文本框的右上角,我想要小圖像按鈕,它是單擊偵聽器,文本框字段應該是空的。

我可以創建自定義文本框並在文本框內繪製位圖。但我無法抓住焦點和聽衆的圖像按鈕。請幫我

請給點意見和樣品。

我想這...

MyApp.java類:

import net.rim.device.api.ui.UiApplication; 

公共類MyApp的擴展UIApplication的{

public static void main(String[] args) { 

    MyApp theApp = new MyApp(); 
    theApp.enterEventDispatcher(); 
} 

public MyApp() { 
    pushScreen(new MyScreen()); 
} 

}

MyScreen.java類:

public final class MyScreen extends MainScreen { 
public MyScreen() { 
    super(Manager.NO_VERTICAL_SCROLL); 
    setTitle("MyTitle"); 

    VerticalFieldManager vfm = new VerticalFieldManager(
      Manager.USE_ALL_HEIGHT | Manager.USE_ALL_HEIGHT); 
    HorizontalFieldManager hfm = new HorizontalFieldManager(
      Manager.USE_ALL_WIDTH); 
    HorizontalFieldManager hfm1 = new HorizontalFieldManager(
      Manager.USE_ALL_WIDTH); 
    customManager ctm = new customManager(Manager.USE_ALL_WIDTH); 

    customManager ctm1 = new customManager(Manager.USE_ALL_WIDTH); 

    hfm.add(ctm); 
    hfm1.add(ctm1); 
    vfm.add(hfm); 
    vfm.add(hfm1); 
    add(vfm); 
} 

}

customManager.java類別:

public class customManager extends Manager implements FieldChangeListener { 

private Textbox txt; 
private Closebtn cls; 

Bitmap bitmap; 

protected customManager(long style) { 
    super(style); 

    // My Coustem TextBOX 
    txt = new Textbox(300, 100); 
    // My Coustem Button 
    cls = new Closebtn(); 

    cls.setChangeListener(this); 
    add(txt); 
    add(cls); 
} 

protected void sublayout(int width, int height) { 

    setPositionChild(getField(0), 10, 10); 
    layoutChild(getField(0), getField(0).getPreferredWidth(), getField(0) 
      .getPreferredHeight()); 

    setPositionChild(getField(1), 
      getField(0).getWidth() - (getField(1).getWidth()), getField(0) 
        .getHeight()/2 - getField(1).getHeight()/2); 
    layoutChild(getField(1), getField(1).getWidth(), getField(1) 
      .getHeight()); 

    setExtent(width, height); 
} 

public void fieldChanged(Field field, int context) { 
    txt.setText(""); 

} 

}

Textbox.java類別:

public class Textbox extends Manager { 
private int managerWidth; 
private int managerHeight; 
private int arcWidth; 

private VerticalFieldManager vfm = new VerticalFieldManager(
     NO_VERTICAL_SCROLL | USE_ALL_WIDTH); 
private EditField editField; 
private Bitmap bagBitmap; 

Textbox(int width, int height, long style) { 
    super(style | NO_VERTICAL_SCROLL | NO_HORIZONTAL_SCROLL); 
    managerWidth = width; 
    managerHeight = height; 
    long innerStyle = style & (READONLY | FOCUSABLE_MASK); // at least 
    if (innerStyle == 0) { 
     innerStyle = FOCUSABLE; 
    } 
    editField = new EditField("", "", 10, innerStyle); 

    arcWidth = editField.getFont().getHeight() & 0xFFFFFFFE; // make it even 

    EncodedImage en = EncodedImage.getEncodedImageResource("_text.png"); 

    bagBitmap = Util.getScaledBitmapImage(en, width, height); 
    add(vfm); 
    vfm.add(editField); 
} 

public void setFont(Font font) { 
    super.setFont(font); 
    editField.setFont(font); 
    arcWidth = editField.getFont().getHeight() & 0xFFFFFFFE; 
    updateLayout(); 
} 



Textbox(int width, int height) { 
    this(width, height, 0L); 
} 

public String getText() { 
    return editField.getText(); 
} 

public void setText(String newText) { 
    editField.setText(newText); 
} 

public int getPreferredWidth() { 
    return managerWidth; 
} 

public int getPreferredHeight() { 
    return managerHeight; 
} 

protected void sublayout(int w, int h) { 
    if (managerWidth == 0) { 
     managerWidth = w; 
    } 
    if (managerHeight == 0) { 
     managerHeight = h; 
    } 
    int actWidth = Math.min(managerWidth, w); 
    int actHeight = Math.min(managerHeight, h); 
    layoutChild(vfm, actWidth - arcWidth, actHeight - arcWidth); 
    setPositionChild(vfm, arcWidth/2, arcWidth/2); 
    setExtent(actWidth, actHeight); 
} 

protected void paint(Graphics g) { 

    g.drawBitmap(0, 0, getWidth(), getHeight(), bagBitmap, 0, 0); 

    super.paint(g); 
} 

}

Closebtn.java類別:

public class Closebtn extends Field { 

private Bitmap bitmap; 

public Closebtn() { 
    super(Manager.FOCUSABLE); 

    EncodedImage en = EncodedImage.getEncodedImageResource("close.png"); 

    bitmap = Util.getScaledBitmapImage(en, 50, 50); 

} 

protected void layout(int width, int height) { 
    setExtent(bitmap.getWidth(), bitmap.getHeight()); 
} 

protected void paint(Graphics graphics) { 
    graphics.drawBitmap(0, 0, bitmap.getWidth(), bitmap.getHeight(), 
      bitmap, 0, 0); 
} 

protected void onFocus(int direction) { 
    bitmap = bitmap; 

} 

protected void onUnfocus() { 
    bitmap = bitmap; 
} 

protected boolean keyChar(char character, int status, int time) { 
    if (character == Characters.ENTER) { 
     clickButton(); 
     return true; 
    } 
    return super.keyChar(character, status, time); 
} 

protected boolean navigationClick(int status, int time) { 
    clickButton(); 
    return true; 
} 

protected boolean trackwheelClick(int status, int time) { 
    clickButton(); 
    return true; 
} 

protected boolean invokeAction(int action) { 
    switch (action) { 
    case ACTION_INVOKE: { 
     clickButton(); 
     return true; 
    } 
    } 
    return super.invokeAction(action); 
} 

public void setDirty(boolean dirty) { 
} 

public void setMuddy(boolean muddy) { 
} 

public void clickButton() { 
    fieldChangeNotify(0); 
} 

}

Util.java類:

public class Util { 

public static Bitmap getScaledBitmapImage(EncodedImage image, int width, 
     int height) { 

    if (image == null) { 
     return null; 
    } 

    int currentWidthFixed32 = Fixed32.toFP(image.getWidth()); 
    int currentHeightFixed32 = Fixed32.toFP(image.getHeight()); 

    int requiredWidthFixed32 = Fixed32.toFP(width); 
    int requiredHeightFixed32 = Fixed32.toFP(height); 

    int scaleXFixed32 = Fixed32.div(currentWidthFixed32, 
      requiredWidthFixed32); 
    int scaleYFixed32 = Fixed32.div(currentHeightFixed32, 
      requiredHeightFixed32); 

    image = image.scaleImage32(scaleXFixed32, scaleYFixed32); 

    return image.getBitmap(); 
} 

}

我的問題是我無法在這裏補充一個以上的域請幫助我..

+0

你加入這兩個領域,但你只能看到一個,因爲在你的'Textbox'類,管理與標誌'USE_ALL_HEIGHT'創建。刪除它,你應該能夠看到兩者。你也忘了添加按鈕。 –

+0

嗨史密斯,你可以解釋一下更多我刪除了文本框中的USE_ALL_HEIGHT類,並且我還在自定義管理器類中添加了文本框和按鈕字段.. – prakash

+0

現在我再次閱讀它,似乎您忘記將hfm1添加到VFM。 –

回答

1

試試這個自定義類:

public class TextFieldWithClear extends HorizontalFieldManager { 
protected HorizontalFieldManager hfmEditTextPanel; 
protected LabelField lblEditText; 
protected EditField textField; 
protected MyImageButton bitmapFieldClear; 
int mHeight; 
int mWidth; 
String mLabel; 

public TextFieldWithClear(String label, int width, int height) { 
    super(FOCUSABLE); 

    Border border = BorderFactory 
      .createSimpleBorder(new XYEdges(2, 2, 2, 2)); 
    this.setBorder(border); 
    Background bg = BackgroundFactory.createSolidBackground(Color.WHITE); 
    this.setBackground(bg); 

    mWidth = width; 
    mHeight = height; 
    mLabel = label; 

    lblEditText = new LabelField(mLabel) { 
     protected void paint(Graphics graphics) { 
      graphics.setColor(0x4B4B4B); 
      super.paint(graphics); 
     } 
    }; 
    add(lblEditText); 

    hfmEditTextPanel = new HorizontalFieldManager(FOCUSABLE 
      | VERTICAL_SCROLL | VERTICAL_SCROLLBAR) { 
     protected void sublayout(int maxWidth, int maxHeight) { 
      maxWidth = mWidth - 30; 
      maxHeight = mHeight; 
      super.sublayout(maxWidth, maxHeight); 
      setExtent(maxWidth, maxHeight); 
     } 
    }; 

    textField = new EditField() { 
     // protected void layout(int width, int height) 
     // { 
     // width = mWidth - 50; 
     // height=35; 
     // super.layout(width, height); 
     // //setExtent(width, height); 
     // } 
    }; 
    hfmEditTextPanel.add(textField); 
    add(hfmEditTextPanel); 
    bitmapFieldClear = new MyImageButton(
      Bitmap.getBitmapResource("btn_delete_normal.png"), 
      Bitmap.getBitmapResource("btn_delete_focused.png")); 
    bitmapFieldClear.setChangeListener(buttonListener); 
    add(bitmapFieldClear); 
} 

public String getText() { 
    String value = ""; 
    if (textField.getText().length() > 0) 
     value = textField.getText(); 
    return value; 

} 

public void setString(String value) { 
    if (value != null) { 
     textField.setText(value); 
    } 
} 

FieldChangeListener buttonListener = new FieldChangeListener() { 
    public void fieldChanged(Field field, int context) { 
     textField.clear(0); 
     textField.setFocus(); 

    } 
}; 
public void onUndisplay() 
{ 
    textField.setEditable(false); 
} 
public void onDisplay() 
{ 
    textField.setEditable(true); 
} 
} 
1

這並不容易,只是覆蓋EditField,所以這是我想嘗試的:

  • 使用水平管理器(例如,HorizontalFieldManager或其他自定義管理器,可能具有固定的列寬。這位經理將在內部有兩個字段:左側是EditField,右側是自定義按鈕字段。
  • 設置一個Background給經理。 BG將繪製綠色背景以及藍色邊框。您可以使用縮放的位圖(看看BackgroundFactory.createBitmapBackground)。
  • 創建一個新的EditField子類,並覆蓋其paintBackground方法,以便它什麼都不做。如果它不起作用,請嘗試覆蓋paint,以便它僅繪製文本。這是最棘手的部分。
  • 用灰色圓圈圖像創建自定義Buttonfield子類。你可以閱讀一個關於如何做到這一點的好教程here。您也可以在Advanced Ui Library中獲得BitmapButtonField。點擊按鈕後,它將在EditField上調用EditField.setText("")
+0

好史密斯,我試試你的方式,讓你知道 – prakash

+0

我編輯了我的問題......請看看,給我你的想法 – prakash