Android Studio 点击按钮实现页面跳转、网页跳转

本文展示了在Android应用中如何通过Intent实现页面跳转到nextActivity以及网页跳转到指定URL。Btn1点击启动nextActivity,Btn2点击打开网页。详细代码已给出,包括XML布局和Java活动类。
摘要由CSDN通过智能技术生成

页面跳转、网页跳转

1)页面跳转

Btn1=findViewById(R.id.btn_1);
        Btn1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent=new Intent(MainActivity.this,nextActivity.class);
                startActivity(intent);
            }
        });

2)网页跳转

Btn2=findViewById(R.id.btn_2);
        final Uri uri=Uri.parse("https://home.cnblogs.com/u/kori/");
        Btn2.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent=new Intent(Intent.ACTION_VIEW,uri);
                startActivity(intent);
            }
        });

3)完整代码

activity_main.xml:

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.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">

    <Button
        android:id="@+id/btn_1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="JUMP"
        android:textSize="20sp"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.529"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintVertical_bias="0.382" />

    <Button
        android:id="@+id/btn_2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="120dp"
        android:text="WEB"
        android:textSize="20sp"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.529"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/btn_1" />
</androidx.constraintlayout.widget.ConstraintLayout>

next.xml:(准备跳转到页面的布局)

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.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">

    <TextView
        android:id="@+id/textView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="SUCCESS!"
        android:textStyle="bold"
        android:textSize="20sp"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>

MainActivity.java:

package com.example.jumpto;

import androidx.appcompat.app.AppCompatActivity;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;

public class MainActivity extends AppCompatActivity {

    Button Btn1,Btn2;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        Btn1=findViewById(R.id.btn_1);
        Btn1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent=new Intent(MainActivity.this,nextActivity.class);
                startActivity(intent);
            }
        });

        Btn2=findViewById(R.id.btn_2);
        final Uri uri=Uri.parse("https://home.cnblogs.com/u/kori/");
        Btn2.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent=new Intent(Intent.ACTION_VIEW,uri);
                startActivity(intent);
            }
        });
    }
}

nextActivity.java:

package com.example.jumpto;

import android.os.Bundle;
import android.widget.Button;
import androidx.appcompat.app.AppCompatActivity;

public class nextActivity extends AppCompatActivity {

        Button Btn1;
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.next);
        }
    }

4)结果呈现

    

最后

Android Studio 点击按钮实现页面跳转、网页跳转就到这了,更多Android Studio相关资料(面试题)可以扫码免费领取!

Android Studio 安装教程

img

有了Java阶段的学习,这一阶段建议以视频学习为主辅以图书查漏补缺。如果以图书为主,可以根据图书讲解敲代码,辅以教学视频查漏补缺。遇到问题可以去百度,入门的问题一般会有很多人遇到,并且给出比较好的解答。

需要掌握基本知识点,比如四大组件如何使用、如何创建Service、如何进行布局、简单的自定义View、动画、网络通信等常见技术。

全套零基础教程已经为你们准备好了,需要的可以添加下方二维码免费领取

全套安卓基础教程

img

img

img

imgimg

### 回答1: 在 Android 中,可以使用 Intent 对象来启动其他 Activity,实现页面跳转。下面是一些示例代码: 1. 从当前 Activity 跳转到目标 Activity: ```java Intent intent = new Intent(CurrentActivity.this, TargetActivity.class); startActivity(intent); ``` 2. 在跳转时传递参数: ```java Intent intent = new Intent(CurrentActivity.this, TargetActivity.class); intent.putExtra("key", value); startActivity(intent); ``` 在目标 Activity 中可以通过以下代码获取传递过来的参数: ```java String value = getIntent().getStringExtra("key"); ``` 3. 从当前 Activity 跳转到目标 Activity 并等待结果返回: ```java Intent intent = new Intent(CurrentActivity.this, TargetActivity.class); startActivityForResult(intent, requestCode); ``` 在目标 Activity 中可以通过以下代码设置返回值: ```java Intent intent = new Intent(); intent.putExtra("result", value); setResult(RESULT_OK, intent); finish(); ``` 在当前 Activity 中可以通过以下代码获取返回的结果: ```java @Override protected void onActivityResult(int requestCode, int resultCode, Intent data) { if (requestCode == requestCode && resultCode == RESULT_OK) { String result = data.getStringExtra("result"); } } ``` 以上是一些基本的页面跳转示例,具体实现根据实际需求进行调整。 ### 回答2: Android页面跳转是指在Android应用中,从一个页面跳转到另一个页面的过程。这是Android应用开发中非常常见的操作,通常用于实现不同页面之间的切换和交互。 在Android中,页面跳转可以通过在Java代码中使用Intent类来实现。首先,我们需要在当前页面的代码中创建一个Intent对象,并通过Intent的构造函数指定要跳转到的目标页面的类名。然后,调用startActivity()方法,将这个Intent对象作为参数传入,即可完成页面跳转。 除了简单的页面跳转外,Intent还可以传递数据。我们可以通过Intent的putExtra()方法将数据存放在Intent对象中,并在目标页面的代码中使用getIntent()方法获取传递过来的数据。 另外,Android还提供了其他方式实现页面跳转,例如使用Fragment实现页面的切换和跳转。通过在一个Activity中嵌入不同的Fragment,并在需要切换页面时动态替换Fragment,可以实现复杂的页面跳转效果。 总的来说,Android页面跳转是通过使用Intent来实现的,可以实现不同页面之间的切换和数据传递。开发者可以根据应用的需求选择适合的方式来实现页面跳转,以提供更好的用户体验。 ### 回答3: Android页面跳转是指在应用程序中从一个界面(Activity)切换到另一个界面的过程。在Android开发中,实现页面跳转通常使用Intent来实现。 1. 首先,需要在当前页面的某个事件触发时创建一个Intent对象。例如,可以在用户点击一个按钮时创建Intent对象。 2. 然后,通过调用Intent的setClass方法或者setComponent方法设置目标页面的类名或者组件名。这里需要指定目标页面的完整包名和类名,或者通过组件名来指定。 3. 如果目标页面需要传递参数,可以通过调用Intent的putExtra方法将参数传递给目标页面。参数的类型可以是基本数据类型或者实现了Parcelable接口的对象。 4. 最后,通过调用当前页面的startActivity方法或者startActivityForResult方法启动目标页面。其中,startActivity方法用于启动一个新页面,而startActivityForResult方法用于启动一个新页面并且希望在新页面关闭时能够得到返回的结果。 在目标页面中,可以通过getIntent方法获取启动当前页面的Intent对象,从而获取传递过来的参数。如果需要返回结果给上一个页面,可以调用setResult方法来设置返回结果,并在目标页面关闭时通过finish方法返回结果给上一个页面。 总之,Android页面跳转是通过创建和配置Intent对象实现的,通过启动目标页面来实现页面之间的切换。同时,可以通过Intent传递参数以及返回结果给上一个页面。
评论 5
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值