多个 SeekBars 监听器

发布于 2024-12-23 18:44:14 字数 760 浏览 0 评论 0原文

我在同一视图上有多个搜索栏:

barheures = (SeekBar)findViewById(R.id.barheures); // make seekbar object
        barheures.setOnSeekBarChangeListener(this); // set seekbar listener.
        // since we are using this class as the listener the class is "this"
        barminutes = (SeekBar)findViewById(R.id.barminutes);
        barminutes.setOnSeekBarChangeListener(this); 

这是侦听器:

@Override
public void onProgressChanged(SeekBar bar, int progress, boolean fromUser) {
    // TODO Auto-generated method stub
    Log.v("", "" + bar);
    textMinutes.setText("" + progress + "Minute(s)" );
    textHours.setText("" + progress + "Heure(s)" );
}

如果第一个或第二个栏已移动到同一个侦听器中,我想做一些不同的事情(这是好的做法吗?),但如何做?我的应用程序无法执行我想要的操作

I have multiple seekbar on the same view :

barheures = (SeekBar)findViewById(R.id.barheures); // make seekbar object
        barheures.setOnSeekBarChangeListener(this); // set seekbar listener.
        // since we are using this class as the listener the class is "this"
        barminutes = (SeekBar)findViewById(R.id.barminutes);
        barminutes.setOnSeekBarChangeListener(this); 

And here is the listener:

@Override
public void onProgressChanged(SeekBar bar, int progress, boolean fromUser) {
    // TODO Auto-generated method stub
    Log.v("", "" + bar);
    textMinutes.setText("" + progress + "Minute(s)" );
    textHours.setText("" + progress + "Heure(s)" );
}

I wanna make something different if the first OR the second bar have moved into the same listener (is it the good practice?), but how to?? Here I have the app that don't do what I want

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

煮茶煮酒煮时光 2024-12-30 18:44:14

现在你有 2 个具有以下 id 的搜索栏:

  1. barheures - 其 id:R.id.barheures
  2. barminutes - 其 id:R.id.barminutes

现在在 onProgressChanged(SeekBar bar, intprogress, boolean fromUser) 方法中,执行以下操作:

@Override
public void onProgressChanged(SeekBar bar, int progress, boolean fromUser) {
    // TODO Auto-generated method stub
    Log.v("", "" + bar);

    switch (bar.getId()) {

    case R.id.barheures:
        textHours.setText("" + progress + "Heure(s)");
        break;

    case R.id.barminutes:
        textMinutes.setText("" + progress + "Minute(s)");
        break;
    }
}


    

Now then you have 2 seekbars with the following ids:

  1. barheures - its id: R.id.barheures
  2. barminutes - its id: R.id.barminutes

Now in the onProgressChanged(SeekBar bar, int progress, boolean fromUser) method, Do the following:

@Override
public void onProgressChanged(SeekBar bar, int progress, boolean fromUser) {
    // TODO Auto-generated method stub
    Log.v("", "" + bar);

    switch (bar.getId()) {

    case R.id.barheures:
        textHours.setText("" + progress + "Heure(s)");
        break;

    case R.id.barminutes:
        textMinutes.setText("" + progress + "Minute(s)");
        break;
    }
}


    
又怨 2024-12-30 18:44:14

您也可以只检查 SeekBar 对象是否相等,而不是比较资源 id。示例:

private SeekBar bar1;
private SeekBar bar2;

@Override
public void onCreate(Bundle savedInstanceState)
{
    super.onCreate(savedInstanceState);

    ...

    bar1 = (SeekBar) findViewById(R.id.bar1);
    bar2 = (SeekBar) findViewById(R.id.bar2);

    ...
}

@Override
public void onProgressChanged(SeekBar bar, int progress, boolean fromUser)
{
    if (bar.equals(bar1))
    {
        // do something
    }
    else if (bar.equals(bar2))
    {
        // do something else
    }
}

我更喜欢这样做,因为这样您只需引用每个 SeekBar 资源的 id 一次(在 onCreate 中)。

Instead of comparing resource ids, you can also just check the SeekBar object for equality. Example:

private SeekBar bar1;
private SeekBar bar2;

@Override
public void onCreate(Bundle savedInstanceState)
{
    super.onCreate(savedInstanceState);

    ...

    bar1 = (SeekBar) findViewById(R.id.bar1);
    bar2 = (SeekBar) findViewById(R.id.bar2);

    ...
}

@Override
public void onProgressChanged(SeekBar bar, int progress, boolean fromUser)
{
    if (bar.equals(bar1))
    {
        // do something
    }
    else if (bar.equals(bar2))
    {
        // do something else
    }
}

I prefer doing it this way because then you only ever reference the id of each SeekBar resource once (in your onCreate).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文