当前位置:网站首页>Intent(有无返回值得跳转)
Intent(有无返回值得跳转)
2022-07-27 17:02:00 【Ashurol】
startActivity(),startActivityForResult()两个方法分别决定有无数据返回的页面跳转
public class MainActivity extends ActionBarActivity {
Button bt1;
Button bt2;
TextView tv;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
bt1=(Button) findViewById(R.id.button1);
bt2=(Button) findViewById(R.id.button2);
tv=(TextView) findViewById(R.id.textView1);
bt1.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
Intent intent=new Intent(MainActivity.this,second.class);
startActivity(intent);
}
});
bt2.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
Intent intent=new Intent(MainActivity.this,second.class);
startActivityForResult(intent, 1);//1是设置请求的标识
}
});
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
// TODO Auto-generated method stub
//requestCode:请求的标识
//resultCode:返回的标识
super.onActivityResult(requestCode, resultCode, data);
if(requestCode==1&&resultCode==2)
{
String content=data.getStringExtra("data");//接收返回的数据
tv.setText(content);
}
}
}对应的布局文件
<LinearLayout 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"
android:orientation="vertical"
tools:context="com.example.xin.intent.MainActivity" >
<Button
android:id="@+id/button1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_marginTop="17dp"
android:text="第一种方法跳转" />
<Button
android:id="@+id/button2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_below="@+id/button1"
android:text="可以回传数据的跳转" />
<TextView
android:id="@+id/textView1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignRight="@+id/button2"
android:layout_below="@+id/button2"
android:layout_marginTop="18dp"
android:text="TextView" />
</LinearLayout>
第二个Activity
public class second extends Activity{
Button bt;
String content="你好,我是页面二返回的数据";
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
bt=(Button) findViewById(R.id.button);
bt.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
Intent intent=new Intent();
intent.putExtra("data", content);
setResult(2, intent);
finish();
}
});
}
}对应的布局文件 <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
>
<Button
android:id="@+id/button"
android:layout_width="306dp"
android:layout_height="wrap_content"
android:text="返回数据" />
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/button"
android:layout_centerHorizontal="true"
android:layout_marginTop="135dp"
android:text="页面二" />
</RelativeLayout>


边栏推荐
- Programming jump
- Map and set
- S32k series chips -- Introduction
- Map和Set
- go-zero单体服务使用泛型简化注册Handler路由
- [basic knowledge of deep learning - 50] PCA dimensionality reduction principal component analysis
- TSMC 5nm is about to mass produce: Apple A14 monopolizes 70% of the production capacity, and Huawei Kirin 1020 takes 30%
- Yanghui triangle
- influxDB系列(三)influxDB配置文件详解
- 【深度学习基础知识 - 47】贝叶斯网络与朴素贝叶斯
猜你喜欢

5W奖金池/面向高校,2022法律科技创新大赛报名火热进行中

Under the heat wave of Web3.0, the ecological shock of Mensa struck

下放三星3J1传感器:代码暗示Pixel 7人脸识别安全性将大增

【深度学习基础知识 - 43】优势比的概念

c语言:5、多维数组

C language: 12. GDB tool debugging C program

golang设置国内镜像,vscode配置golang开发环境,vscode调试golang代码

【深度学习基础知识 - 42】逻辑回归详解

来一遍《剑指Offer》03. 数组中重复的数字

Map and set
随机推荐
The first Xiaolong 765G! Redmi K30 5g release: support 5g dual-mode 120Hz screen, priced from 1999 yuan
c语言:clion调试方法
【深度学习基础知识 - 44】逻辑回归实现多分类的方法
英特尔发布Horse Ridge芯片:22nm工艺,能够控制多个量子位
Intel's process roadmap for the next 10 years is exposed: 1.4nm process will be launched in 2029! How?
[basic knowledge of deep learning - 47] Bayesian networks and naive Bayes
全球手机芯片设计领域首家!紫光展锐荣膺TMMi4级国际认证
c语言:6、指针的简单使用与注意事项
[basic knowledge of deep learning - 49] kmeans
坚持软硬一体化,一恒科发力智能化教育机器人市场
Chinese character search Pinyin wechat applet project source code
嵌入式C语言结构体
大佬们,ORACLE CDC,本地运行,老是遇到这个An exception occurred in
Can set be used to define local variables in OPDS SQL
估值超156亿元!华勤通讯完成10亿元B轮融资!高通创投、英特尔资本领投
原厂PW4203降压型1-3节锂电池充电芯片
Responsibility should be assigned to people, and Guangzhou should take multiple measures to build a "safety line" for children in summer
Optimization of embedded C language for indefinite cycles
rxbinding
c语言:11、管道