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>
好了,复制以上代码,来体验一下吧。
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 理解Rust引用及其生命周期标识(上)
· 物流快递公司核心技术能力-地址解析分单基础技术分享
· 单线程的Redis速度为什么快?
· 展开说说关于C#中ORM框架的用法!
· Pantheons:用 TypeScript 打造主流大模型对话的一站式集成库
· SQL Server 2025 AI相关能力初探