4.24

所花时间(包括上课):1.5

打码量(行):150

博客量(篇):1

了解到知识点:学习Action和data属性

 

package com.example.myapp;

 

import android.content.Intent;

import android.net.Uri;

import android.os.Bundle;

import android.view.View;

import android.widget.Button;

import androidx.appcompat.app.AppCompatActivity;

 

public class MainActivity extends AppCompatActivity {

 

    @Override

    protected void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_main);

 

        // 获取按钮视图

        Button actionViewButton = findViewById(R.id.action_view_button);

        Button actionDialButton = findViewById(R.id.action_dial_button);

 

        // 设置ACTION_VIEW按钮点击监听器

        actionViewButton.setOnClickListener(new View.OnClickListener() {

            @Override

            public void onClick(View v) {

                // 创建隐式Intent,指定动作和数据

                Intent intent = new Intent(Intent.ACTION_VIEW);

                intent.setData(Uri.parse("https://www.example.com"));

                // 启动目标Activity

                startActivity(intent);

            }

        });

 

        // 设置ACTION_DIAL按钮点击监听器

        actionDialButton.setOnClickListener(new View.OnClickListener() {

            @Override

            public void onClick(View v) {

                // 创建隐式Intent,指定动作和数据

                Intent intent = new Intent(Intent.ACTION_DIAL);

                intent.setData(Uri.parse("tel:1234567890"));

                // 启动目标Activity

                startActivity(intent);

            }

        });

    }

}

<!-- activity_main.xml -->

<?xml version="1.0" encoding="utf-8"?>

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"

    android:layout_width="match_parent"

    android:layout_height="match_parent"

    android:padding="16dp">

 

    <!-- ACTION_VIEW 按钮 -->

    <Button

        android:id="@+id/action_view_button"

        android:layout_width="wrap_content"

        android:layout_height="wrap_content"

        android:text="打开网页"

        android:layout_centerHorizontal="true"

        android:layout_marginTop="50dp" />

 

    <!-- ACTION_DIAL 按钮 -->

    <Button

        android:id="@+id/action_dial_button"

        android:layout_width="wrap_content"

        android:layout_height="wrap_content"

        android:text="拨打电话"

        android:layout_centerHorizontal="true"

        android:layout_below="@id/action_view_button"

        android:layout_marginTop="20dp" />

</RelativeLayout>

posted @ 2024-04-24 20:43  赵千万  阅读(1)  评论(0编辑  收藏  举报