2021年1月15日 界面跳转

今天学习了部分的界面跳转功能,具体实现如下:
App功能:
01 在第一个Activity输入消息
02 点击第一个Activity的发送按钮
03 发送消息到第二个Activity
04 第二个Activity显示收到的消息
开发步骤:
01 新建单Activty和Layout的基础App
02 添加第二个Activty和Layout
03 启动第二个Activty
04 使用Intent传递数据
 
选中MainActivity,菜单栏->Refactor->Rename, 快捷键Shift+F6 改名为CreateMessageActivity
 
 为什么不能直接修改名字呢?
因为我们使用向导时至少有三处做了自动的修改,
01 Activity的Java文件名和类名
02 Layout的上下文context
03 Manifest的activity名称
 
启动新的activity:
android.intent.action.MAIN决定App最先启动的
Activity android.intent.category.LAUNCHER决定应用程序是否显示 在App列表
二者配合使用,决定入口Activity
完成两个界面布局
 
功能界面:

具体实现代码:

 AndroidManifest.xml:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="bjfu.it.sun.messager">

    <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.Messager">
        <activity android:name=".ReceiveMessageActivity">


        </activity>
        <activity android:name=".CreatMessageActivity">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>

</manifest>

CreatMessageActivity:

package bjfu.it.sun.messager;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;

public class CreatMessageActivity extends AppCompatActivity {
//定义常量,作为消息的KEY
    public static final String MESSAGE_KEY="bjfu.it.sun.messager";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_create_message);
    }
    public void onSendMessage(android.view.View button){
        //获得标记框引用
        EditText editText=findViewById(R.id.input);
        //取出编辑框文字
        String message =editText.getText().toString();

        Intent intent =new Intent(this,ReceiveMessageActivity.class);
        intent.putExtra(MESSAGE_KEY,message);
        startActivity(intent);
    }

}

ReceiveMessageActivity:

package bjfu.it.sun.messager;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.os.Bundle;
import android.widget.TextView;

public class ReceiveMessageActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_receive_message);
        //获得intent引用
        Intent intent=getIntent();
        //根据KEY取出value
        String message=intent.getStringExtra(CreatMessageActivity.MESSAGE_KEY);
        //获得文本框引用,设置文字
        TextView textView=findViewById(R.id.output);
        textView.setText(message);
    }
}

activity_create_message.xml:

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".CreatMessageActivity">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical">

        <EditText
            android:id="@+id/input"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:ems="10"
            android:hint="@string/hint"
            android:inputType="textPersonName"
            android:textSize="30sp" />

        <Button
            android:id="@+id/button3"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:onClick="onSendMessage"
            android:text="@string/hint" />
    </LinearLayout>
</androidx.constraintlayout.widget.ConstraintLayout>

activity_receive_message.xml:

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".ReceiveMessageActivity">

    <TextView
        android:id="@+id/output"
        android:layout_width="358dp"
        android:layout_height="118dp"
        android:layout_marginStart="4dp"
        android:text="2nd Activity"
        android:textSize="30sp"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>

strings.xml:

<resources>
    <string name="app_name">Messager</string>
    <string name="send">Send Message</string>
    <string name="hint">Enter a Message</string>
</resources>

遇到的问题:

在ReceiveMessageActivity加入编辑框时一直提示错误,

 

 点一下魔法棒就好了,

还有就是,

public void onSendMessage(android.view.View button){
Intent intent =new Intent(this,ReceiveMessageActivity.class);
startActivity(intent);
}这个,.class特别重要,加上问题就解决,否则一堆飘红
 
明天继续。

 

原文地址:https://www.cnblogs.com/j-y-s/p/14383647.html