Android SeekBar:“setOnSeekBarChangeListener”,=此标记后的预期

时间:2014-01-20 22:20:54

标签: android seekbar

我对Android很新,我试图实现SeekBar,

SeekBar batteryBar = (SeekBar) findViewById(R.id.battery_level);
batteryBar.setOnSeekBarChangeListener(new BatteryBar());

private class BatteryBar implements SeekBar.OnSeekBarChangeListener {
    public void onProgressChanged(SeekBar seekBar, int progress,
            boolean fromUser) {
        // TODO Auto-generated method stub
        // To be implemented....
    }

    public void onStartTrackingTouch(SeekBar seekBar) {
        // TODO Auto-generated method stub
    }

    public void onStopTrackingTouch(SeekBar seekBar) {
        // TODO Auto-generated method stub
    }

}

但我总是得到错误

  

令牌上的语法错误" setOnSeekBarChangeListener",=此令牌后的预期

根据Android SeekBar setOnSeekBarChangeListenerhttp://webtutsdepot.com/2011/12/03/android-sdk-tutorial-seekbar-example/它应该是正确的方法,如何做到这一点..感谢您的帮助

2 个答案:

答案 0 :(得分:0)

尝试这样做而不创建新类

batteryBar.setOnSeekBarChangeListener(new OnSeekBarChangeListener() {       

@Override       
public void onStopTrackingTouch(SeekBar seekBar) {      
    // TODO Auto-generated method stub      
}       

@Override       
public void onStartTrackingTouch(SeekBar seekBar) {     
    // TODO Auto-generated method stub      
}       

@Override       
public void onProgressChanged(SeekBar seekBar, int progress,boolean fromUser) {     
    // TODO Auto-generated method stub      
} 
}

答案 1 :(得分:0)

我注意到您发布的代码有一件事是您正在尝试将Seekbar侦听器实现为内部类。如果您希望您的Activity实现OnSeekBarChangeListener,它应该看起来:

import android.app.Activity;
import android.os.Bundle;
import android.widget.SeekBar;
import android.widget.SeekBar.OnSeekBarChangeListener;

public class example extends Activity implements OnSeekBarChangeListener{

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        SeekBar batteryBar = (SeekBar) findViewById(R.id.battery_level);
        batteryBar.setOnSeekBarChangeListener(this);
        //this references your class that is implementing OnSeekBarChangeListener
    }

    @Override
    public void onProgressChanged(SeekBar seekBar, int progress,
            boolean fromUser) {

    }

    @Override
    public void onStartTrackingTouch(SeekBar seekBar) {

    }

    @Override
    public void onStopTrackingTouch(SeekBar seekBar) {

    }

}

如果您没有使用内部类的任何具体推理,那么我建议使用下面的代码。您将可以访问相同的方法。它还消除了对内部类的需要。这也为您提供了将多个OnSeekBarChangeListener()作为不同搜索栏的不同变量的优势。如果在类上实现OnSeekBarChangeListener(),则必须为不同的Seekbar处理不同的情况。

SeekBar.OnSeekBarChangeListener  BatteryBar = new SeekBar.OnSeekBarChangeListener() {

    @Override
    public void onStopTrackingTouch(SeekBar seekBar) {
        // TODO Auto-generated method stub

    }

    @Override
    public void onStartTrackingTouch(SeekBar seekBar) {
        // TODO Auto-generated method stub

    }

    @Override
    public void onProgressChanged(SeekBar seekBar, int progress,
            boolean fromUser) {
        // TODO Auto-generated method stub

    }
};

我希望这会有所帮助

更新:

public class demo extends Activity  {
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    SeekBar batteryBar = (SeekBar) findViewById(R.id.battery_level);
    batteryBar.setOnSeekBarChangeListener(BatteryBar);
}

SeekBar.OnSeekBarChangeListener  BatteryBar = new SeekBar.OnSeekBarChangeListener() {

    @Override
    public void onStopTrackingTouch(SeekBar seekBar) {
        // TODO Auto-generated method stub

    }

    @Override
    public void onStartTrackingTouch(SeekBar seekBar) {
        // TODO Auto-generated method stub

    }

    @Override
    public void onProgressChanged(SeekBar seekBar, int progress,
            boolean fromUser) {
        // TODO Auto-generated method stub

    }
};
}