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


    0)提示

    • 页面跳转时出现黑屏闪退的情况,可以参考这篇文章
    • 网页跳转需要手机联网,可以参考这篇文章,如果还不行的话,换成高版本的模拟机再试试

    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)结果呈现 

        

  • 相关阅读:
    第四次作业的完善
    第四次作业
    第三次附加作业
    采用mybatis-plus并且在controller方法上加@Transactional,一共经过了多少层动态代理
    mybatis事务不起作用,原来表引擎是MyISAM
    从docker registry拉取所需secret的namespace要与容器一样
    kubecfg.p12要记住密码,并且要导入到个人区
    Four Types of Books
    函数式编程与响应式编程
    类的【TypeVariable】和变量的【ParameterizedType】
  • 原文地址:https://www.cnblogs.com/kori/p/12496197.html
Copyright © 2020-2023  润新知