如何在其中一个 viewpager 页面中访问 mybutton?

how to get access to mybutton in one of viewpager pages?

我是这个 android 和 java 的新手。

我的 viewpager 现在可以滑动我所有的 xml 布局,但我不知道如何访问其中的项目。

我试过使用Button mybutton = (Button) findViewById (R.layout.result),然后在mybutton中设置onClickListener,但后来每次运行它都被强制关闭

这是我的代码:

public class 主要延伸 Activity {

private ViewPager myviewpager;
private ViewPagerAdapter myviewpageradapter;
private static int NUM_AWESOME_VIEWS = 12;
Button button_1;



@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    myviewpageradapter = new ViewPagerAdapter();
    myviewpager = (ViewPager) findViewById(R.id.myviewpager);
    myviewpager.setAdapter(myviewpageradapter);

        button_1 = (Button) myviewpager.findViewById(R.id.first_button);
        button_1.setOnClickListener(new OnClickListener() {
            public void onClick(View v) {
                finish();
            }
        });
}

private class ViewPagerAdapter extends PagerAdapter {

    @Override
    public int getCount() {
            return NUM_AWESOME_VIEWS;
    }

    @Override
    public Object instantiateItem(View collection, int position) {
            LayoutInflater layoutinflater = getLayoutInflater();

            View view;
            view = layoutinflater.inflate(R.layout.page1, null);

            switch (position)
            {
                case 0 : view = layoutinflater.inflate(R.layout.page1, null); break;
                case 1 : view = layoutinflater.inflate(R.layout.page2, null); break;
                case 2 : view = layoutinflater.inflate(R.layout.page3, null); break;
                case 3 : view = layoutinflater.inflate(R.layout.page4, null); break;
                case 4 : view = layoutinflater.inflate(R.layout.page5, null); break;
                case 5 : view = layoutinflater.inflate(R.layout.page6, null); break;
                case 6 : view = layoutinflater.inflate(R.layout.page7, null); break;
                case 7 : view = layoutinflater.inflate(R.layout.page8, null); break;
                case 8 : view = layoutinflater.inflate(R.layout.page9, null); break;
                case 9 : view = layoutinflater.inflate(R.layout.page10, null); break;
                case 10 : view = layoutinflater.inflate(R.layout.page11, null); break;
                case 11 : view = layoutinflater.inflate(R.layout.result, null);break;
            }
            ((ViewPager) collection).addView(view,0);
            return view;
    }

    @Override
    public void destroyItem(View collection, int position, Object view) {
            ((ViewPager) collection).removeView((View) view);
    }



    @Override
    public boolean isViewFromObject(View view, Object object) {
            return view==((View)object);
    }

    @Override
    public void finishUpdate(View arg0) {}


    @Override
    public void restoreState(Parcelable arg0, ClassLoader arg1) {}

    @Override
    public Parcelable saveState() {
            return null;
    }

    @Override
    public void startUpdate(View arg0) {}

}

你不能像我朋友那样使用 ViewPager findViewById

由于您在适配器中扩充了不同的视图,android 无法找到您所引用的按钮,因为它不知道哪个 xml 布局包含它。

你可以这样做..

private ViewPager myviewpager;
private ViewPagerAdapter myviewpageradapter;
private static int NUM_AWESOME_VIEWS = 12;
Button button_1;


@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    myviewpageradapter = new ViewPagerAdapter();
    myviewpager = (ViewPager) findViewById(R.id.myviewpager);
    myviewpager.setAdapter(myviewpageradapter);


}

private class ViewPagerAdapter extends PagerAdapter {

    @Override
    public int getCount() {
        return NUM_AWESOME_VIEWS;
    }

    @Override
    public Object instantiateItem(View collection, int position) {
        LayoutInflater layoutinflater = getLayoutInflater();

        View view = null;

        switch (position) {
            case 0:
                view = layoutinflater.inflate(R.layout.page1, null);
                Button bb = (Button) view.findViewById(R.id.first_button);
                bb.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        Toast.makeText(MainActivity.this, "clicked button in first item", Toast.LENGTH_SHORT).show();
                    }
                });
                break;
            case 1:
                view = layoutinflater.inflate(R.layout.page2, null);
                break;
            case 2:
                view = layoutinflater.inflate(R.layout.page3, null);
                break;
            case 3:
                view = layoutinflater.inflate(R.layout.page4, null);
                break;

        }
        ((ViewPager) collection).addView(view);
        return view;
    }

    @Override
    public void destroyItem(View collection, int position, Object view) {
        ((ViewPager) collection).removeView((View) view);
    }


    @Override
    public boolean isViewFromObject(View view, Object object) {
        return view == ((View) object);
    }

就像这样,您可以在那些不同的 xml 布局文件中单独找到项目,当然您可以深入研究并寻找更好的解决方案,但您这样做的方式会导致 ANR。试一试...