2012-07-15 74 views
3

我是Android新手,我正在學習編碼&設計一個Android項目。我與ViewPager有問題,無法在此網站上或通過Google搜索找到答案。如何在ViewPager中設置OnClickListener

問題: 我不能讓一個Button執行它的作用,而它在ViewPager

你可以看到我的項目&的apk文件在這裏:https://dl.dropbox.com/u/9820517/ForOneTimeDownload/TestPagerView.rar

這裏是我的PagerDemo.java

public class PagerDemo extends Activity { 

    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     this.requestWindowFeature(Window.FEATURE_NO_TITLE); 
     setContentView(R.layout.activity_pager_demo); 

     myPagerAdapter adapter = new myPagerAdapter(); 
     ViewPager myPager = (ViewPager) findViewById(R.id.myfivepanelpager); 
     myPager.setAdapter(adapter); 
     myPager.setCurrentItem(2); 

     SetAllBtnFunc(); 
    } 

    public void SetAllBtnFunc() { 
     //all func 
     View.OnTouchListener touch = new View.OnTouchListener() { 

       public boolean onTouch(View v, MotionEvent event) { 
        // TODO Auto-generated method stub 
        Log.d("hlv_trinh", "Button be Touched!"); 
        return false; 
       } 
      }; 

      View.OnClickListener click = new View.OnClickListener() { 

       public void onClick(View v) { 
        // TODO Auto-generated method stub 
        Log.d("hlv_trinh", "Button be Clicked!"); 
       } 
      }; 

     //Get view 
     View v = getLayoutInflater().inflate(R.layout.middle, null); 

     Button b = (Button) v.findViewById(R.id.myBtn); 
     b.setOnClickListener(click); 
     b.setOnTouchListener(touch); 
     } 

    @Override 
    public boolean onCreateOptionsMenu(Menu menu) { 
     getMenuInflater().inflate(R.menu.activity_pager_demo, menu); 
     return true; 
    } 
    private class myPagerAdapter extends PagerAdapter { 

     public int getCount() { 
      return 5; 
     } 

     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.farleft; 
       break; 
      case 1: 
       resId = R.layout.left; 
       break; 
      case 2: 
       resId = R.layout.middle; 
       break; 
      case 3: 
       resId = R.layout.right; 
       break; 
      case 4: 
       resId = R.layout.farright; 
       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; 
     } 
    } 
    } 

我的版面內容,在這裏:

<?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:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_gravity="center" 
     android:text="Middle" 
     android:textSize="30dp" > 
    </TextView> 

    <Button 
     android:id="@+id/myBtn" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:text="Test Click" /> 
</LinearLayout> 
+0

非常感謝您的編輯^^ – 2012-07-17 05:32:13

回答

5

這是一個簡單的解決方案。
在佈局XML,在按鈕標籤,設置android:onClick="AnyName",然後在你的PagerDemo.java,地點:

public void AnyName(View v) { 
    // Do your stuff 
} 

,在一個ViewPager設置onClickListener

+1

** NOTE **對於像我這樣的人來說:這意味着真的做你需要的東西,沒有那些_findViewById_「s和_setOnClickListener_」的瘋狂 – Inoy 2014-11-10 22:53:14

0

我爲視圖尋呼機發現了一些棘手的解決方案瀏覽器沒有任何點擊事件,你可以嘗試點擊視圖頁面的子頁面上的事件。我的意思是設置點擊每個頁面的根元素上的監聽器,這將工作肯定。

相關問題