gpt4 book ai didi

java - 如何在黑莓中的 hfm 中对齐文本字段和按钮字段

转载 作者:太空宇宙 更新时间:2023-11-04 15:02:34 25 4
gpt4 key购买 nike

我想向 Horizo​​ntalFieldManager 添加一个 TextField 和一个 ButtonField。现在,我使用以下代码

HorizontalFieldManager hfmFieldManager = new HorizontalFieldManager(NO_VERTICAL_SCROLL | NO_HORIZONTAL_SCROLL  | USE_ALL_WIDTH);
hfmFieldManager.setPadding(5, 0, 5, 0);

VerticalFieldManager buttonFieldManager = new VerticalFieldManager(USE_ALL_WIDTH | Field.FIELD_RIGHT);
ButtonField btnDone = new ButtonField("DONE", FIELD_VCENTER | Field.FIELD_RIGHT);
btnDone.setMargin(0, MARGIN, 0, MARGIN);
buttonFieldManager.add(btnDone);

TextField txtField = new TextField(FIELD_VCENTER | FIELD_LEFT)/*{
protected void layout(int width, int height) {
super.layout((int) (Display.getWidth() * 0.5) , getPreferredHeight());
}
}*/;
txtField.setBorder(roundedBorder);
txtField.setBackground(BackgroundFactory.createSolidBackground(Color.WHITE));
hfmFieldManager.add(txtField);
hfmFieldManager.add(buttonFieldManager);

但它仅显示 TextField。我想在右侧完全显示按钮字段,并在文本字段的剩余左侧显示。(从左到右显示文本字段、按钮字段)。

如何调整文本字段的宽度以兼容所有屏幕尺寸?

提前致谢

最佳答案

试试这个 -

JustifiedHorizontalFieldManager j=new JustifiedHorizontalFieldManager(your_text_field,your_button_field, true);

然后将该字段添加到您的屏幕。

//JustifiedHorizo​​ntalFieldManager类在下面给出

import net.rim.device.api.ui.Field;
import net.rim.device.api.ui.Manager;

public class JustifiedHorizontalFieldManager extends Manager
{
private static final int SYSTEM_STYLE_SHIFT = 32;

public Field _leftField;
public Field _rightField;

private boolean _giveLeftFieldPriority;

public JustifiedHorizontalFieldManager( Field leftField, Field rightField, boolean giveLeftFieldPriority )
{
this( leftField, rightField, giveLeftFieldPriority, Field.USE_ALL_WIDTH );
}

public JustifiedHorizontalFieldManager( Field leftField, Field rightField, boolean giveLeftFieldPriority, long style )
{
super( style );

_leftField = leftField;
_rightField = rightField;

add( _leftField );
add( _rightField );

_giveLeftFieldPriority = giveLeftFieldPriority;
}


public JustifiedHorizontalFieldManager( boolean giveLeftFieldPriority, long style )
{
super( style );
_giveLeftFieldPriority = giveLeftFieldPriority;
}

public void addLeftField( Field field )
{
if( _leftField != null ) {
throw new IllegalStateException();
}
_leftField = field;
add( _leftField );
}

public void addRightField( Field field )
{
if( _rightField != null ) {
throw new IllegalStateException();
}
_rightField = field;
add( _rightField );
}

public int getPreferredWidth()
{
return _leftField.getPreferredWidth() + _rightField.getPreferredWidth();
}

public int getPreferredHeight()
{
return Math.max( _leftField.getPreferredHeight(), _rightField.getPreferredHeight() );
}

protected void sublayout( int width, int height )
{
Field firstField;
Field secondField;
if( _giveLeftFieldPriority ) {
firstField = _leftField;
secondField = _rightField;
} else {
firstField = _rightField;
secondField = _leftField;
}

int maxHeight = 0;

int availableWidth = width;
availableWidth -= _leftField.getMarginLeft();
availableWidth -= Math.max( _leftField.getMarginRight(), _rightField.getMarginLeft() );
availableWidth -= _rightField.getMarginRight();

layoutChild( firstField, availableWidth, height - firstField.getMarginTop() - firstField.getMarginBottom() );
maxHeight = Math.max( maxHeight, firstField.getMarginTop() + firstField.getHeight() + firstField.getMarginBottom() );
availableWidth -= firstField.getWidth();

layoutChild( secondField, availableWidth, height - secondField.getMarginTop() - secondField.getMarginBottom() );
maxHeight = Math.max( maxHeight, secondField.getMarginTop() + secondField.getHeight() + secondField.getMarginBottom() );
availableWidth -= secondField.getWidth();

if( !isStyle( Field.USE_ALL_HEIGHT ) ) {
height = maxHeight;
}
if( !isStyle( Field.USE_ALL_WIDTH ) ) {
width -= availableWidth;
}

setPositionChild( _leftField, _leftField.getMarginLeft(), getFieldY( _leftField, height ) );
setPositionChild( _rightField, width - _rightField.getWidth() - _rightField.getMarginRight(), getFieldY( _rightField, height ) );

setExtent( width, height );
}

private int getFieldY( Field field, int height )
{
switch( (int)( ( field.getStyle() & FIELD_VALIGN_MASK ) >> SYSTEM_STYLE_SHIFT ) ) {
case (int)( FIELD_BOTTOM >> SYSTEM_STYLE_SHIFT ):
return height - field.getHeight() - field.getMarginBottom();
case (int)( FIELD_VCENTER >> SYSTEM_STYLE_SHIFT ):
return field.getMarginTop() + ( height - field.getMarginTop() - field.getHeight() - field.getMarginBottom() ) / 2;
default:
return field.getMarginTop();
}
}


public Field getLeftField()
{
return _leftField;
}

public Field getRightField()
{
return _rightField;
}

public void replace( Field oldField, Field newField )
{
if( oldField == newField ) {
// Nothing to do
return;
}

if( oldField == _leftField ) {
_leftField = newField;
} else if( oldField == _rightField ) {
_rightField = newField;
}
add( newField );
delete( oldField );
}

}

关于java - 如何在黑莓中的 hfm 中对齐文本字段和按钮字段,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22396239/

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