两个 activity 中相互切换,使用 Intent 对像,使用很简单
Intent in = new Intent();
in.setClass(this,active2.class);
// 向 active2 传值
in.putExtra("tt", et.getText().toString());
this.startActivity(in);
this.finish();
in.setClass(this,active2.class);
// 向 active2 传值
in.putExtra("tt", et.getText().toString());
this.startActivity(in);
this.finish();
详细代码
main.xml 第一个 activity用的 xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="这是 active 1111 " />
<EditText
android:id="@+id/et1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
/>
<Button
android:id="@+id/btn1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="go to active2"
/>
</LinearLayout>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="这是 active 1111 " />
<EditText
android:id="@+id/et1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
/>
<Button
android:id="@+id/btn1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="go to active2"
/>
</LinearLayout>
main2.xml 第二个 activity用的 xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="这是 active2 " />
<EditText
android:id="@+id/et1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
/>
<Button
android:id="@+id/btn1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="go to active1"
/>
</LinearLayout>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="这是 active2 " />
<EditText
android:id="@+id/et1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
/>
<Button
android:id="@+id/btn1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="go to active1"
/>
</LinearLayout>
activetest java代码
package zziss.android.activetest;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.widget.Button;
import android.widget.EditText;
import android.view.View;
public class ActiveTestActivity extends Activity implements View.OnClickListener {
/** Called when the activity is first created. */
private Button btn1;
private EditText et;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
btn1 = (Button)this.findViewById(R.id.btn1);
et = (EditText)this.findViewById(R.id.et1);
btn1.setOnClickListener(this);
}
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
Intent in = new Intent();
in.setClass(this,active2.class);
// 向 active2 传值
in.putExtra("tt", et.getText().toString());
this.startActivity(in);
this.finish();
}
}
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.widget.Button;
import android.widget.EditText;
import android.view.View;
public class ActiveTestActivity extends Activity implements View.OnClickListener {
/** Called when the activity is first created. */
private Button btn1;
private EditText et;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
btn1 = (Button)this.findViewById(R.id.btn1);
et = (EditText)this.findViewById(R.id.et1);
btn1.setOnClickListener(this);
}
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
Intent in = new Intent();
in.setClass(this,active2.class);
// 向 active2 传值
in.putExtra("tt", et.getText().toString());
this.startActivity(in);
this.finish();
}
}
active java 代码
package zziss.android.activetest;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
public class active2 extends Activity implements View.OnClickListener{
private Button btn1;
private EditText et;
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.main2);
btn1 = (Button)this.findViewById(R.id.btn1);
et = (EditText)this.findViewById(R.id.et1);
Bundle data = this.getIntent().getExtras();
if (data.containsKey("tt"))
et.setText(data.getString("tt"));
btn1.setOnClickListener(this);
}
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
Intent in = new Intent();
in.setClass(this,ActiveTestActivity.class);
this.startActivity(in);
this.finish();
}
}
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
public class active2 extends Activity implements View.OnClickListener{
private Button btn1;
private EditText et;
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.main2);
btn1 = (Button)this.findViewById(R.id.btn1);
et = (EditText)this.findViewById(R.id.et1);
Bundle data = this.getIntent().getExtras();
if (data.containsKey("tt"))
et.setText(data.getString("tt"));
btn1.setOnClickListener(this);
}
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
Intent in = new Intent();
in.setClass(this,ActiveTestActivity.class);
this.startActivity(in);
this.finish();
}
}