0

このコードから、

public class Custom_TopField extends Manager {
private Bitmap download = Config_GlobalFunction.Bitmap("btn_download.png");
private Bitmap downloadactive = Config_GlobalFunction
        .Bitmap("btn_download_active.png");
private Bitmap refresh = Config_GlobalFunction.Bitmap("icon_refresh.png");
private Bitmap refreshactive = Config_GlobalFunction
        .Bitmap("icon_refresh_active.png");
private Bitmap back = Config_GlobalFunction.Bitmap("btn_back.png");
private Bitmap backctive = Config_GlobalFunction
        .Bitmap("btn_back_active.png");
private Bitmap news = Config_GlobalFunction.Bitmap("icon_news.png");
private Bitmap newsactive = Config_GlobalFunction
        .Bitmap("icon_news_active.png");

private Custom_ButtonField downloadbtn, refreshbtn, backbtn, newsbtn;
private Custom_LabelField title;
private int left, right, fontsize;
private Database_Webservice webservice;

Custom_TopField(final MainScreen mainscreen, final int position,
        final int catsid, final String header, int left, int right) {
    super(Manager.USE_ALL_WIDTH | Manager.NO_VERTICAL_SCROLL
            | Manager.NO_HORIZONTAL_SCROLL);
    this.left = left;
    this.right = right;

    if (Display.getWidth() > 480)
        fontsize = 43;
    else if (Display.getWidth() < 481 && Display.getWidth() > 320)
        fontsize = 33;
    else
        fontsize = 23;

    webservice = new Database_Webservice();
    setBackground(Config_GlobalFunction.loadbackground(Display.getWidth()
            + "_" + "header_bar.png"));

    if (position != 0) {
        title = new Custom_LabelField(Config_GlobalFunction.maintitle,
                DrawStyle.ELLIPSIS | LabelField.USE_ALL_WIDTH
                        | DrawStyle.HCENTER | Field.FOCUSABLE, Color.WHITE) {
            protected boolean navigationClick(int status, int time) {
                Main.getUiApplication().pushScreen(
                        new Custom_LoadingScreen(1));
                Main.getUiApplication().invokeLater(new Runnable() {
                    public void run() {
                        Main.getUiApplication().pushScreen(
                                new Main_AllLatestNews(false));
                    }
                }, 1 * 1000, false);
                return true;
            }

            protected boolean touchEvent(TouchEvent message) {
                int eventCode = message.getEvent();
                if (eventCode == TouchEvent.UNCLICK)
                    Main.getUiApplication().pushScreen(
                            new Menu_PopupMenu(position));

                return true;
            }
        };

    } else {
        title = new Custom_LabelField(Config_GlobalFunction.maintitle,
                DrawStyle.ELLIPSIS | LabelField.USE_ALL_WIDTH
                        | DrawStyle.HCENTER, Color.WHITE);
    }
    title.setFont(Font.getDefault().derive(Font.BOLD, fontsize));
    add(title);

    if (left == 1) {
        newsbtn = new Custom_ButtonField(news, newsactive, newsactive) {
            protected boolean navigationClick(int status, int time) {
                Main.getUiApplication().pushScreen(
                        new Menu_PopupMenu(position));
                return true;
            }

            protected boolean touchEvent(TouchEvent message) {
                int eventCode = message.getEvent();
                if (eventCode == TouchEvent.UP)
                    Main.getUiApplication().pushScreen(
                            new Menu_PopupMenu(position));

                return true;
            }
        };

        add(newsbtn);
    } else if (left == 2) {
        backbtn = new Custom_ButtonField(back, backctive, backctive) {
            protected boolean navigationClick(int status, int time) {
                Main.getUiApplication().popScreen(mainscreen);
                return true;
            }

            protected boolean touchEvent(TouchEvent message) {
                int eventCode = message.getEvent();
                if (eventCode == TouchEvent.UP)
                    Main.getUiApplication().popScreen(mainscreen);
                return true;
            }
        };
        add(backbtn);
    }

    if (right == 1) {
        downloadbtn = new Custom_ButtonField(download, downloadactive,
                downloadactive) {
            protected boolean navigationClick(int status, int time) {
                if (Config_GlobalFunction
                        .Dialog(Config_GlobalFunction.alertdownload)) {
                    if (Config_GlobalFunction.isConnected()) {
                        webservice.UpdateAllCatNews();
                    } else
                        Config_GlobalFunction.Message(
                                Config_GlobalFunction.nowifi, 1);
                } else
                    Config_GlobalFunction.CloseDialog();
                return true;
            }

            protected boolean touchEvent(TouchEvent message) {
                int eventCode = message.getEvent();
                if (eventCode == TouchEvent.UP) {
                    if (Config_GlobalFunction
                            .Dialog(Config_GlobalFunction.alertdownload)) {
                        if (Config_GlobalFunction.isConnected()) {
                            webservice.UpdateAllCatNews();
                        } else
                            Config_GlobalFunction.Message(
                                    Config_GlobalFunction.nowifi, 1);
                    } else
                        Config_GlobalFunction.CloseDialog();
                }
                return true;
            }
        };
        add(downloadbtn);
    } else if (right == 2) {
        refreshbtn = new Custom_ButtonField(refresh, refreshactive,
                refreshactive) {
            protected boolean navigationClick(int status, int time) {
                if (Config_GlobalFunction.isConnected()) {
                    webservice
                            .refreshCatNewsindex(catsid, position, header);
                } else
                    Config_GlobalFunction.Message(
                            Config_GlobalFunction.nowifi, 1);
                return true;
            }

            protected boolean touchEvent(TouchEvent message) {
                int eventCode = message.getEvent();
                if (eventCode == TouchEvent.UP) {
                    if (Config_GlobalFunction.isConnected())
                        webservice.refreshCatNewsindex(catsid, position,
                                header);
                    else
                        Config_GlobalFunction.Message(
                                Config_GlobalFunction.nowifi, 1);
                }
                return true;
            }
        };
        add(refreshbtn);
    }
}
}

