运行效果图
修改布局文件activity_main.xml:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/LinearLayout1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal" >
<!-- 定义ViewFlipper视图切换器 -->
<ViewFlipper
android:id="@+id/ViewFlipper"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<!-- 定义第一个屏幕 -->
<LinearLayout
android:id="@+id/LinearLayout01"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:text="登录页面"/>
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="请输入用户名"/>
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:password="true"
android:maxLength="12"
android:hint="请输入密码"/>
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="登录"/>
</LinearLayout>
<!-- 定义第二个屏幕 -->
<LinearLayout
android:id="@+id/LinearLayout02"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:text="注册页面"/>
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="请输入用户名"/>
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:password="true"
android:maxLength="12"
android:hint="请输入密码"/>
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:password="true"
android:maxLength="12"
android:hint="再次输入密码"/>
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="登录"/>
</LinearLayout>
<!-- 定义第二个屏幕 -->
<LinearLayout
android:id="@+id/LinearLayout03"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:text="登录成功"/>
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="进入商城"/>
</LinearLayout>
</ViewFlipper>
</LinearLayout>
修改MainActivity.java文件:
import android.os.Bundle;
import android.app.Activity;
import android.view.GestureDetector;
import android.view.MotionEvent;
import android.widget.ViewFlipper;
public class Show_2Activity extends Activity implements android.view.GestureDetector.OnGestureListener{
private ViewFlipper flipper;//定义页面中的ViewFlipper对象
private GestureDetector detector;//定义手势识别监听器对象
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_show_2);
setTitle("滑动切换屏幕");
//初始化手势
detector=new GestureDetector((android.view.GestureDetector.OnGestureListener) this);
findById();//获取对象
}
private void findById() {
flipper=(ViewFlipper) this.findViewById(R.id.ViewFlipper);//得到对象id
}
//当页面被触摸时自动回调,把拿到的时间给手势识别器进行处理
public boolean onTouchEvent(MotionEvent event){
return this.detector.onTouchEvent(event);
}
//当页面按下时自动回调哦
public boolean onDown(MotionEvent e){
return false;
}
//当页面滑动时自动回调
public boolean onFling(MotionEvent e1,MotionEvent e2,float velocityX,float velocityY){
//得到滑动过程中的两点的X和Y坐标,进行判断
if(e1.getX()>e2.getX()){
//当向左滑动时显示上一张
this.flipper.showNext();
}else if(e1.getX()<e2.getX()){
//党项右滑动时显示下一张
this.flipper.showPrevious();
}else{
return false;
}
return true;
}
//当在界面长按是自动调用
public void onLongPress(MotionEvent e){
}
//界面滚动的时候调用
public boolean onScroll(MotionEvent e1,MotionEvent e2,float distanceX,float distanceY){
return false;
}
//当轻击界面的时候自动调用
public boolean onSingleTapUp(MotionEvent e){
return false;
}
//当界面被按压的时候自动调用
public void onShowPress(MotionEvent e){
}
}
可以添加动画
在res文件下新建anim文件,然后在新建布局文件in_leftright.xml
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
<translate
android:duration="3000"
android:fromXDelta="-100%p"
android:toXDelta="0" />
</set>
依照上述再新建out_leftright.xml,in_rightleft.xml,out_rightleft.xml文件
调用
//得到滑动过程中的两点的X和Y坐标,进行判断
if(e1.getX()>e2.getX()){
flipper.setInAnimation(this, R.anim.in_rightleft);
flipper.setOutAnimation(this, R.anim.out_rightleft);</strong>
//当向左滑动时显示上一张
this.flipper.showNext();//调用该函数来显示FrameLayout里面的下一个View。
}else if(e1.getX()<e2.getX()){
<strong>flipper.setInAnimation(this, R.anim.in_leftright);
flipper.setOutAnimation(this, R.anim.out_leftright);
//党项右滑动时显示下一张
this.flipper.showPrevious();//调用该函数来显示FrameLayout里面的上一个View
}