Android stdio 学习之 Intent 类(启动外部浏览器或实现页面组件跳转)

版权声明: https://blog.csdn.net/qq_25233621/article/details/82960167

首先

1.确保已经安装好了Android stdio 环境 

2.了解Intent类是什么

详情可查看网址:https://developer.android.com/guide/components/intents-filters

说白了,Intent就是一个捆绑各个组件的纽带,不仅仅可以联系捆绑自己应用本身的组件,同样也可以联系别的应用程序的组件。

比如说,我们常用的微信中的扫一扫功能,微信本身并不带有拍照功能,但是它可以调用系统中的相关组件,让它看起来微信自身是带有照相模块的。

3.开始实现Intent的简单外部调用

通过简单的几行代码,实现在本应用中启动浏览器或者其他相关应用

这里我们选择点击按钮实现访问外部浏览器并导航到百度首页,当然,我们同样也可以在本应用中嵌套一个网页什么的,但不在本文涉及内容。

一,建立一个空的项目

二,在打开layout中的布局文件和MainActivity.java文件(默认打开)

三,在布局文件中添加一个Button并进行相关设置

四,在onCreate函数内创建一个Button类变量并与Button(布局Button的Id进行捆绑)

五,声明一个Intent 类变量,前提是已经设置好了网址,并用Uri.parse解析网址

六,开始活动

详细代码如下: MainActivity.java

package com.example.administrator.myapplication;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;

public class MainActivity extends AppCompatActivity {

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

        Button button = (Button)findViewById(R.id.button);
        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String baidu = "http://www.baidu.com";  // 网址
                Uri webdress = Uri.parse(baidu);  // 解析网址
                Intent intent = new Intent(Intent.ACTION_VIEW, webdress); // 创建绑定
                startActivity(intent); // 开始活动
            }
        });

activity_main.xml

<?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">

    <TextView
        android:id="@+id/textView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Hello World!"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintVertical_bias="0.25" />

    <Button
        android:id="@+id/button"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginStart="8dp"
        android:layout_marginTop="8dp"
        android:layout_marginEnd="8dp"
        android:layout_marginBottom="8dp"
        android:text="Button"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/textView"
        app:layout_constraintVertical_bias="0.098" />

</android.support.constraint.ConstraintLayout>

其中包的名称可能不同,只需要换成自己的包名即可。

在者 其实页面跳转也很简单,确保自己已经新建了一个MainActivity(作为主界面),在app视图上右键新建一个empty activity作为第二页面,这时,我们就有了两个活动,两个xml布局文件,当然活动,布局的名称是不同的。

这里只简单说一下如何实现跳转,与上文的情况基本一致,详情看代码。

MainActivity.java 这里我引用了我之间做的一个小程序的界面

package com.example.administrator.simplecalculate;

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

public class MainActivity extends AppCompatActivity {

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

        Button simplebtn = (Button) findViewById(R.id.simplebtn);    // 绑定button 的 id
        simplebtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent1 = new Intent(MainActivity.this,simple.class);//要跳转的活动(这个在主函数,所以就是MainActivity.this,simple.class是我另一个activity)
                startActivity(intent1);    // 开始跳转
            }
        });    //基本上就这三个主要部分,当然我们也可以采取更详细但比较麻烦的步骤
                // intent.setAction() intent.setData() 来使用

        Button two_nbtn = (Button) findViewById(R.id.two_nbtn);
        two_nbtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent2 = new Intent(MainActivity.this,two_n.class);
                startActivity(intent2);
            }
        });

        Button three_nbtn = (Button) findViewById(R.id.three_nbtn);
        three_nbtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent3 = new Intent(MainActivity.this,three_n.class);
                startActivity(intent3);
            }
        });

    }
}

布局文件

<?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">

    <Button
        android:id="@+id/simplebtn"
        android:layout_width="200dp"
        android:layout_height="40dp"
        android:layout_marginStart="8dp"
        android:layout_marginTop="8dp"
        android:layout_marginEnd="8dp"
        android:layout_marginBottom="8dp"
        android:text="简单计算"
        app:layout_constraintBottom_toTopOf="@+id/two_nbtn"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintVertical_bias="1.0" />

    <Button
        android:id="@+id/two_nbtn"
        android:layout_width="200dp"
        android:layout_height="43dp"
        android:layout_marginStart="8dp"
        android:layout_marginEnd="8dp"
        android:text="二阶行列式计算"
        app:layout_constraintBottom_toTopOf="@+id/three_nbtn"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent" />

    <Button
        android:id="@+id/three_nbtn"
        android:layout_width="200dp"
        android:layout_height="40dp"
        android:layout_marginStart="8dp"
        android:layout_marginEnd="8dp"
        android:layout_marginBottom="172dp"
        android:text="三阶行列式计算"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent" />

    <TextView
        android:id="@+id/hint"
        android:layout_width="78dp"
        android:layout_height="20dp"
        android:layout_marginStart="8dp"
        android:layout_marginTop="8dp"
        android:layout_marginEnd="8dp"
        android:layout_marginBottom="8dp"
        android:text="选择功能:"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.113"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintVertical_bias="0.115" />
</android.support.constraint.ConstraintLayout>

猜你喜欢

转载自blog.csdn.net/qq_25233621/article/details/82960167
今日推荐