温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

使用Android实现通过界面跳转功能

发布时间:2020-10-30 18:55:56 来源:亿速云 阅读:250 作者:Leah 栏目:开发技术

今天就跟大家聊聊有关使用Android实现通过界面跳转功能,可能很多人都不太了解,为了让大家更加了解,小编给大家总结了以下内容,希望大家根据这篇文章可以有所收获。

布局

<&#63;xml version="1.0" encoding="utf-8"&#63;>
<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实现通过界面跳转功能

看完上述内容,你们对使用Android实现通过界面跳转功能有进一步的了解吗?如果还想了解更多知识或者相关内容,请关注亿速云行业资讯频道,感谢大家的支持。

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI