AndroidStudio||活动/网页跳转(内含源代码)

16 篇文章 0 订阅

step by step.

学习视频:【(学完必会)Android studio基础,从入门到精通,学完小白也能会-哔哩哔哩】 https://b23.tv/UaWngO0

目录

1.Activity跳转(显式Intent)

2. app与chrome跳转(隐式Intent

3. 拨打10086跳转


续上篇:

https://blog.csdn.net/weixin_51159944/article/details/129796008?csdn_share_tail=%7B%22type%22%3A%22blog%22%2C%22rType%22%3A%22article%22%2C%22rId%22%3A%22129796008%22%2C%22source%22%3A%22weixin_51159944%22%7D

1.Activity跳转(显式Intent)

@Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Button button2 = (Button) findViewById(R.id.Button_2); //引用按钮触发事件
        button2.setOnClickListener(new View.OnClickListener(){
            @Override
            public void onClick(View v){
                //触发
                Intent intent = new Intent(MainActivity.this,FirstActivity.class);
                startActivity(intent);
            }
        });

 效果:(已跳转)

2. app与chrome跳转(隐式Intent

 intent.setData(Uri.parse("http://www.baidu.com"));

<activity android:name=".FirstActivity"
            android:exported="true">
            <intent-filter>
                <action android:name="com.example.demo1test.ACTION_START"/>
                <category android:name="android.intent.category.DEFAULT"/>
                <category android:name="come.example.demo1test.MY_CATEGORY"/>
            </intent-filter>
        </activity>
public void onClick(View v){
                //触发
                //Intent intent = new Intent(MainActivity.this,FirstActivity.class);
                //Intent intent = new Intent("com.example.activitytest.ACTION_START");
                Intent intent = new Intent(Intent.ACTION_VIEW);
                intent.setData(Uri.parse("http://www.baidu.com"));
                //intent.addCategory("com.example.activitytest.MY_CATEGORY");
                startActivity(intent);
            }

 

3. 拨打10086跳转

"tel:10086" 

//系统拨号
                Intent intent2 = new Intent(Intent.ACTION_VIEW);
                intent2.setData(Uri.parse("tel:10086"));
                startActivity(intent2);

 

 代码存储:

FirstActivity: 

package com.example.demo1;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;

public class FirstActivity extends AppCompatActivity {

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

    }
}

MainActivity: 

package com.example.demo1;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Button button2 = (Button) findViewById(R.id.Button_2); //引用按钮触发事件
        button2.setOnClickListener(new View.OnClickListener(){
            @Override
            public void onClick(View v){
                //触发页面
                //Intent intent = new Intent(MainActivity.this,FirstActivity.class);
                //Intent intent = new Intent("com.example.activitytest.ACTION_START");
                //触发页面
                Intent intent = new Intent(Intent.ACTION_VIEW);
                intent.setData(Uri.parse("http://www.baidu.com"));
                //intent.addCategory("com.example.activitytest.MY_CATEGORY");
                //startActivity(intent);
                //系统拨号
                Intent intent2 = new Intent(Intent.ACTION_VIEW);
                intent2.setData(Uri.parse("tel:10086"));
                startActivity(intent2);
            }
        });
    }
    public boolean onCreateOptionsMenu(Menu menu){
        getMenuInflater().inflate(R.menu.main2,menu);
        return true;
        }
    public boolean onOptionsItemSelected(MenuItem item){
        switch (item.getItemId()){
            case R.id.add_item:
                Toast.makeText(this,"添加了",Toast.LENGTH_SHORT).show();
                break;
            case R.id.remove_item:
                Toast.makeText(this,"删除了",Toast.LENGTH_SHORT).show();
                break;
            default:
        }
        return true;
        }
    }

AndroidManifest: 

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

    <application
        android:allowBackup="true"
        android:dataExtractionRules="@xml/data_extraction_rules"
        android:fullBackupContent="@xml/backup_rules"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:supportsRtl="true"
        android:theme="@style/Theme.Demo1"
        tools:targetApi="31">
        <activity android:name=".FirstActivity"
            android:exported="true">
            <intent-filter>
                <action android:name="com.example.demo1test.ACTION_START"/>
                <category android:name="android.intent.category.DEFAULT"/>
                <category android:name="come.example.demo1test.MY_CATEGORY"/>
            </intent-filter>
        </activity>
        <activity
            android:name=".MainActivity"
            android:exported="true">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>

</manifest>

firstActivity.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=".FirstActivity">
    <Button
        android:text="firstA的按钮"
        android:id="@+id/Button_1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        tools:ignore="MissingConstraints" />



</androidx.constraintlayout.widget.ConstraintLayout>

mainActivity.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">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="欢迎使用"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />
    <Button
        android:text="按钮2"
        android:id="@+id/Button_2"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        tools:ignore="MissingConstraints" />

</androidx.constraintlayout.widget.ConstraintLayout>

  • 3
    点赞
  • 13
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值