newsbtn左右にボタンがありますdownloadbtnnewsbtnフォーカスしているときにクリックするdownloadbtnと、プロンプトは表示されませんDialogが、表示されますpushscreen。右、をクリックすると、の代わりにdownloadbtnプロンプ​​トが表示されます。Dialogpushscreen

両方のボタンの焦点が合っていないときは、ボタンをクリックします。ボタンは焦点が合っており、期待どおりに実行されます。

デフォルトのボタンに焦点を合わせてみましたがButtonField、別のフィールドをクリックすると、正しく機能します。

custom_buttonfieldクラスの問題だと思います。

これが私のCustom_FieldButtonです。

public class Custom_ButtonField extends ButtonField {
Bitmap mNormal;
Bitmap mFocused;
Bitmap mActive;

int mWidth;
int mHeight;

private int color = -1;
String text;

public Custom_ButtonField(Bitmap normal, Bitmap focused, Bitmap active) {
    super(CONSUME_CLICK | Field.FOCUSABLE | Field.FIELD_HCENTER
            | Field.FIELD_VCENTER);
    mNormal = normal;
    mFocused = focused;
    mActive = active;
    mWidth = mNormal.getWidth();
    mHeight = mNormal.getHeight();
    setMargin(0, 0, 0, 0);
    setPadding(0, 0, 0, 0);
    setBorder(BorderFactory.createSimpleBorder(new XYEdges(0, 0, 0, 0)));
    setBorder(VISUAL_STATE_ACTIVE,
            BorderFactory.createSimpleBorder(new XYEdges(0, 0, 0, 0)));
}

public Custom_ButtonField(String text, Bitmap normal, Bitmap focused,
        Bitmap active, int color) {
    super(CONSUME_CLICK | Field.FOCUSABLE | Field.FIELD_HCENTER
            | Field.FIELD_VCENTER);
    this.color = color;
    mNormal = normal;
    mFocused = focused;
    mActive = active;
    mWidth = mNormal.getWidth();
    mHeight = mNormal.getHeight();
    setMargin(0, 0, 0, 0);
    setPadding(0, 0, 0, 0);
    setBorder(BorderFactory.createSimpleBorder(new XYEdges(0, 0, 0, 0)));
    setBorder(VISUAL_STATE_ACTIVE,
            BorderFactory.createSimpleBorder(new XYEdges(0, 0, 0, 0)));
    this.text = text;
}

public Custom_ButtonField(String text, Bitmap normal, Bitmap focused,
        Bitmap active, int color, long style) {
    super(style);
    this.color = color;
    mNormal = normal;
    mFocused = focused;
    mActive = active;
    mWidth = mNormal.getWidth();
    mHeight = mNormal.getHeight();
    setMargin(0, 0, 0, 0);
    setPadding(0, 0, 0, 0);
    setBorder(BorderFactory.createSimpleBorder(new XYEdges(0, 0, 0, 0)));
    setBorder(VISUAL_STATE_ACTIVE,
            BorderFactory.createSimpleBorder(new XYEdges(0, 0, 0, 0)));
    this.text = text;
}

public void setText(String text) {
    this.text = text;
    invalidate();
}

public String getText() {
    return text;
}

public void setColor(int color) {
    this.color = color;
}

protected void onFocus(int direction) {
    super.onFocus(direction);
    color = 0x540604;
    this.invalidate();
}

protected void onUnfocus() {
    super.onUnfocus();
    color = Color.WHITE;
    this.invalidate();
}

protected void paint(Graphics graphics) {
    int fontcontent;
    if (Display.getWidth() > 480)
        fontcontent = 28;
    else if (Display.getWidth() < 481 && Display.getWidth() > 320)
        fontcontent = 23;
    else
        fontcontent = 18;

    Bitmap bitmap = null;
    switch (getVisualState()) {
    case VISUAL_STATE_NORMAL:
        bitmap = mNormal;
        break;
    case VISUAL_STATE_FOCUS:
        bitmap = mFocused;
        break;
    case VISUAL_STATE_ACTIVE:
        bitmap = mActive;
        break;
    default:
        bitmap = mNormal;
    }
    setBackground(BackgroundFactory.createBitmapBackground(bitmap));
    graphics.setFont(Font.getDefault().derive(Font.PLAIN, fontcontent));
    graphics.setColor(color);
    graphics.drawText(text, (mNormal.getWidth() - Font.getDefault()
            .getAdvance(text)) / 2, ((mNormal.getHeight() - Font
            .getDefault().getHeight()) / 2) + 10, DrawStyle.HCENTER
            | DrawStyle.VCENTER);
}

public int getPreferredWidth() {
    return mWidth;
}

public int getPreferredHeight() {
    return mHeight;
}

protected void layout(int width, int height) {
    setExtent(mWidth, mHeight);
}
}

