Android 两个Activity之间实现滑动切换

1.第一个Activity:

package com.example.myapi.animation;


import com.example.myapi.R;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;

/**
 *两个Activity之间实现左右滑动
 * @author USER
 *
 */
public class SlideAnimationActivity extends Activity implements OnClickListener{
    private Button btn;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.anim_left_layout);
        btn = (Button)findViewById(R.id.button1);
        btn.setOnClickListener(this);
    }
        
    public void onClick(View v) {
        if(btn.getId() == v.getId()){
            Intent intent = new Intent();
            intent.setClass(SlideAnimationActivity.this, SlideSecondAnimationActivity.class);
            startActivity(intent);
            overridePendingTransition(R.anim.in_from_right, R.anim.out_to_left);
        }
        
    }
}

2.第二个Activity:

package com.example.myapi.animation;

import com.example.myapi.R;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;

/**
 * 左右滑动的第二个Activity
 * @author USER
 *
 */
public class SlideSecondAnimationActivity extends Activity implements OnClickListener{
    private Button btn = null;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.anim_right_layout);
        btn = (Button)findViewById(R.id.button2);
        btn.setOnClickListener(this);
    }

    public void onClick(View v) {
        if(btn.getId() == v.getId()){
            Intent intent = new Intent();
            intent.setClass(SlideSecondAnimationActivity.this, SlideAnimationActivity.class);
            startActivity(intent);
            overridePendingTransition(R.anim.in_from_right, R.anim.out_to_left);
        }
    }

}

3.动画所需要的连个xml文件:

a)left.xlm:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >

    <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true"
        android:layout_centerVertical="true"
        android:text="@string/hello_world"
        tools:context=".MyAPIDemo" />

    <Button
        android:id="@+id/button1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:layout_alignRight="@+id/textView1"
        android:layout_marginRight="36dp"
        android:layout_marginTop="64dp"
        android:text="Button" />

</RelativeLayout>

b)right.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" >

    <Button
        android:id="@+id/button2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="切换" />

</LinearLayout>

好了,复制以上代码,来体验一下吧。

posted on 2012-09-19 16:47  飘杨......  阅读(483)  评论(0)    收藏  举报