3
나는 우리 자신의 높이와 너비를 설정할 수있는 블랙 베리 CustomButtonField를 만들었습니다. 문제는 내가 직면 한 것은 단추의 중앙에 레이블을 표시하는 방법을 모른다는 것입니다.Blackberry - 사용자 지정 ButtonField 가운데에 레이블을 그립니다.
나는 우리 자신의 높이와 너비를 설정할 수있는 블랙 베리 CustomButtonField를 만들었습니다. 문제는 내가 직면 한 것은 단추의 중앙에 레이블을 표시하는 방법을 모른다는 것입니다.Blackberry - 사용자 지정 ButtonField 가운데에 레이블을 그립니다.
페인트 방법을 사용하여 레이블 레이아웃, 색상 및 글꼴을 수정할 수 있습니다.
alt text http://img9.imageshack.us/img9/8017/custombutton.jpg
참조 예 : 사용
class CustomButton extends ButtonField {
int mHeight;
int mWidth;
public CustomButton(int height, int width, String label) {
super(label, CONSUME_CLICK);
mHeight = height;
mWidth = width;
}
public int getPreferredHeight() {
return mHeight;
}
public int getPreferredWidth() {
return mWidth;
}
protected void layout(int width, int height) {
super.layout(mWidth, mHeight);
setExtent(mWidth, mHeight);
}
protected void paint(Graphics graphics) {
graphics.setColor(Color.WHITE);
String label = getLabel();
int x = (getPreferredWidth() - getFont().getAdvance(label)) >> 1;
int y = (getPreferredHeight() - getFont().getHeight()) >> 1;
graphics.drawText(label, x, y);
}
}
예 :
class Scr extends MainScreen implements FieldChangeListener {
CustomButton button1;
CustomButton button2;
CustomButton button3;
public Scr() {
add(button1 = new CustomButton(15, 60, "first"));
button1.setChangeListener(this);
add(button2 = new CustomButton(30, 120, "second"));
button2.setChangeListener(this);
add(button3 = new CustomButton(50, 200, "third"));
button3.setChangeListener(this);
}
public void fieldChanged(Field field, int context) {
if (field == button1)
Dialog.inform("first");
if (field == button2)
Dialog.inform("second");
if (field == button3)
Dialog.inform("third");
}
}