赞
踩
通过前面的学习,知道如何在一个Activity中加入一个文本框和一个按钮,现在来学习如何在MainActivity中启动另一个新的Activity。
一、响应按钮的点击
为了响应按钮(button)的点击事件, 打开activity_main.xml布局文件,加入android:onClick属性到<Button>元素中。如下:
- <Button
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:text="@string/button_send"
- android:onClick="sendMessage" />
其中android:onClick属性值"sendMessage",是activity中的方法名,当用户点击该按钮时,系统调用该方法。
打开MainActivity类(在工程目录src/中),加入相应的方法sendMessage(),如下:
- /* Called when the user clicks the Send Button */
- public void sendMessage(View view){
- // Do something in response to button
- }
这需要导入View类:
import android.view.View;
技巧:在Eclipse中,按Ctrl+Shift+O来导入缺失的类
为了使系统匹配源代码中的方法语android:onClick中给定的方法一致,署名(signature)必须明确的显示出来,确切的说,方法必须是:
Intent intent = new Intent(this, DisplayMessageActivity.class);
此处使用的构造器有两个参数:
- /* 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);
- }
注:需要导入android.content.intent和android:widget.EditText。同时需要定义常量EXTRA_MESSAGE,如下:
- public class MainActivity extends Activity {
-
- public static final String EXTRA_MESSAGE = "com.example.myfirstapp.MESSAGE";
- ...
- }
一个Intent可以携带各种数据类型的键-值(key-value)对(称为extra)的集合。
- /* 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);
- startActivity(intent);
- }
现在需要创建一个DisplayMessageActivity类。
- import android.os.Bundle;
- import android.app.Activity;
- import android.view.Menu;
- import android.view.MenuItem;
- import android.support.v4.app.NavUtils;
- import android.annotation.TargetApi;
- import android.os.Build;
-
- public class DisplayMessageActivity extends Activity {
-
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.activity_display_message);
- // Show the Up button in the action bar.
- setupActionBar();
- }
-
- /**
- * Set up the {@link android.app.ActionBar}, if the API is available.
- */
- @TargetApi(Build.VERSION_CODES.HONEYCOMB)
- private void setupActionBar() {
- if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {
- getActionBar().setDisplayHomeAsUpEnabled(true);
- }
- }
-
- @Override
- public boolean onCreateOptionsMenu(Menu menu) {
- // Inflate the menu; this adds items to the action bar if it is present.
- getMenuInflater().inflate(R.menu.display_message, menu);
- return true;
- }
-
- @Override
- public boolean onOptionsItemSelected(MenuItem item) {
- switch (item.getItemId()) {
- case android.R.id.home:
- // This ID represents the Home or Up button. In the case of this
- // activity, the Up button is shown. Use NavUtils to allow users
- // to navigate up one level in the application structure. For
- // more details, see the Navigation pattern on Android Design:
- //
- // http://developer.android.com/design/patterns/navigation.html#up-vs-back
- //
- NavUtils.navigateUpFromSameTask(this);
- return true;
- }
- return super.onOptionsItemSelected(item);
- }
-
- }
- <?xml version="1.0" encoding="utf-8"?>
- <manifest xmlns:android=" http://schemas.android.com/apk/res/android"
- package="com.example.myfirstapp"
- android:versionCode="1"
- android:versionName="1.0" >
-
- <uses-sdk
- android:minSdkVersion="8"
- android:targetSdkVersion="17" />
-
- <application
- android:allowBackup="true"
- android:icon="@drawable/ic_launcher"
- android:label="@string/app_name"
- android:theme="@style/AppTheme" >
- <activity
- android:name="com.example.myfirstapp.MainActivity"
- android:label="@string/app_name" >
- <intent-filter>
- <action android:name="android.intent.action.MAIN" />
-
- <category android:name="android.intent.category.LAUNCHER" />
- </intent-filter>
- </activity>
- <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>
-
- </manifest>
其中android:parentActivityName属性声明了在App逻辑层次中Activity的父Activity的名字。系统使用该值来执行缺省的导航行为,如Android 4.1中的up导航。
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
-
- // Get the message from the intent
- Intent intent = getIntent();
- String message = intent.getStringExtra(MainActivity.EXTRA_MESSAGE);
-
- // Create the text view
- TextView textView = new TextView(this);
- textView.setTextSize(40);
- textView.setText(message);
-
- // setContentView(R.layout.activity_display_message);
- setContentView(textView);
-
- // Show the Up button in the action bar.
- setupActionBar();
-
- }
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。