Android开发之页面跳转

 

 activity_main

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    >

    <Button
        android:id="@+id/button"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="跳转到其它Activity"
        android:textAllCaps="false"
        android:background="@android:color/holo_blue_light"
        android:textColor="@android:color/white"
        android:textSize="25sp"
        android:padding="5dp"
        android:layout_centerInParent="true"/>

</RelativeLayout>
 

MainActivity 

package com.etime.myproject;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;

/**
 * 为便于测试,请按照以下要求进行:
 * 1、MainActivity的mButton.setOnClickListener()中调用testIntent1时
 *    在AnotherActivity的mButton.setOnClickListener()中调用testIntent1
 *
 * 2、MainActivity的mButton.setOnClickListener()中调用testIntent2时
 *    在AnotherActivity的mButton.setOnClickListener()中调用testIntent2
 *
 * 3、请在配置文件AndroidManifest中配置AnotherActivity
 *    <activity android:name=".AnotherActivity"/>
 */
public class MainActivity extends Activity {
    private Button mButton;
    private Context mContext;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        mContext=this;
        mButton=(Button)findViewById(R.id.button);
        mButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                testIntent2();
            }
        });
    }

    //利用Intent启动另外的Activity
    private void testIntent1(){
        Intent intent=new Intent(mContext,AnotherActivity.class);
        startActivity(intent);
    }

    //利用Intent启动另外的Activity
    private void testIntent2(){
        Intent intent=new Intent(mContext,AnotherActivity.class);
        intent.putExtra("name","hanmeimei");
        intent.putExtra("age",19);
        startActivity(intent);
    }
}

 

activity_another

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    >

    <Button
        android:id="@+id/button"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="This is Another Activity"
        android:background="@android:color/holo_blue_light"
        android:textColor="@android:color/white"
        android:textSize="20sp"
        android:padding="4dp"
        android:textAllCaps="false"
        android:layout_centerInParent="true"/>
</RelativeLayout>
 

AnotherActivity 

package com.etime.myproject;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;

public class AnotherActivity extends Activity {
    public static final String TAG="TAG";
    private Button mButton;
    private Context mContext;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_another);
        init();
    }

    private void init(){
        mContext=this;
        mButton=(Button)findViewById(R.id.button);
        mButton.setOnClickListener(new View.OnClickListener(){
            @Override
            public void onClick(View v) {
                //关闭页面
                finish();
            }
        });

        testIntent2();
    }

    private void testIntent1(){

    }

    private void testIntent2(){
        Intent intent=this.getIntent();
        String name=intent.getStringExtra("name");
        int age=intent.getIntExtra("age",0);
        Log.i(TAG,"name="+name);
        Log.i(TAG,"age="+age);
    }

}
 

AndroidManifest

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

    <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/AppTheme">
        <activity android:name=".MainActivity">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <!-- 注册Activity -->
        <activity android:name=".AnotherActivity"/>
    </application>

</manifest>

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
Android Studio 是一款由谷歌开发的集成开发环境(IDE),用于开发 Android 应用程序。在 Android Studio 中,界面跳转和数据传输是移动应用开发中两个重要的方面。 界面跳转: 1. 使用 Intent:在 Android 中,Intent 是一个非常重要的类,用于启动一个 Activity。通过使用 Intent,你可以从一个 Activity 跳转到另一个 Activity。Intent 包含一个动作(action)和一个或多个数据(data),这决定了 Activity 的类型和需要的数据。 2. 使用导航架构:Android 支持多种导航架构,如 Tab 组件、Drawer、ViewPager 等,可以让你轻松地实现页面间的跳转。例如,你可以创建一个 Drawer 组件,其中包含多个选项,用户可以通过点击这些选项在不同的页面之间进行跳转。 3. 使用 Fragment:Fragment 是 Android 中的一个重要组件,用于构建应用程序的界面。Fragment 可以与多个视图组件(如 ViewPager 或 ListView)关联,允许你以更灵活的方式创建和管理界面。你可以将 Fragment 添加到 Activity 中,并在需要时进行跳转。 数据传输: 1. 使用 Intent extras:Intent 提供了一个机制,可以将数据从一个 Activity 或 Service 传递到另一个。你可以使用 Intent 的 extras 字段来传递数据,例如使用 Bundle 类来存储键值对。 2. 使用 SharedPreferences:SharedPreferences 是 Android 提供的一种轻量级存储机制,用于保存应用程序的短期配置和数据。SharedPreferences 可以用于保存简单的数据类型,如字符串、布尔值或整数值。 3. 使用网络通信:如果你的应用程序需要与服务器进行数据交互,你可能需要使用网络通信技术。Android 支持多种网络通信方式,如 HTTP、HTTPS 和蓝牙通信等。你可以使用 Java 的 HttpURLConnection 类或第三方库(如 OkHttp)来实现网络通信。 4. 使用数据库:如果你的应用程序需要存储大量数据,你可能需要使用数据库。Android 支持 SQLite 数据库,它是一种轻量级的关系型数据库,可以用于存储和检索应用程序的数据。 总的来说,Android Studio 提供了一些强大的工具和功能,可以帮助你实现移动应用开发中的界面跳转和数据传输。你可以根据具体的需求和场景选择合适的技术和方法来实现这些功能。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

谷哥的小弟

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值