向上一个活动传输数据需要的内容
1.一个主页文件和一个子文件
MainActivity.java和IntentActivity.Java
2.文件对应的布局文件。
3.intent 的开启和传递数据
activity_main.xml布局文件
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<Button
android:text="button";
android:id="@+id/button";
android:layout_width="match_parent"
android:layout_height="match_parent"/>
</LinearLayout>
activity_intent.xml布局文件
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".IntentActivity">
<button
android:text="button1";
android:id="@+id/buton1"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
</LinearLayout>
MainActivity.java
package com.example.toast;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.content.Intent;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button button=(Button)findViewById(R.id.button);
button.setOnClickListener(new View.setOnClickListener(){
@Override
public void onClick(View v){
String data="hello sy";
Intent intent=new intent(MainActivity.this,IntentActivity.class);
intent.putExtra("name",data);
startActivity(intent);
}
});
}
}
IntentActivity.java
package com.example.toast;
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
public class IntentActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_intent);
Intent intent=getIntent();
String data=intent.getStringExtra("name");
Log.d("IntentActivity",data);
}
}
返回数据给上一个活动需要的内容
1.一个主文件和一个子文件。
2.布局文件不变,不写了
mainActivity.java文件
package com.example.toast;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.content.Intent;
import java.lang.String;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button button=(Button)findViewById(R.id.button);
button.setOnClickListener(new View OnClickListener(){
@Override
public void onClick(View v){
Intent intent=new intent(MainActivity.this,IntentActivity.class);
startActivityForResult(intent,1);
}
});
}
@Override
orotected void onActivityResult(int requestCode,int resultCode,Intent data){
switch(requestCode){
case 1:
if(resultCode ==RESULT_OK){
String returnedData=data.getStringExtra("name");
Log.d("MainActivity",returnedData);
}
break;
default;
}
}
}
IntentActivity.java文件
package com.example.toast;
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
public class IntentActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_intent);
Button button=(Button)findViewById(R.id.button2);
button.setOnColickListener(new View.OnColickListener(){
@Override
public void onClick(View v){
Intent intent=new Intent();
intent.putExtra("name","hello sy");
setResult(RESULT_OK,intent);
finish();
}
});
}
}