<TextView
android:id="@+id/textView1"
android:layout_width="60dp"
android:layout_height="wrap_content"
android:layout_toRightOf="@id/editText1"
android:text=" + "
android:textSize="30sp" />
<EditText
android:id="@+id/editText2"
android:layout_width="60dp"
android:layout_height="wrap_content"
android:layout_toRightOf="@id/textView1" />
<TextView
android:id="@+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_toRightOf="@id/editText2"
android:text=" = "
android:textSize="30sp" />
<EditText
android:id="@+id/editText3"
android:layout_width="70dp"
android:layout_height="wrap_content"
android:layout_toRightOf="@id/textView2" />
<Button
android:id="@+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@id/editText3"
android:layout_centerInParent="true"
android:layout_marginTop="30dp"
android:text="跳转到SecondActivity"/>
</RelativeLayout>
second.xml:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<TextView
android:id="@+id/textView1"
android:layout_width="70dp"
android:layout_height="wrap_content"
android:textSize="20sp"/>
<EditText
android:id="@+id/editText1"
android:layout_width="60dp"
android:layout_height="wrap_content"
android:layout_toRightOf="@id/textView1"/>
<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_toRightOf="@id/editText1"
android:text="返回给MainActivity"/>
</RelativeLayout>
然后我们来看看两个Activity
MainActivity:
public class MainActivity extends Activity
{
private Button button;
private EditText editText1;
private EditText editText2;
private EditText editText3;
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
button = (Button)findViewById(R.id.button);
editText1 = (EditText)findViewById(R.id.editText1);
editText2 = (EditText)findViewById(R.id.editText2);
editText3 = (EditText)findViewById(R.id.editText3);
button.setOnClickListener(new OnClickListener()
{
@Override
public void onClick(View v)
{
Intent intent = new Intent();
intent.putExtra("message", editText1.getText().toString().trim() + " + " + editText2.getText().toString().trim() + " = ?");
intent.setClass(MainActivity.this, SecondActivity.class);
/*
* 如果希望启动另一个Activity,并且希望有返回值,则需要使用startActivityForResult这个方法,
* 第一个参数是Intent对象,第二个参数是一个requestCode值,如果有多个按钮都要启动Activity,则requestCode标志着每个按钮所启动的Activity
*/
startActivityForResult(intent, 1000);
}
});
}
/**
* 所有的Activity对象的返回值都是由这个方法来接收
* requestCode: 表示的是启动一个Activity时传过去的requestCode值
* resultCode:表示的是启动后的Activity回传值时的resultCode值
* data:表示的是启动后的Activity回传过来的Intent对象
*/
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data)
{
super.onActivityResult(requestCode, resultCode, data);
if(requestCode == 1000 && resultCode == 1001)
{
String result_value = data.getStringExtra("result");
editText3.setText(result_value);
}
}