随笔 - 632  文章 - 17  评论 - 54  阅读 - 93万

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   飘杨......  阅读(475)  评论(0编辑  收藏  举报
编辑推荐:
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 理解Rust引用及其生命周期标识(上)
阅读排行:
· 物流快递公司核心技术能力-地址解析分单基础技术分享
· .NET 10首个预览版发布:重大改进与新特性概览!
· 单线程的Redis速度为什么快?
· 展开说说关于C#中ORM框架的用法!
· Pantheons:用 TypeScript 打造主流大模型对话的一站式集成库
< 2025年3月 >
23 24 25 26 27 28 1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31 1 2 3 4 5

点击右上角即可分享
微信分享提示