Android实现界面跳转功能

2020-10-10 0 530

本文实例为大家分享了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实现界面跳转功能

完成。

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持自学编程网。

遇见资源网 Android Android实现界面跳转功能 http://www.ox520.com/23739.html

常见问题

相关文章

发表评论
暂无评论
官方客服团队

为您解决烦忧 - 24小时在线 专业服务