如何通过我的 ActionBar Tab 设置来实现此代码?

发布于 2024-12-23 17:09:18 字数 2526 浏览 1 评论 0原文

我最近更改了我的应用程序,以使用操作栏选项卡在片段之间进行切换。我为主要活动上的第一个片段所做的编码工作正常,但在第二个选项卡上,它显示了相应 XML 文件中的项目,但是我的 Java 代码似乎都不起作用。我想要 3 个搜索栏来更改文本视图的数量,当我不使用片段时它可以工作,但就是无法让它工作。

我必须将代码放在该文件的某个位置吗?

public class CountdownFragmentTab extends Fragment {

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {

    return inflater.inflate(R.layout.countdown, container, false);

}   

就是我想添加到该文件中的内容:

SeekBar seekBar1 = (SeekBar)findViewById(R.id.seekBar1);
    SeekBar seekBar2 = (SeekBar)findViewById(R.id.seekBar2);
    SeekBar seekBar3 = (SeekBar)findViewById(R.id.seekBar3);
    final TextView seekBar1Value = (TextView)findViewById(R.id.seekbar1value);
    final TextView seekBar2Value = (TextView)findViewById(R.id.seekbar2value);
    final TextView seekBar3Value = (TextView)findViewById(R.id.seekbar3value);

    seekBar1.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {

        public void onProgressChanged(SeekBar seekBar1, int progress, boolean fromUser) {
            // TODO Auto-generated method stub
            seekBar1Value.setText(String.valueOf(progress));
        }

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

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

    seekBar2.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {

        public void onProgressChanged(SeekBar seekBar2, int progress, boolean fromUser) {
            // TODO Auto-generated method stub
            seekBar2Value.setText(String.valueOf(progress));
        }

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

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

    seekBar3.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {

        public void onProgressChanged(SeekBar seekBar3, int progress, boolean fromUser) {
            // TODO Auto-generated method stub
            seekBar3Value.setText(String.valueOf(progress));
        }

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

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

I recently changed my application to have the action bar tabs to switch between Fragments. I coding I have done for the first fragment which is on the main activity is working fine, but on the second Tab it displays the items in its corresponding XML file but, none of my Java code seems to be working. I want 3 seek bars to change the number of a Text View, I had it working when I wasn't using Fragments, but just cant get it to work.

Do i have to put the code in a certain place of this file?

public class CountdownFragmentTab extends Fragment {

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {

    return inflater.inflate(R.layout.countdown, container, false);

}   

}

And this is what i want to add to this file:

SeekBar seekBar1 = (SeekBar)findViewById(R.id.seekBar1);
    SeekBar seekBar2 = (SeekBar)findViewById(R.id.seekBar2);
    SeekBar seekBar3 = (SeekBar)findViewById(R.id.seekBar3);
    final TextView seekBar1Value = (TextView)findViewById(R.id.seekbar1value);
    final TextView seekBar2Value = (TextView)findViewById(R.id.seekbar2value);
    final TextView seekBar3Value = (TextView)findViewById(R.id.seekbar3value);

    seekBar1.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {

        public void onProgressChanged(SeekBar seekBar1, int progress, boolean fromUser) {
            // TODO Auto-generated method stub
            seekBar1Value.setText(String.valueOf(progress));
        }

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

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

    seekBar2.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {

        public void onProgressChanged(SeekBar seekBar2, int progress, boolean fromUser) {
            // TODO Auto-generated method stub
            seekBar2Value.setText(String.valueOf(progress));
        }

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

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

    seekBar3.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {

        public void onProgressChanged(SeekBar seekBar3, int progress, boolean fromUser) {
            // TODO Auto-generated method stub
            seekBar3Value.setText(String.valueOf(progress));
        }

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

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

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

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

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文