2012-06-19 2 views
2

BlackBerry에서 클릭 이벤트 중에 배경색을 변경하는 방법은 무엇입니까? 예를 들어 길게 누르면 배경색을 변경해야합니다. 나를 위해 그것은 기본 색 파란색 걸립니다. 그것을 바꾸는 방법?BlackBerry에서 클릭 이벤트 중에 ButtonField 배경을 변경하려면

이것은 사용자 정의 buttton 필드입니다. 그러나 버튼 클릭 이벤트의 기본 청색을 표시합니다.

public class CustomButtonField extends ButtonField implements GlobalConstant { 
int mHeight; 
int mWidth; 
public final static int DEFAULT_BACKGROUND_COLOR_NORMAL = 0x167c9c; 
public final static int DEFAULT_BACKGROUND_COLOR_ON_FOCUS = 0x188118; 
private int backgroundColorNormal = DEFAULT_BACKGROUND_COLOR_NORMAL; 
private int backgroundColorOnFocus = DEFAULT_BACKGROUND_COLOR_ON_FOCUS; 
private Background noraml_bg; 
private Background focus_bg; 
private boolean isFocusable; 
private boolean isround_button = false; 


public CustomButtonField(int height, int width, String label) { 
    super(label, CONSUME_CLICK); 

    noraml_bg = menuButton_bgNormal; 
    focus_bg = menuButton_bgFocus; 

    mHeight = height; 
    mWidth = width; 
    this.isFocusable = true; 
    setBorder(BorderFactory.createSimpleBorder(new XYEdges(0, 0, 0, 0))); 
    setBorder(VISUAL_STATE_ACTIVE, 
      BorderFactory.createSimpleBorder(new XYEdges(0, 0, 0, 0))); 

} 

public CustomButtonField(int height, int width, String label, boolean isround_button) { 
    super(label, CONSUME_CLICK); 

    this.isround_button = isround_button; 
    noraml_bg = roundButton_bgNormal; 
    focus_bg = roundButton_bgFocus; 
    mHeight = height; 
    mWidth = width; 
    this.isFocusable = true; 

    XYEdges padding = new XYEdges(1,1,1,1); 
    XYEdges color = new XYEdges (Color.BLACK,Color.BLACK,Color.BLACK,Color.BLACK); 
    int lineStyle = Border.STYLE_SOLID; 

    Border roundedBorder = BorderFactory.createSimpleBorder(padding, color, lineStyle); 
    setBorder(roundedBorder); 

} 

/* 
* (non-Javadoc) 
* 
* @see net.rim.device.api.ui.component.ButtonField#getPreferredHeight() 
*/ 
public int getPreferredHeight() { 
    return mHeight; 
} 

/* 
* (non-Javadoc) 
* 
* @see net.rim.device.api.ui.component.ButtonField#getPreferredWidth() 
*/ 
public int getPreferredWidth() { 
    return mWidth; 
} 

/* 
* (non-Javadoc) 
* 
* @see net.rim.device.api.ui.component.ButtonField#layout(int, int) 
*/ 
protected void layout(int width, int height) { 
    super.layout(mWidth, mHeight); 
    setExtent(mWidth, mHeight); 
} 

/* 
* (non-Javadoc) 
* 
* @see 
* net.rim.device.api.ui.component.ButtonField#paint(net.rim.device.api. 
* ui.Graphics) 
*/ 
protected void paint(Graphics graphics) { 

    String label = getLabel(); 
    int x = (getPreferredWidth() - getFont().getAdvance(label)) >> 1; 
    int y = (getPreferredHeight() - getFont().getHeight()) >> 1; 
    if (isFocus() == false) { 
     this.setBackground(noraml_bg); 
     if(isround_button){ 
      graphics.setColor(0x666666); 
     }else{ 
      graphics.setColor(Color.WHITE); 
     } 

     graphics.drawText(label, x, y); 
    } else { 
     this.setBackground(focus_bg); 
     graphics.setColor(Color.WHITE); 

     graphics.drawText(label, x, y); 
    } 
} 

protected void drawFocus(Graphics graphics, boolean on) { 
    if (on) { 
     graphics.setColor(backgroundColorOnFocus); 
    } else { 
     graphics.setColor(backgroundColorNormal); 
    } 
} 

public boolean isFocusable() { 
    return isFocusable; 
} 

은}

