1

「画像ボタン」を持つ BlackBerry アプリケーションに取り組んでいます。「イメージ ボタン」は、クリックするたびに「オン」から「オフ」に、またはその逆に変更できます。今私がやろうとしているのは、アプリを閉じて再度ロードすると、この「画像ボタン」が最後の状態としてロードされるはずです。「画像ボタン」を「ON」に設定してアプリを閉じた場合、次回読み込み時に「ON」で読み込まれます。イメージ ボタンを作成するときに、値が「true」に設定されている場合、オフ/オンとして作成された場合でも、イメージは「オン」として読み込まれます。コンストラクターの外側で ImageButton を作成します。

LabeledSwitch onImg=new LabeledSwitch(off,on,off,on,true); 

次に、コンストラクター内で、最後の画像の状態を確認しようとしたため、それに応じて画像ボタンを再度作成しました。ただし、 (boolean)((Boolean) persistentHashtable.get("image")).booleanValue() は、正常にコンパイルされても CastException をスローします。

persistentObject = PersistentStore.getPersistentObject(KEY);

 if (persistentObject.getContents() == null) 
 {
    persistentHashtable = new Hashtable();
    persistentObject.setContents(persistentHashtable);
 } else {
    persistentHashtable = (Hashtable) persistentObject.getContents();
     }

  if (persistentHashtable.containsKey("image")) 
 {
     boolean booleanVal = (boolean)((Boolean) persistentHashtable.get("image")).booleanValue();
     if (booleanVal==true)
     {
         onImg=new LabeledSwitch(on,off,on,off,true);
     }
     else
     {
         onImg=new LabeledSwitch(off,on,off,on,false);
     }
 }

終了時に画像の状態を保存しています:

public boolean onClose() 
    {
        int choose=Dialog.ask(Dialog.D_YES_NO, "Are you sure Want to Exit?");
        if(choose==Dialog.YES)
        {
            if(onImg._on)
             persistentHashtable.put("image", Boolean.TRUE);

            else
                persistentHashtable.put("image", Boolean.FALSE); 
            System.exit(0);

        }
        return true;
    }

ガイドしてください。参考までに、画像ボタンの作成に使用される LabeledSwitch クラスを以下に示します。

import net.rim.device.api.system.*;
import net.rim.device.api.ui.*;

public class LabeledSwitch extends Field {
    private String _textOn;
    private String _textOff;

    private int _textWidth;
    private int _textHeight;

    private int _totalWidth;
    private int _totalHeight;

    private Bitmap _imageOn;
    private Bitmap _imageOnFocus;
    private Bitmap _imageOff;
    private Bitmap _imageOffFocus;

    public boolean _on; //chngd
    private boolean _selected;

    private Font _labelFont;

    private static MenuItem _changeOptionsItem = new ChangeOptionMenuItem();

    private int _textColour = 0x888888;
    private int _textColourFocus = 0x000000;

    private int _horizontalTextImageGap;

    private Bitmap _switchImage;  
    private String _labelText;  
    int mHeight;
    int mWidth;

    public LabeledSwitch(){}

    public LabeledSwitch( Bitmap imageOn
                , Bitmap imageOff
                , Bitmap imageOnFocus
                , Bitmap imageOffFocus
                , boolean onByDefault ) {
        super( Field.FIELD_VCENTER );

        //_textOn = textOn ="";
        //_textOff = textOff ="";

        _imageOn = imageOn;
        _imageOff = imageOff;
        _imageOnFocus = imageOnFocus;
        _imageOffFocus = imageOffFocus;
        _on = onByDefault; 
        _selected = false;

        _horizontalTextImageGap = _imageOn.getHeight() / 3;

    }

    public void applyFont() {
        _labelFont = getFont().derive( Font.PLAIN, _imageOn.getHeight()  );
    }

    /**
     * Change the state of the switch
     * @param on - if true, the switch will be set to on state
     */
    public void setOn(boolean on) {
        _on = on;
        invalidate();
    }

    public boolean getOnState() {
        return _on;
    }

    public boolean isFocusable() {
        return true;
    }

    public int getPreferredWidth() {
        return _totalWidth;
    }

    public int getPreferredHeight() {
        return _totalHeight;
    }

    protected void layout( int width, int height ) {
        // 
        _textWidth = Math.max( _labelFont.getAdvance( _textOn + "a" ), _labelFont.getAdvance( _textOff + "a" ) )-36;
        _textHeight = _labelFont.getHeight();

        _totalWidth = _imageOn.getWidth() + _horizontalTextImageGap + _textWidth;
        _totalHeight = _imageOn.getHeight();

        mWidth = getPreferredWidth();
        mHeight = getPreferredHeight();
        setExtent(mWidth, mHeight);

       // setExtent( _totalWidth, _totalHeight );
    }

