Android studio 多个界面跳转(意图的应用)

本文介绍了如何在Android Studio中使用意图实现从MainActivity到register和login_Activity的界面跳转,展示了在不同Activity间传递数据和活动间的导航过程。
摘要由CSDN通过智能技术生成

Android studio 多个界面跳转(意图的应用)

如下图所示,是建立的一些个文件,3个activity对应3个前端界面,本次实验要进行的是在MainActivity对register的跳转,MainActivity到login_Activity的跳转。
在这里插入图片描述
下面依次为这三个界面的图示
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
意图使用显示意图模式
在这里插入图片描述
实验操作的视频已经发布在个人的blog当中,是视频文件,大家可以去观看

以下是代码的部分

activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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"
    android:background="@drawable/back"
    tools:context=".MainActivity"
    android:orientation="vertical"
   >

    <ImageView
        android:layout_width="60dp"
        android:layout_height="60dp"
        android:layout_marginLeft="180dp"
        android:layout_marginTop="160dp"
        android:background="@drawable/qq"/>

    <TextView
        android:id="@+id/lable"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginLeft="20dp"
        android:layout_marginTop="20dp"
        android:text="QQ"
        android:textSize="35dp"
        android:textColor="@color/black"/>
    <EditText
        android:id="@+id/userName"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
         android:hint="请输入您的用户名"
        android:textSize="20dp"
        android:layout_marginLeft="20dp"
        android:layout_marginTop="18dp"/>
    <EditText
        android:id="@+id/psw"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:hint="请输入您的密码"
        android:textSize="20dp"
        android:layout_marginLeft="20dp"
        android:layout_marginTop="18dp"/>

    <Button
        android:id="@+id/btn_login"
        android:layout_width="240dp"
        android:layout_height="48dp"
        android:text="登 录"
        android:textColor="#FFFFFF"
        android:backgroundTint="@android:color/darker_gray"
        android:textSize="18dp"
        android:layout_gravity="center_horizontal"
        android:layout_marginTop="15dp"/>
    <Button
        android:id="@+id/to_register"
        android:layout_width="240dp"
        android:layout_height="48dp"
        android:text="注 册"
        android:textColor="#FFFFFF"
        android:backgroundTint="@android:color/darker_gray"
        android:textSize="18dp"
        android:layout_gravity="center_horizontal"
        android:layout_marginTop="15dp"/>
</LinearLayout>

activity_login.xml代码

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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"
    android:background="@drawable/shu"
    android:orientation="vertical"
    tools:context=".MainActivity">
    <TextView
        android:id="@+id/news_item_content_text_view"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginLeft="110dp"
        android:layout_marginTop="300dp"
        android:text="登录成功"
        android:textSize="45dp" />

    <Button
        android:id="@+id/btn_logout"
        android:layout_width="220dp"
        android:layout_height="48dp"
        android:layout_centerHorizontal="true"
        android:layout_gravity="center_horizontal"
        android:layout_marginLeft="100dp"
        android:layout_marginTop="400dp"
        android:backgroundTint="@color/purple_200"
        android:text="退  出"
        android:textColor="@color/black"
        android:textSize="18dp"
        android:onClick="method1"
       />
</RelativeLayout>

activity_register.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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"
    android:background="@drawable/back"
    tools:context=".MainActivity"
    android:orientation="vertical"
    >



    <TextView
        android:id="@+id/lable"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginLeft="20dp"
        android:layout_marginTop="20dp"
        android:text="QQ"
        android:textSize="35dp"
        android:textColor="@color/black"/>
    <EditText
        android:id="@+id/userName"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:hint="请输入您的用户名"
        android:layout_marginLeft="40dp"
        android:layout_marginTop="25dp"/>
    <EditText
        android:id="@+id/psword"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:hint="请输入您的密码"
        android:layout_marginLeft="40dp"
        android:layout_marginTop="25dp"/>
    <EditText
        android:id="@+id/psword2"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:hint="请再次输入您的密码"
        android:layout_marginLeft="40dp"
        android:layout_marginTop="25dp"/>
    <Button
        android:id="@+id/btn_register"
        android:layout_width="220dp"
        android:layout_height="40dp"
        android:text="注 册"
        android:textColor="#FFFFFF"
        android:backgroundTint="@android:color/darker_gray"
        android:textSize="18dp"
        android:layout_gravity="center_horizontal"
        android:layout_marginTop="25dp"/>
    <Button
        android:id="@+id/to_login"
        android:layout_width="220dp"
        android:layout_height="40dp"
        android:text="返回登录"
        android:textColor="#FFFFFF"
        android:backgroundTint="@android:color/darker_gray"
        android:textSize="18dp"
        android:layout_gravity="center_horizontal"
        android:layout_marginTop="25dp"/>
</LinearLayout>

Mainactivity.java

package com.example.myapplication;

import androidx.appcompat.app.AlertDialog;
import androidx.appcompat.app.AppCompatActivity;

import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

//这个是主界面
public class MainActivity extends AppCompatActivity {

    @Override

    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        TextView userName=findViewById(R.id.userName);
        TextView psw=findViewById(R.id.psw);
        Button btn_login=findViewById(R.id.btn_login);
        Button to_register=findViewById(R.id.to_register);
        String username=getIntent().getStringExtra("username");
        String password=getIntent().getStringExtra("psw");
        userName.setText(username);
        psw.setText(password);
        to_register.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent=new Intent(MainActivity.this,register.class);
                startActivity(intent);

            }
        });
        btn_login.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent=new Intent(MainActivity.this,login_Activity.class);
                startActivity(intent);
            }
        });
    }



}

