因为在
<Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/button_send" android:onClick="sendMessage" />中,点击调用的方法为sendMessage,所以在后台Activity中加入这个方法。
/** Called when the user clicks the Send button */ public void sendMessage(View view) { // Do something in response to button }在这个方法中加入以下代码:Intent intent = new Intent(this, DisplayMessageActivity.class); EditText editText = (EditText) findViewById(R.id.edit_message); String message = editText.getText().toString(); intent.putExtra(EXTRA_MESSAGE, message);其中,要导入:import android.content.Intent;DisplayMessageActivity现在还没有创建。 另外创建一个Activity, 在DisplayMessageActivity
的onCreate()方法中:Intent intent = getIntent(); String message = intent.getStringExtra(MainActivity.EXTRA_MESSAGE);得到Intent,并得到发过来的信息。MainActivity
通过以下方式显示信息。在AndroidManifest.xml加入这个类的相关内容:
<application ... > ... <activity android:name="com.example.myfirstapp.DisplayMessageActivity" android:label="@string/title_activity_display_message" android:parentActivityName="com.example.myfirstapp.MainActivity" > <meta-data android:name="android.support.PARENT_ACTIVITY" android:value="com.example.myfirstapp.MainActivity" /> </activity> </application>完整的源代码。略