当前位置:网站首页>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>


边栏推荐
- 嵌入式C语言循环展开
- 记一次无准备的实习面试
- 成本高、落地难、见效慢,开源安全怎么办?
- c语言:11、管道
- HDU1171_Big Event in HDU【01背包】
- [Huawei cloud stack] [shelf presence] issue 13: have you seen the decoupling architecture of the management area? Help government and enterprise customers solve big problems
- [daily accumulation - 07] CUDA multi version switching
- C language: 8. Makefile preparation
- HDU1171_ Big event in HDU [01 backpack]
- C language: 5. Multidimensional array
猜你喜欢

Basic network faults and troubleshooting

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

一种比读写锁更快的锁,还不赶紧认识一下

让你的聊天气泡丰富多彩

Come to sword finger offer 03. Repeated numbers in the array

C language: 12. GDB tool debugging C program

c语言:14、预处理

A lock faster than read-write lock. Don't get to know it quickly

Tab control of MFC advanced control (CTabCtrl)

二叉搜索树
随机推荐
S32K系列芯片--简介
5W bonus pool / for colleges and universities, 2022 legal science and technology innovation competition is in progress
Low code implementation exploration (45) business parameters
OPPO发布首款AR眼镜,宣布未来3年投入500亿进行研发
【深度学习基础知识 - 37】解决正负样本不均衡 Focal Loss
应用程序池已被禁用
三星将推多款RISC-V架构芯片,5G毫米波射频芯片会率先采用
爱立信承认在中国等五国行贿,向美支付10.6亿美元罚款
Basic network faults and troubleshooting
Release Samsung 3J1 sensor: the code implies that the safety of pixel 7 face recognition will be greatly increased
记一次无准备的实习面试
[basic knowledge of deep learning - 43] concept of odds ratio
【日常积累 - 06】查看cuda和cudnn版本
时间复杂度和空间复杂度
c语言:6、指针的简单使用与注意事项
Summary of APP launch in vivo application market
嵌入式C语言指针别名
C language: 5. Multidimensional array
C language: C language code style
Hdu1573 x problem [univariate linear congruence equations]