簡體   English   中英

如何在Android Page Viewer中實現按鈕的點擊偵聽器?

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

我想在android中創建一個簡單的頁面控件。...我想從一個頁面移動到另一個頁面,水平滾動,就像android設備中的主屏幕一樣。

我在xml中有多個布局,例如main.xmllayout_first.xmllayout_second.xmllayout_third.xml

現在,我的layout_first.xml有一個簡單的按鈕,我想為該按鈕實現點擊監聽器

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

            }
        });

不,我不知道將上面的代碼放在哪里

這是我的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>

這是我的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>

這是我的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>

這是我的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>

這是我的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;
         }

    }

}

View view = inflater.inflate(resId, null);之后View view = inflater.inflate(resId, null); 添加以下內容:

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