比如说我有一个进度条,在onCreate活动中我正在设置onProgressChanged监听器。
sbt.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
int progressChanged = 0;
@Override
public void onStopTrackingTouch(SeekBar seekBar) {
// Toast.makeText(ProductDetailsInfoActivity.this,"seek bar progress:"+progressChanged,
// Toast.LENGTH_SHORT).show();
// 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) {
//recalcpriceornot=false;
// TODO Auto-generated method stub
//Toast.makeText(ProductDetailsInfoActivity.this, String.valueOf(progress), Toast.LENGTH_SHORT).show();
try {
//if (recalcpriceornot)
{ GetItemPriceCalculation(progress);
recalctotalprice();}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
progressChanged = progress;
if(progress <= 100){
setProgressBarColor(sbt,Color.GREEN);
}else{
setProgressBarColor(sbt,Color.RED);
}
}
});
我的其他控件(价格框)调整进度,进度条调整价格框,因此它将循环进行。
我可以通过
关闭听众sbt.setOnSeekBarChangeListener(null);
但要重新打开它我是否必须为听众重写整个部分。这有更好的方法吗? 非常感谢
答案 0 :(得分:1)
不是内联创建SeekBar.OnSeekBarChangeListener,而是创建SeekBar.OnSeekBarChangeListener类型的成员变量。如果要将其打开,可以将变量传递给它。如果要将其关闭,可以将其传递给null。
private SeekBar sbt; //Not sure where this gets initialized in your code.
private SeekBar.OnSeekBarChangeListener mListener = new SeekBar.OnSeekBarChangeListener() {
int progressChanged = 0;
@Override
public void onStopTrackingTouch(SeekBar seekBar) {
// Toast.makeText(ProductDetailsInfoActivity.this,"seek bar progress:"+progressChanged,
// Toast.LENGTH_SHORT).show();
// 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) {
//recalcpriceornot=false;
// TODO Auto-generated method stub
//Toast.makeText(ProductDetailsInfoActivity.this, String.valueOf(progress), Toast.LENGTH_SHORT).show();
try {
//if (recalcpriceornot)
{ GetItemPriceCalculation(progress);
recalctotalprice();}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
progressChanged = progress;
if(progress <= 100){
setProgressBarColor(sbt,Color.GREEN);
}else{
setProgressBarColor(sbt,Color.RED);
}
}
});
void enableListener()
{
sbt.setOnSeekBarChangeListener(mListener);
}
void disableListener()
{
sbt.setOnSeekBarChangeListener(null);
}
您还可以让此对象实现/扩展OnSeekBarChangeListener,而不是将变量保留给侦听器。在这种情况下,启用将传入“this”。
答案 1 :(得分:0)
是。您可以将侦听器存储在实例变量中,然后在代码中的多个位置重用它。
例如,在您的类中创建一个实例变量,如下所示:
private SeekBar.OnSeekBarChangeListener mSeekBarChangeListener = new SeekBar.OnSeekBarChangeListener() {
int progressChanged = 0;
@Override
public void onStopTrackingTouch(SeekBar seekBar) {
// Toast.makeText(ProductDetailsInfoActivity.this,"seek bar progress:"+progressChanged,
// Toast.LENGTH_SHORT).show();
// TODO Auto-generated method stub
}
// other methods, etc
});
然后你可以随时设置听众:
// remove listener
sbt.setOnSeekBarChangeListener(null);
// reattach it later
sbt.setOnSeekBarChangeListener(mSeekBarChangeListener);
答案 2 :(得分:0)
不是实现内联侦听器,而是可以单独实现它。
private SeekBar.OnSeekBarChangeListener mySeekBarListener = new SeekBar.OnSeekBarChangeListener() {
};
现在每次需要设置一个监听器时,请使用:
sbt.setOnSeekBarChangeListener(mySeekBarListener);
编辑:
您还可以将侦听器实现分离到另一个类中。然后,您可以创建该类的对象并在set方法中使用它。如果你想要
,另一种做同样事情的方法答案 3 :(得分:0)
当您要启用SeekBar.OnSeekBarChangeListener
时,您的活动可以实施this
并使用SeekBar
来引用听众:
import android.app.Activity;
import android.database.SQLException;
import android.graphics.Color;
import android.os.Bundle;
import android.widget.SeekBar;
import android.widget.SeekBar.OnSeekBarChangeListener;
public class MainActivity extends Activity implements OnSeekBarChangeListener {
private int progressChanged = 0;
private SeekBar sbt;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
sbt = (SeekBar) findViewById(R.id.seekBar1);
// Turns OFF
sbt.setOnSeekBarChangeListener(null);
// Turns ON
sbt.setOnSeekBarChangeListener(this);
}
@Override
public void onStopTrackingTouch(SeekBar seekBar) {
// Toast.makeText(ProductDetailsInfoActivity.this,"seek bar progress:"+progressChanged,
// Toast.LENGTH_SHORT).show();
// 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) {
// recalcpriceornot=false;
// TODO Auto-generated method stub
// Toast.makeText(ProductDetailsInfoActivity.this,
// String.valueOf(progress), Toast.LENGTH_SHORT).show();
try {
// if (recalcpriceornot)
{
GetItemPriceCalculation(progress);
recalctotalprice();
}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
progressChanged = progress;
if (progress <= 100) {
setProgressBarColor(sbt, Color.GREEN);
} else {
setProgressBarColor(sbt, Color.RED);
}
}
}