답변

8
Field의 시각적 상태 표시기를 사용하여

BackgroundFactory 당신이 시각적 상태를 다음과 같은 사항에 대해 Background을 설정할 수 있습니다

  • VISUAL_STATE_ACTIVE-활성 표시 상태. 사용자가 필드와 상호 작용하고 있습니다.
  • VISUAL_STATE_DISABLED - 비활성화 된 시각적 상태. 가능한 필드와의 상호 작용이 없습니다.
  • VISUAL_STATE_DISABLED_FOCUS-사용하지 않지만 초점을 맞춘 시각적 상태. 필드가 강조 표시되지만 필드와 다른 가능한 상호 작용은 없습니다.
  • VISUAL_STATE_FOCUS-초점 시각적 상태. 필드에 포커스가 있습니다 (강조 표시됨).
  • VISUAL_STATE_NORMAL - 정상적인 시각적 상태. 현장과의 현재 상호 작용이 없습니다.


확인 다음 코드 :

ButtonField bfTest = new ButtonField("Button Field"); 

Background commonBgOne = BackgroundFactory.createSolidBackground(Color.RED); 
Background commonBgTwo = BackgroundFactory.createSolidBackground(Color.GREEN); 

bfTest.setBackground(VISUAL_STATE_ACTIVE, commonBgOne); 
bfTest.setBackground(VISUAL_STATE_DISABLED, commonBgTwo); 
bfTest.setBackground(VISUAL_STATE_DISABLED_FOCUS, commonBgTwo); 
bfTest.setBackground(VISUAL_STATE_FOCUS, commonBgOne); 
bfTest.setBackground(VISUAL_STATE_NORMAL, commonBgTwo); 


취소 기본 테두리

Border commonBorder = BorderFactory.createSimpleBorder(new XYEdges()); 

bfTest.setBorder(VISUAL_STATE_ACTIVE, commonBorder); 
bfTest.setBorder(VISUAL_STATE_DISABLED, commonBorder); 
bfTest.setBorder(VISUAL_STATE_DISABLED_FOCUS, commonBorder); 
bfTest.setBorder(VISUAL_STATE_FOCUS, commonBorder); 
bfTest.setBorder(VISUAL_STATE_NORMAL, commonBorder); 
+0

안녕하세요 Rupak, 귀하의 대답은 일반 및 포커스 상태에 대한 배경색을 변경하는 것입니다 ....하지만 버튼 클릭 이벤트 중 하나 더 색상을 표시 .... 이전 응답 .... 감사합니다. –

+0

실제로 있음 우리의 응용 프로그램, 사용자 정의 단추 필드에 대해 우리는 0x2e7594 및 0x134f74 그라데이션 색상 코드를 보통으로 설정합니다. 초점을위한 0x8cc10b & 0x4e7500 색상 그라디언트 색상 코드. 버튼 클릭 이벤트의 경우 기본 색상은 세 번째 색상으로 표시됩니다. 따라서 기본 파란색을 포커스 그라디언트 색상 (0x8cc10b 및 0x4e7500)으로 변경해야합니다. 클릭 이벤트의 버튼 기본 파란색을 변경하는 방법 –

+0

위에 게시 된 코드를 사용해 보셨습니까? 이것은 하나의 예이며 Field의 모든 가능한 시각적 상태에 대한 배경을 ButtonField로 설정합니다. 코드를 게시하지 않으면이 문제와 관련하여 도움을 줄 수 없습니다. 그리고 저는 여러분이 'VISUAL_STATE_ACTIVE'에 대해서 이야기하고 있다고 생각합니다. – Rupak

2

당신은 버튼의 setBackground의 속성을 사용하여 시도?

+0

안녕 악사, 예 나는 그것을 시도 ... 우리는 설정 r에 배경에 3 가지 색상입니다. 보통의 경우 1 회, 클릭하는 경우 2 회, 초점을 위해 3 회. 노멀과 포커스의 배경색을 변경했습니다. 그러나 클릭 이벤트를 설정할 수 없습니다. –

+0

코드를 공유하지 않는 이유는 무엇입니까? – Rupak

관련 문제