    public void paint( Graphics g ){
        Font oldFont = g.getFont();
        int oldColor = g.getColor();

        try { 

            if( _on ) {
                _switchImage = g.isDrawingStyleSet(Graphics.DRAWSTYLE_FOCUS) ? _imageOnFocus : _imageOn;
            } else {
                _switchImage = g.isDrawingStyleSet(Graphics.DRAWSTYLE_FOCUS) ? _imageOffFocus : _imageOff;
            }

            g.setFont( _labelFont );

            // Determine Label Colour
            g.setColor( g.isDrawingStyleSet(Graphics.DRAWSTYLE_FOCUS) ? _textColourFocus : _textColour );

            // Label
            g.drawText( _on ? _textOn : _textOff, 0, ( getHeight() - _textHeight ) / 2, DrawStyle.RIGHT, _textWidth ); 

            // Image
            //g.drawBitmap( _textWidth + _horizontalTextImageGap, 0, _switchImage.getWidth(), _switchImage.getHeight(), _switchImage, 0, 0 );
            g.drawBitmap(0, 5, mWidth, mHeight, _switchImage, 0, 0);
        } finally {
            g.setFont( oldFont );
            g.setColor( oldColor );
        }
    }

    public void paintBackground( Graphics g ) {}

    protected void drawFocus( Graphics g, boolean on ){
        // Paint() handles it all
        g.setDrawingStyle( Graphics.DRAWSTYLE_FOCUS, true );
        paint( g );
    }

    protected boolean keyChar( char key, int status, int time ){
        if( key == Characters.SPACE || key == Characters.ENTER ) {
            toggle();            
            return true;
        }
        return false;
    }

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

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

    protected boolean trackwheelClick( int status, int time ){        
        if( isEditable() ) {
            toggle();            
            return true;
        }
        return super.trackwheelClick(status, time);
    }

    /**
     * Toggles the state of the switch
     */
    private void toggle(){
        _on = !_on;
        invalidate();
        fieldChangeNotify( 0 );
    }

    public void setDirty( boolean dirty ){
        // We never want to be dirty or muddy
    }

    public void setMuddy( boolean muddy ){
        // We never want to be dirty or muddy
    }    

    protected void makeContextMenu(ContextMenu contextMenu){
        super.makeContextMenu(contextMenu);
        if((Ui.getMode() < Ui.MODE_ADVANCED) && isEditable()) {
            contextMenu.addItem(_changeOptionsItem);
        }
    }

    /**
     * @category Internal InnerClass
     */
    static class ChangeOptionMenuItem extends MenuItem {
        ChangeOptionMenuItem() {
            super("Toggle", 30270, 10);
        }

        ChangeOptionMenuItem(String text) {
            super(text, 30270, 10);
        }

        public void run() {
            LabeledSwitch theSwitch = (LabeledSwitch)getTarget();
            theSwitch.toggle();
        }

        public int getPriority() {
            return 100 + (getTarget().isMuddy() ? 1000 : 0);
        }
    };
}
4

2 に答える 2

3

したがって、ここには複数の問題がある可能性があると思います。ユーゲンの呼びかけは正しいと思いcommit()ます。

LabeledSwitchしかし、クラスの使い方が間違っていると思います。あなたがそのクラスを書いていないと思いますか?その1つのクラス内に複数のコーディング規則があるため、私はそれを言うだけです.

このようなカスタム BlackBerry UI クラスを複数見てきましたが、これがそのクラスの動作方法であると確信しています。

  1. このクラスは、オン状態とオフ状態を表す PNG 画像をクラスに渡すことで定義できる 2 つの視覚的状態を切り替えることができます。

  2. パラメータにまたは値を指定setOn()してメソッドを呼び出すことにより、クラスをこれらの状態間で切り替えることができます。そのパラメータの値によって、クラスのカスタムメソッドで描画される PNG ファイルが決まります。truefalsepaint()

  3. setOn()クラスの元の作成者は、およびgetOnState()メソッドを使用して、スイッチの現在の状態を変更およびアクセスすることを意図しています。これを変更する必要があります

public boolean _on;

これに:

private boolean _on;

スイッチがオンになっているかどうかを知りたい場合は、 に尋ねてくださいonImg.getOnState()

ちなみに、スイッチには とは別の名前を付けることをお勧めしますonImg。とても紛らわしい名前です。スイッチを使用してfooonOffSwitchと呼ばれるものをオフおよびオンにする場合は、 、またはtoggleSwitch、またはのようなものにする必要があります。スイッチがonのときに表示される画像を常に表すメンバー変数のコンテキストでは、名前を付けると混乱します。fooSwitchonImg_imageOn

したがって、要約すると、これの代わりに:

 if (booleanVal==true)
 {
     onImg=new LabeledSwitch(on,off,on,off,true);
 }
 else
 {
     onImg=new LabeledSwitch(off,on,off,on,false);
 }

次のようなものを使用します。

 onOffSwitch = new LabeledSwitch(on, off, on, off, true);
 onOffSwitch.setOn(booleanValue);        

また

 onOffSwitch = new LabeledSwitch(on, off, on, off, booleanValue);
于 2012-11-28T08:28:31.380 に答える
1

まず、永続オブジェクトを保存するために必要な次のコードが見つかりません。

persistentObject.commit();

これがあなたのコードからのいくつかの余分なものであることを願っています. そうしないと、値を読み取る前に常に値を書き換えます. したがって、「読み取り」コードは意味がないため、「書き込み」の後に留まるべきではありません。

于 2012-11-28T06:58:36.207 に答える