button = (Button) views.get(2).findViewById(R.id.start_btn);

主布局


    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >
            android:id="@+id/viewpager"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:background="#00000000"/>
               android:layout_width="fill_parent"
         android:layout_height="wrap_content"
         android:id="@+id/ll"
         android:orientation="horizontal"
         android:gravity="center_horizontal"
         android:layout_alignParentBottom="true">
                       android:layout_width="wrap_content"
             android:layout_height="wrap_content"
             android:id="@+id/iv1"
             android:src="@drawable/login_point_selected"/>
                         android:layout_width="wrap_content"
             android:layout_height="wrap_content"
             android:id="@+id/iv2"
             android:src="@drawable/login_point"/>
                           android:layout_width="wrap_content"
             android:layout_height="wrap_content"
             android:id="@+id/iv3"
             android:src="@drawable/login_point"/>
     



代码

public class Guide extends Activity implements OnPageChangeListener,OnClickListener{
private ViewPager vp;
private Button button;
private ViewPagerAdapter vpAdapter;
private List views;
private ImageView [] dots;
private int[] ids={R.id.iv1,R.id.iv2,R.id.iv3};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.guide);
inintView();
initDots();
}
private void inintView(){
LayoutInflater inflater = LayoutInflater.from(this);
views = new ArrayList();
views.add(inflater.inflate(R.layout.one, null));
views.add(inflater.inflate(R.layout.two, null));
views.add(inflater.inflate(R.layout.three, null));
vpAdapter = new ViewPagerAdapter(views, this);
vp = (ViewPager) findViewById(R.id.viewpager);
vp.setAdapter(vpAdapter);
vp.setOnPageChangeListener(this);
button = (Button) views.get(2).findViewById(R.id.start_btn);
button.setOnClickListener(this);
 
}
private void initDots(){
dots = new ImageView[views.size()];
for (int i = 0; i < views.size(); i++) {
dots[i]=(ImageView) findViewById(ids[i]);

}
}
@Override
public void onPageScrollStateChanged(int arg0) {

}
@Override
public void onPageScrolled(int arg0, float arg1, int arg2) {

}
@Override
public void onPageSelected(int arg0) {
for (int i = 0; i < views.size(); i++) {
if (arg0 == i) {
dots[i].setImageResource(R.drawable.login_point_selected);
}else{
dots[i].setImageResource(R.drawable.login_point);

}
}
}
@Override
public void onClick(View v) {
startActivity(new Intent(Guide.this,MainActivity.class));

}
 


}

其中view中加载了三个布局

three.xml布局


    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >
    
            android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:background="@drawable/bg3"
        />
            android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
            android:gravity="center_horizontal"
        android:orientation="horizontal">
                    android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:id="@+id/start_btn"
            android:text="JINRU"
            />
   


获取three布局中的button实例 

button = (Button) views.get(2).findViewById(R.id.start_btn);

直接写findViewById(R.id.start_btn)出错

RelativeLayout布局可以覆盖控件!!!

你可能感兴趣的:(android)