2014-02-21 44 views
0

我想在android中创建一个简单的页面控制....我想从一个页面移动到另一个页面水平滚动,就像在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; 
     } 

    } 

} 

回答

1

View view = inflater.inflate(resId, null);增加以下内容:

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

     } 
    }); 
}