简体   繁体   English

如何在Android Page Viewer中实现按钮的点击侦听器?

[英]How to implement click listener for a button in android Page Viewer?

I want to create a simple Page Control in android.... I want to move from one page to another page scrolling horizontally, like the home screen in android device. 我想在android中创建一个简单的页面控件。...我想从一个页面移动到另一个页面,水平滚动,就像android设备中的主屏幕一样。

I have multiple layout in xml like main.xml , layout_first.xml , layout_second.xml and layout_third.xml 我在xml中有多个布局,例如main.xmllayout_first.xmllayout_second.xmllayout_third.xml

Now I have a simple button in my layout_first.xml , I want to implement a click listener for the button like 现在,我的layout_first.xml有一个简单的按钮,我想为该按钮实现点击监听器

button.setOnClickListener(new OnClickListener() 
        {
            public void onClick(View v) 
            {

            }
        });

No I don't know where to put the above code 不,我不知道将上面的代码放在哪里

Here is my main.xml 这是我的main.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent">

    <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Main Layout"
        android:textAppearance="?android:attr/textAppearanceLarge" />

<android.support.v4.view.ViewPager
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:id="@+id/myfivepanelpager"/>
</LinearLayout>

Here is my layout_first.xml 这是我的layout_first.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >

    <TextView
        android:id="@+id/myTextView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="First Layout"
        android:textAppearance="?android:attr/textAppearanceLarge" />

    <Button
        android:id="@+id/button"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Button" />

</LinearLayout>

Here is my layout_second.xml 这是我的layout_second.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >

    <TextView
        android:id="@+id/myTextView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Second Layout"
        android:textAppearance="?android:attr/textAppearanceLarge" />



</LinearLayout>

Here is my layout_third.xml 这是我的layout_third.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >

    <TextView
        android:id="@+id/myTextView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Third Layout"
        android:textAppearance="?android:attr/textAppearanceLarge" />



</LinearLayout>

Here is my java code 这是我的java代码

public class MainActivity extends Activity 
{

    @Override
    protected void onCreate(Bundle savedInstanceState) 
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        MyPagerAdapter adapter = new MyPagerAdapter();

        ViewPager myPager = (ViewPager) findViewById(R.id.myfivepanelpager);
        myPager.setAdapter(adapter);
        myPager.setCurrentItem(0);



    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) 
    {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }



    private class MyPagerAdapter extends PagerAdapter 
    {
        @Override
        public int getCount() 
        {
            // TODO Auto-generated method stub
             return 3;
        }

        public Object instantiateItem(View collection, int position) 
        {
            LayoutInflater inflater = (LayoutInflater) collection.getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);

            int resId = 0;

            switch (position) 
            {
            case 0:
                resId = R.layout.layout_first;
                break;
            case 1:
                resId = R.layout.layout_second;
                break;
            case 2:
                resId = R.layout.layout_third;
                break;

            }

            View view = inflater.inflate(resId, null);

            ((ViewPager) collection).addView(view, 0);

            return view;
        }

         @Override
         public void destroyItem(View arg0, int arg1, Object arg2) {
             ((ViewPager) arg0).removeView((View) arg2);

         }


         @Override
         public boolean isViewFromObject(View arg0, Object arg1) {
             return arg0 == ((View) arg1);

         }

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

    }

}

After View view = inflater.inflate(resId, null); View view = inflater.inflate(resId, null);之后View view = inflater.inflate(resId, null); add the following: 添加以下内容:

if(position == 0){
    view.findViewById(R.id.button).setOnClickListener(new OnClickListener() {
        public void onClick(View v){

        }
    });
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM