安卓的Activity
之间的传值
activivity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/activity_main"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="hello.unc.com.government.MainActivity">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Hello World!"
android:id="@+id/tvOut" />
<Button
android:text="Start Aty1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/textView2"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_marginLeft="41dp"
android:layout_marginStart="41dp"
android:layout_marginTop="54dp"
android:id="@+id/btnStartAty1" />
</RelativeLayout>
aty1.xm
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:text="Aty1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/tvOut"
android:layout_weight="1" />
<Button
android:text="Stop"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/btnStopAty1"
android:layout_weight="1" />
</LinearLayout>
MianActivity.java
package hello.unc.com.government;
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
public class MainActivity extends AppCompatActivity {
private TextView tvOut;
private Button btnStart;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
btnStart =(Button) findViewById(R.id.btnStartAty1);
tvOut = (TextView) findViewById(R.id.tvOut);
btnStart.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent i = new Intent(MainActivity.this,Aty1.class);
Bundle data = new Bundle();
data.putString("txt","Hello Aty1");
i.putExtras(data);
startActivityForResult(i,0);
}
});
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
String result = data.getStringExtra("result");
tvOut.setText(result);
super.onActivityResult(requestCode, resultCode, data);
}
}
Aty1.java
package hello.unc.com.government;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
/**
* Created by 星灿 on 2016/9/29.
*/
public class Aty1 extends Activity {
private TextView tvOut;
private Button btnStop;
@Override
protected void onCreate(Bundle savedInstanceState) {
setContentView(R.layout.aty1);
btnStop = (Button)findViewById(R.id.btnStopAty1);
btnStop.setOnClickListener(new View.OnClickListener(){
public void onClick(View v){
Intent i = new Intent();
i.putExtra("result","Hello MainActivity");
setResult(0,i);
finish();
}
});
tvOut = (TextView) findViewById(R.id.tvOut);
Bundle data = getIntent().getExtras();
String txt = data.getString("txt");
tvOut.setText(txt);
super.onCreate(savedInstanceState);
}
}
AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="hello.unc.com.government">
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name=".Aty1"></activity>
</application>
</manifest>