ボタンのフォーカス状態が関数に影響を与えないように、クラスを編集するのを誰が手伝ってくれますか?*

4

2 に答える 2

0

使用setFieldChangeListenerはすべてのアクションに適用されます。

    if (position != 0) {
        title = new Custom_LabelField(Config_GlobalFunction.maintitle,
                DrawStyle.ELLIPSIS | LabelField.USE_ALL_WIDTH
                        | DrawStyle.HCENTER | Field.FOCUSABLE
                        | ButtonField.CONSUME_CLICK, Color.WHITE) {
            protected boolean navigationClick(int status, int time) {
                Main.getUiApplication().pushScreen(
                        new Custom_LoadingScreen(1));
                Main.getUiApplication().invokeLater(new Runnable() {
                    public void run() {
                        Main.getUiApplication().pushScreen(
                                new Main_AllLatestNews());
                    }
                }, 1 * 1000, false);
                return true;
            }
        };
    } else {
        title = new Custom_LabelField(Config_GlobalFunction.maintitle,
                DrawStyle.ELLIPSIS | LabelField.USE_ALL_WIDTH
                        | DrawStyle.HCENTER, Color.WHITE);
    }
    title.setFont(Font.getDefault().derive(Font.BOLD, fontsize));
    add(title);

    if (left == 1) {
        newsbtn = new Custom_ButtonField(news, newsactive, newsactive);
        newsbtn.setChangeListener(new FieldChangeListener() {
            public void fieldChanged(Field field, int context) {
                Main.getUiApplication().pushScreen(
                        new Menu_PopupMenu(position));
            }
        });
        add(newsbtn);
    } else if (left == 2) {
        backbtn = new Custom_ButtonField(back, backctive, backctive);
        backbtn.setChangeListener(new FieldChangeListener() {
            public void fieldChanged(Field field, int context) {
                Main.getUiApplication().popScreen(mainscreen);
            }
        });
        add(backbtn);
    }

    if (right == 1) {
        downloadbtn = new Custom_ButtonField(download, downloadactive,
                downloadactive);
        downloadbtn.setChangeListener(new FieldChangeListener() {
            public void fieldChanged(Field field, int context) {
                if (Config_GlobalFunction
                        .Dialog(Config_GlobalFunction.alertdownload)) {
                    if (Config_GlobalFunction.isConnected()) {
                        webservice.UpdateAllCatNews();
                    } else
                        Config_GlobalFunction.Message(
                                Config_GlobalFunction.nowifi, 1);
                } else
                    Config_GlobalFunction.CloseDialog();
            }
        });
        add(downloadbtn);
    } else if (right == 2) {
        refreshbtn = new Custom_ButtonField(refresh, refreshactive,
                refreshactive);
        refreshbtn.setChangeListener(new FieldChangeListener() {
            public void fieldChanged(Field field, int context) {
                if (Config_GlobalFunction.isConnected()) {
                    Config_GlobalFunction.Message(
                            Config_GlobalFunction.refreshing, 1);
                    webservice.UpdateMoreCatNews(catsid, position, header);
                } else
                    Config_GlobalFunction.Message(
                            Config_GlobalFunction.nowifi, 1);
            }
        });
        add(refreshbtn);
    }
}

