1、启动另一个Activity
-
完成上面的步骤之后,实现了自己设计的MainActivity界面,但界面上的Button还不能响应单击的事件。
-
定义button上的事件以及事件处理程序
-
定义一个新的响应页面来展示处理结果
1. 响应SEND按钮单击事件
(1)修改MainActivity的布局文件res/layout/activity_main.xml,给Button元素添加一个属性android:onClick
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="match_parent">
<EditText android:id="@+id/edit_message"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:hint="@string/edit_message"/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/button_send"
android:onClick="sendMessage"/>
</LinearLayout>
(2)根据代码设置,单击这个按钮时,则由MainAcivity中的sendMessage()方法进行按钮单击事件处理。
package com.example.hello_world;
import androidx.appcompat.app.AppCompatActivity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
public class MainActivity extends AppCompatActivity {
public final static String EXTRA_MESSAGE = "测试消息hello world";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
public void sendMessage(View view) {
Intent intent = new Intent(this,DisplayMessageActivity.class);
EditText editText = findViewById(R.id.edit_message);
String message = editText.getText().toString();
intent.putExtra(EXTRA_MESSAGE,message);
startActivity(intent);
}
}
2、创建Intent对象传递消息
-
Android的Intent机制可以在运行时绑定两个独立的组件,利用Intent对象在不同组件之间传递消息。
-
修改MainActivity.java文件,在sendMessage()中创建Intent对象,通过R.id.edit_message获取布局文件中定义的EditText对象,并通过Intent的putExtra()方法,把EditText中的文本信息赋给Intent对象,然后通过Intent启动另一个指定的、名称为DisplayMessageActivity 的Activity
3、创建另一个Activity(命名为DisplayMessageActivity)
package com.example.hello_world;
import android.os.Bundle;
import androidx.appcompat.app.AppCompatActivity;
public class MainActivity2 extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main2);
}
}
4、添加该Activity到manifest文件
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.hello_world">
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/Theme.Hello_world">
<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=".DisplayMessageActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
5、运行App,发送并显示消息
Android App开发起步