0

当我按下我的开关以使其显示“开”位置时,我实际上如何让它做一些事情,例如,将它链接到将播放器音量设置为 0 的方法?我猜它的接口FieldChangeListener

我所有的实现都发生在MainScreen课堂上。

Bitmap switch_left = Bitmap.getBitmapResource("switch_left.png");
Bitmap switch_right = Bitmap.getBitmapResource("switch_right.png");
Bitmap switch_left_focus = Bitmap.getBitmapResource("switch_left_focus.png");
Bitmap switch_right_focus = Bitmap.getBitmapResource("switch_right_focus.png");

LabeledSwitch silentSwitch = new LabeledSwitch(switch_left, switch_right, switch_left_focus, switch_right_focus, "on", "off", true );
JustifiedHorizontalFieldManager silent = new JustifiedHorizontalFieldManager( new LabelField( "Silent Mode" ), silentSwitch, false, USE_ALL_WIDTH );
silent.setPadding(5,5,5,5);
add(silent);

我导入了一个名为 OpenGlSpriteDemo 的游戏演示,并查看了他们如何使用字段更改侦听器实现开始按钮,因此我尝试为 Labeledswitch 执行此操作。我是否朝着正确的方向前进?

LabeledSwitch silentSwitch = new LabeledSwitch(switch_left, switch_right, switch_left_focus, switch_right_focus, "on", "off", false );
silentSwitch.setChangeListener(this);

public void fieldChanged(Field arg0, int arg1) 
{
    //If user sets the switch to on, reduce the volume to 0, 
    // else if user sets the switch to false, change it back 
    // to the default volume    
}

在此处输入图像描述

4

1 回答 1

1

使用布尔标志来判断它是否已经静音,并使用音量变量的 get 和 set 方法并将其传递给:

volume.setLevel(getVolume());       

boolean isSilent = false;

public void fieldChanged(Field field, int context) 
{
    if(!isSilent && field == silentSwitch)
    {
        setVolume(0);
        isSilent = true;
    }
    else if(field == silentSwitch && isSilent)
    {
        setVolume(20);
        isSilent = false;           
    }
}
于 2012-09-22T19:07:49.863 回答