ただし、クリックするか、トラックホイールを使用してボタンをクリックすると、青色で表示されるという悪い点があります。

于 2012-07-26T07:47:45.857 に答える
0

あなたがすでに回答を投稿していることは知っていますが、別のオプションを提案したいと思います。BlackBerryAdvancedUIサンプルをダウンロードしてみてください。便利なカスタムクラスがたくさんありField、ほとんどすべてのアプリが必要とすることを実行します。

詳細はこちら

お持ちのCustom_ButtonFieldクラスにはまだ多くのコードが含まれていないため、 Advanced UI Samplesフォルダー(の下)にあるBitmapButtonFieldクラスから始めることをお勧めします。/src/com/samples/toolkit/ui/component

カラーペイントコードを追加する必要がある場合は、すでに問題がないようです。

重要なのは、ほとんどのFieldサブクラスでは、おそらくメソッドをオーバーライドすべきではないというtouchEvent()ことです。できますが、ほとんどの人が考えるよりも注意が必要です。これは主に、タッチの正確な座標が重要な複雑なフィールドや、カスタムのスワイプジェスチャを処理している場合に役立ちます。

メソッドをまったく実装しない場合は、またはtouchEvent()のコードを必要な目的に使用できます。navigationClick()navigationUnClick()

しかし、私はあなたが正しい軌道に乗っていると思います。BitmapButtonField一般に、クリックされたときに何をすべきかについて何も知らない再利用可能なボタンクラス(のような)があります。私は通常、ボタンがクリックされたときに何かをするためにManagerクラスに登録させます。FieldChangeListenerこれは、高度なUIサンプルに表示されるものです。

    BitmapButtonField one = new BitmapButtonField(
          Bitmap.getBitmapResource("button_back_normal.png"), 
          Bitmap.getBitmapResource("button_back_focus.png") );
    add(one);
    one.setChangeListener(new FieldChangeListener() {
        public void fieldChanged(Field f, int context) {
            Dialog.alert("Hello Button!");
        }
    });

BitmapButtonField、フォーカス状態を台無しにすることなく、タッチクリックを処理し、ホイールクリックも追跡するようにすでに適切に設定されています。

それから始めてみて、これがうまくいくかどうかを確認してください。幸運を!

于 2012-07-27T09:43:12.027 に答える