layout文件 很简单
给Button设置一个点击事件就可以了
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Hello World!"
android:textSize="50dp"
android:onClick="tobaidu" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="打开相机"
android:textSize="50dp"
android:onClick="tocamera" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="打开相册"
android:textSize="50dp"
android:onClick="tophoto" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="打电话"
android:textSize="50dp"
android:onClick="tocall" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="发短信"
android:textSize="50dp"
android:onClick="tomsg" />
</LinearLayout>
然后就是在Java代码中稍微设置一下就好了
package com.example.secondprogram;
import androidx.activity.result.ActivityResult;
import androidx.activity.result.ActivityResultCallback;
import androidx.activity.result.ActivityResultLauncher;
import androidx.activity.result.contract.ActivityResultContracts;
import androidx.appcompat.app.AppCompatActivity;
import android.content.Intent;
import android.content.SharedPreferences;
import android.net.Uri;
import android.os.Bundle;
import android.provider.Settings;
import android.view.View;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
//去浏览器的指定网址页面
public void tobaidu(View view) {
Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setData(Uri.parse("https://fanyi.baidu.com/?" +
"aldtype=16047#en/zh/Features%20that%20apply%" +
"20to%20distribution%20by%20the%20bundle"));
startActivity(intent);
}
//系统自带相机
public void tocamera(View view) {
Intent intent = new Intent("android.media.action.IMAGE_CAPTURE");
startActivity(intent);
}
//系统自带相册
public void tophoto(View view) {
Intent intent=new Intent("android.intent.action.PICK");
intent.setType("image/*");
startActivity(intent);
}
//系统自带电话,且打开时候110已经帮你按好了
public void tocall(View view) {
Intent intent = new Intent(Intent.ACTION_DIAL);
intent.setData(Uri.parse("tel:110"));
startActivity(intent);
}
//打开短信,且收件人和消息已经帮你输入好了,点击发送即可
public void tomsg(View view) {
Intent intent = new Intent(Intent.ACTION_SENDTO);
intent.setData(Uri.parse("smsto:110"));
intent.putExtra("ssss", "收下");
startActivity(intent);
//此处是一个存储工具,可以忽略,SharedPreferences是本地持久存储类
//SharedPreferences sharedPreferences =
//getSharedPreferences("ss", MODE_PRIVATE);
//SharedPreferences.Editor editor = sharedPreferences.edit();
}
}