Android实现界面跳转功能

作者:Red&&Black 时间:2022-05-07 21:51:32 

本文实例为大家分享了Android实现界面跳转的具体代码,供大家参考,具体内容如下

布局


<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.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=".MainActivity">

<!-- 线性布局 、垂直排列 -->
<LinearLayout
  android:orientation="vertical"
  android:layout_width="match_parent"
  android:layout_height="match_parent">
 <!-- 编辑框
@string/hint 表示 res/values/strings.xml下名为hint的标签
   strings.xml用于字符串资源及其格式化
 -->
 <EditText
   android:layout_width="match_parent"
   android:layout_height="wrap_content"
   android:inputType="textPersonName"
   android:text="Name"
   android:ems="10"
   android:id="@+id/input" android:hint="@string/hint"/>
 <Button
   android:text="@string/send"
   android:layout_width="match_parent"
   android:layout_height="wrap_content" android:id="@+id/button2" android:onClick="send"/>
</LinearLayout>

</android.support.constraint.ConstraintLayout>

或者点击text左边的design进行布局

响应onclick


package com.android02;

import android.content.Intent;
import android.os.Bundle;
import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.Snackbar;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.EditText;

public class MainActivity extends AppCompatActivity {

//定义常量,作为消息的key
public final static String MESSAGE_KEY="com.android2";

@Override
protected void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);
 /**
  * 指定布局(res下的activity_main.xml编译成r.java)
  */
 setContentView(R.layout.activity_main);
}

/**
 *响应onclick事件
 */
public void send(View button){

//以id获取EditText
 EditText editText = findViewById(R.id.input);

//获取编辑框文字
 String message = editText.getText().toString();

//activity、service和broadcast receiver通过Intent进行交互
 Intent intent = new Intent(this,ReceiveActivity.class);

//在intent中附加信息
 intent.putExtra(MESSAGE_KEY,message);
 startActivity(intent);
}

}

创建ReceiveActivity

右击java>>new>>Activity>>Empty Activity
然后进入创建页面指定Activity的名字…
然后它在AndroidManifest.xml中自动注册


package com.android02;

import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.TextView;

public class ReceiveActivity extends AppCompatActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
 super.onCreate(savedInstanceState);
 setContentView(R.layout.activity_receive);

//获取intent引用
 Intent intent = getIntent();

//以MESSAGE_KEY获取获取编辑框文字
 String message = intent.getStringExtra(MainActivity.MESSAGE_KEY);

//以id获取TextView
 TextView textView = findViewById(R.id.output);

//显示message
 textView.setText(message);

}
}

测试

通过AVD manager 创建虚拟手机或使用真机测试

Android实现界面跳转功能

Android实现界面跳转功能

完成。

来源:https://blog.csdn.net/m0_46267375/article/details/108724554

标签:Android,界面跳转
0
投稿

猜你喜欢

  • Java 单例模式的实现资料整理

    2022-05-29 21:27:33
  • Maven 多profile及指定编译问题的解决

    2022-04-22 23:43:24
  • JAVA遍历一个文件夹中的所有文件的小例子

    2023-04-07 17:13:49
  • Java实现调用外部程序的示例代码

    2023-11-10 06:43:12
  • java mybatis如何操作postgresql array数组类型

    2023-04-25 22:59:37
  • Java使用Apache POI库读取Excel表格文档的示例

    2021-06-02 04:32:49
  • SpringBoot项目从搭建到发布一条龙

    2023-11-21 09:28:44
  • Spring与Struts整合之让Spring管理控制器操作示例

    2022-08-22 12:53:46
  • 详解在idea 中使用Mybatis Generator逆向工程生成代码

    2023-09-09 17:05:24
  • Java背包问题求解实例代码

    2023-10-05 06:20:33
  • Maven+SSM框架实现简单的增删改查

    2023-11-16 17:14:38
  • c# base64转字符串实例

    2021-06-25 01:47:54
  • Java语言中4种内部类的超详细讲解

    2022-02-23 18:08:10
  • sharding-jdbc5.0.0实现分表实践

    2023-12-07 10:12:26
  • springboot结合maven实现多模块打包

    2022-01-16 07:13:51
  • java中staticclass静态类详解

    2021-10-12 19:47:35
  • 排序算法图解之Java冒泡排序及优化

    2022-07-16 01:28:38
  • java实现给图片加铺满的网格式文字水印

    2023-07-30 05:22:12
  • 比Math类库abs()方法性能更高的取绝对值方法介绍

    2023-10-14 07:51:36
  • 深入理解Spring事务原理

    2022-01-18 04:16:39
  • asp之家 软件编程 m.aspxhome.com