当我按下开关以使其显示在“ on”位置时,我实际上如何使其执行某些操作,例如,将其链接到将播放器的音量设置为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
}

最佳答案

使用布尔值标志判断它是否已经静音,然后使用volume变量的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;
    }
}

关于java - 当我打开和关闭它时,如何使labelSwitch做某事?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/12546397/

10-11 22:30
查看更多