关于在Android中Activity页面跳转的方法
2023-09-11 14:21:21 时间
一.无返回结果的页面跳转
1.创建两个类FActivity.java和SActivity.java
2.创建两个layout目录下的factivity.xml和sactivity.xml
3.在AndroidManifest配置文件里注册FActivity.java和SActivity.java,并把原来MainActivity注册信息中的
<intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter>
删去,这几句是设置初始启动的Activity页面
4.在FActivity的注册信息中添加
<intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter>
5.实现从FActivity页面中的button1点击后跳转到SActivity页面
FActivity.java
public class FActivity extends Activity{ private Button btn1; @Override protected void onCreate(Bundle savedInstanceState) { // TODO Auto-generated method stub super.onCreate(savedInstanceState); setContentView(R.layout.factivity); btn1 = (Button) findViewById(R.id.button1); btn1.setOnClickListener(new OnClickListener(){ @Override public void onClick(View v) { // TODO Auto-generated method stub Intent intent = new Intent(FActivity.this,SActivity.class); startActivity(intent); } }); } }
SActivity.java
public class SActivity extends Activity{ @Override protected void onCreate(Bundle savedInstanceState) { // TODO Auto-generated method stub super.onCreate(savedInstanceState); setContentView(R.layout.sactivity); } }
factivity.xml
<TextView android:id="@+id/textView1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="第一个页面" /> <Button android:id="@+id/button1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="跳转第二个页面" />
sactivity.xml
<TextView android:id="@+id/textView2" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="这是第二个页面" />
二.有返回结果的页面跳转
1.创建两个类FActivity.java和SActivity.java
2.创建两个layout目录下的factivity.xml和sactivity.xml
3.在AndroidManifest配置文件里注册FActivity.java和SActivity.java,并把原来MainActivity注册信息中的
<intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter>
删去,这几句是设置初始启动的Activity页面
4.在FActivity的注册信息中添加
<intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter>
5.实现从FActivity页面中的button点击后跳转到SActivity页面,再点击SActivity的button从SActivity传回一个值到FActivity
FActivity.java
public class FActivity extends Activity{ private Button btn2; private TextView tv2; @Override protected void onCreate(Bundle savedInstanceState) { // TODO Auto-generated method stub super.onCreate(savedInstanceState); setContentView(R.layout.factivity); btn2 = (Button) findViewById(R.id.button2); tv2 = (TextView) findViewById(R.id.textView2); btn2.setOnClickListener(new OnClickListener(){ @Override public void onClick(View v) { // TODO Auto-generated method stub Intent intent = new Intent(FActivity.this,SActivity.class); startActivityForResult(intent, 1); } }); } @Override protected void onActivityResult(int requestCode, int resultCode, Intent data) { // TODO Auto-generated method stub super.onActivityResult(requestCode, resultCode, data); if(requestCode==1&&resultCode==2) { String content=data.getStringExtra("data"); tv2.setText(content); } } }
SActivity
public class SActivity extends Activity{ private Button btn1; private String content="我是传回来的值"; @Override protected void onCreate(Bundle savedInstanceState) { // TODO Auto-generated method stub super.onCreate(savedInstanceState); setContentView(R.layout.sactivity); btn1 = (Button) findViewById(R.id.button1); btn1.setOnClickListener(new OnClickListener(){ @Override public void onClick(View v) { // TODO Auto-generated method stub Intent data = new Intent(); data.putExtra("data", content); setResult(2,data); finish(); } }); } }
factivity.xml
<Button android:id="@+id/button2" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="传值的的跳转第二个界面" /> <TextView android:id="@+id/textView2" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="结果" />
sactivity.xml
<Button android:id="@+id/button1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="带值返回" />
相关文章
- [android]MonkeyRunner-andrec实现思路
- [Android Pro] 关于BitmapFactory.decodeStream(is)方法无法正常解码为Bitmap对象的解决方法
- Android中View绘制流程以及invalidate()等相关方法分析
- Android平台Airplay的实现方法
- Android开发中,那些让你相见恨晚的方法、类或接口
- Android开机启动Activity或者Service方法(转载)
- 转__Android开源项目(三 完结篇)
- EasyPlayer RTSP 安卓Android播放器显示模式设置方法
- 56、LeakCanary——直白的展现Android中的内存泄露
- android中实现无标题、全屏的几种方法
- Android Framework层——App启动过程浅析
- 理解Android系统中强指针的实现
- 性能测试 查看Android APP 帧数FPS的方法
- Android getstring方法 BaseApplication.getContext().getString
- Android 11.0 SystemUI导航栏固定在底部显示的修改
- Android 清理app缓存数据的方法
- Android 多组RadioGroup的选项互斥
- Android studio 项目修改包名的方法
- Android 桌面Widget (小组件)开发详解
- android中单元測试中的断言assert的使用与扩展
- Android 滑动冲突问题的简单解决思路
- 【错误记录】Android Studio 集成 ARoute 编译报错 ( 兼容 support 库和 androidx 库 | add ‘tools:replace=“android:appCo )
- 【Android 进程保活】应用进程拉活 ( 系统 Service 机制拉活 | Service 组件 onStartCommand 方法分析 | 源码资源 )
- Android开发之去掉标题栏的三种方法,推荐第三种
- Android在初始化时弹出popwindow的方法
- Android自己定义效果——随机抽奖
- Android (AMS)ActivityManagerService源码分析
- Android中View绘制流程以及invalidate()等相关方法分析
- Android中View绘制流程以及invalidate()等相关方法分析
- Android Studio下“Error:Could not find com.android.tools.build:gradle:2.2.1”的解决方法
- Android对H265和VP9的支持(十四)
- Android Studio的gradle-3.3-all.zip下载和使用方法