register.java

package com.example.myapplication;

import androidx.appcompat.app.AppCompatActivity;

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

//这个是控制注册界面的
public class register extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_register);
        TextView userName=findViewById(R.id.userName);
        TextView psw=findViewById(R.id.psword);
        TextView psw2=findViewById(R.id.psword2);
        Button btn_register=findViewById(R.id.btn_register);
        Button to_login=findViewById(R.id.to_login);
        btn_register.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent =new Intent(register.this,MainActivity.class);
                intent.putExtra("username",userName.getText().toString());
                  intent.putExtra("password",psw.getText().toString());
                startActivity(intent);
            }
        });
        to_login.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent=new Intent(register.this,login_Activity.class);
                startActivity(intent);
            }
        });
    }

}

login_activity.java

package com.example.myapplication;

import androidx.appcompat.app.AlertDialog;
import androidx.appcompat.app.AppCompatActivity;

import android.content.DialogInterface;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;

//这个是控制最终界面的
public class login_Activity extends AppCompatActivity {
    Button btn_logout;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login);
        btn_logout=findViewById(R.id.btn_logout);

    }
    public void method1(View view) {
        AlertDialog.Builder builder = new AlertDialog.Builder(this);
        builder.setIcon(R.mipmap.ic_launcher)
                .setTitle("哎呀呀,怎么要退出了")
                .setMessage("要退出吗?")
                .setPositiveButton("确定", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialogInterface, int i) {
                        dialogInterface.dismiss();
                        login_Activity.this.finish();
                    }
                })
                .setNegativeButton("否", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialogInterface, int i) {
                        dialogInterface.dismiss();
                    }
                });
        AlertDialog ad=builder.create();
        //show()方法展示
        ad.show();

    }
}
### 回答1: Android Studio可以创建多个界面,每个界面都可以包含不同的布局和功能。你可以使用Activity或Fragment来创建界面,也可以使用自定义View来实现特定的功能。在Android Studio中,你可以使用布局编辑器来设计和编辑界面,也可以使用代码编辑器来编写和修改代码。同时,你还可以使用资源管理器来管理应用程序的资源文件,如图片、字符串和样式等。总之,Android Studio提供了丰富的工具和功能,帮助你轻松创建多个界面应用程序。 ### 回答2: Android Studio是一款用于开发Android应用程序的集成开发环境(IDE),它提供了丰富的工具和功能,可以帮助开发者创建多个界面Android应用程序。 在Android Studio中,可以使用多个界面来构建应用程序。每个界面都是一个XML布局文件,其中定义了界面的组件和布局。可以使用可视化编辑器来设计界面,也可以直接编辑XML代码。通过使用多个界面,开发者可以实现应用程序的不同功能和页面。 创建多个界面的第一步是创建一个新的XML布局文件。可以右键点击项目文件夹,选择"New"->"Android Resource File"来创建一个新的XML文件。在创建界面时,可以选择不同的布局类型,例如线性布局、相对布局等。 在界面文件中,可以添加各种不同的组件,例如按钮、文本框、图片等。可以使用布局属性来定义组件在界面中的位置和大小。 除了XML布局文件,还需要在Java代码中连接界面应用程序的逻辑。可以使用findViewById()方法来获取界面中的组件,并在代码中操作它们。可以为按钮等组件添加事件监听器,以响应用户的操作。 要使多个界面交互起来,可以使用Intent。Intent是Android应用程序之间进行通信的一种机制。通过创建Intent对象,可以在不同的界面之间传递数据,并启动其他界面。 总之,Android Studio提供了创建多个界面的工具和功能,开发者可以根据应用程序的需求来设计和实现多个界面。这样可以实现丰富多样的用户体验,并让应用程序更加灵活和易用。 ### 回答3: Android Studio是一款用于开发Android应用程序的集成开发环境(IDE)。在Android Studio中,我们可以创建多个界面,以便实现复杂的应用程序。 创建多个界面的首要步骤是创建不同的Activity或Fragment。Activity是一个用户界面窗口,Fragment是一个可以嵌入到Activity中的可重用组件。 在Android Studio中,我们可以通过新建Activity或者Fragment的方式创建多个界面。在创建界面之后,我们可以在布局文件中添加各种控件、设置布局和样式,以及处理用户操作等。 为了在不同的界面之间实现页面切换,我们可以使用Intent和FragmentManager。Intent可以用于Activity之间的页面跳转,而FragmentManager可以用于Fragment之间的切换。通过这些方式,我们可以在多个界面之间进行数据的传递和共享,并实现复杂的逻辑操作。 为了提高开发效率和代码的组织性,我们还可以使用其他技术和工具来管理多个界面,例如使用MVP模式或者MVVM模式进行架构设计,使用ViewModel或者LiveData进行数据绑定和转换,使用Dependency Injection(依赖注入)来管理组件的依赖关系等。 总之,Android Studio提供了丰富的功能和工具,可以方便地创建和管理多个界面。通过合理利用这些功能和工具,我们可以开发出功能丰富、交互友好的Android应用程序。
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值