当前位置:网站首页>Intent (whether there is return value to jump)
Intent (whether there is return value to jump)
2022-07-27 19:53:00 【Ashurol】
startActivity(),startActivityForResult() The two methods determine whether there is page Jump with data return
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 Is to set the identity of the request
}
});
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
// TODO Auto-generated method stub
//requestCode: Identification of the request
//resultCode: The identity returned
super.onActivityResult(requestCode, resultCode, data);
if(requestCode==1&&resultCode==2)
{
String content=data.getStringExtra("data");// Receive the returned data
tv.setText(content);
}
}
}The corresponding layout file
<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=" First method jump " />
<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=" Jump that can return data " />
<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>
the second Activity
public class second extends Activity{
Button bt;
String content=" Hello , I am the data returned by page 2 ";
@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();
}
});
}
} The corresponding layout file <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=" Return the data " />
<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=" Page 2 " />
</RelativeLayout>


边栏推荐
- JS 事件监听 鼠标 键盘 表单 页面 onclick onkeydown onChange
- [basic knowledge of deep learning - 39] comparison of BN, LN and WN
- Detailed interpretation of IEC104 protocol (II) interaction process and protocol analysis
- HDU1171_ Big event in HDU [01 backpack]
- 文件操作防护
- Object常用方法学习【clone和equals】
- MySQL time zone problem
- Influxdb series (IV) TSM engine (storage principle)
- Ericsson admitted bribery in China and other five countries and paid a fine of $1.06 billion to the United States
- Marqueetextview (running lantern)
猜你喜欢
随机推荐
Count the six weapons of the domestic interface cooperation platform!
【深度学习基础知识 - 45】机器学习中常用的距离计算方法
Matplotlib(基本用法)
Flink introduction and operation architecture
Gesturedetector (gesture recognition)
Use of jvisualvm
OneNote code highlighting
Object常用方法学习【clone和equals】
MySQL time zone problem
dp(动态规划)
focal loss
内置模块10.18
View pagoda PHP extension directory
首发骁龙765G!Redmi K30 5G版发布:支持5G双模120Hz屏,定价1999元起
常见运算符9.21
全局函数
Embedded C language loop deployment
Broadcastreceiver (broadcast)
被动收入:回归原始且安全的两种赚取方法
DatePicker(日期选择器)与TimePicker(时间选择器)









