目录
问题描述
Button 1: Toast text : peace and love
Button 2: 跳转到打电话页面,并显示电话号码520(隐式跳转)
Button 3: 跳转到浏览器页面,并基于data提供的浏览器地址CCTV节目官网_央视网 直接访问网页
Button 4: Activity 1 jump to Activity 2 显示跳转
Button 5: Activity 2 jump to Activity 2 隐式跳转 (需要在Activity 2中定义intent filter 跳转动作)
代码:
MainActivity.java
package com.example.task1;
import androidx.appcompat.app.AppCompatActivity;
import android.annotation.SuppressLint;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;
public class MainActivity extends AppCompatActivity {
Button b1,b2,b3,b4,b5;
@SuppressLint("MissingInflatedId")
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
b1 = findViewById(R.id.bt1);
b2 = findViewById(R.id.bt2);
b3 = findViewById(R.id.bt3);
b4 = findViewById(R.id.bt4);
b5 = findViewById(R.id.bt5);
// Mylistener listener = new Mylistener();
listener lst = new listener();
b1.setOnClickListener(lst);
b2.setOnClickListener(lst);
b3.setOnClickListener(lst);
b4.setOnClickListener(lst);
b5.setOnClickListener(lst);
}
class listener implements View
.OnClickListener{
@Override
public void onClick(View view){
if(view.getId() == R.id.bt1)
Toast.makeText(MainActivity.this, "peace and love", Toast.LENGTH_SHORT).show();
else if(view.getId() == R.id.bt2){
Intent diatil = new Intent(Intent.ACTION_DIAL, Uri.parse("tel:520"));
startActivity(diatil);}
//打电话,隐式跳转
else if (view.getId() == R.id.bt3) {
Intent diatil = new Intent(Intent.ACTION_VIEW, Uri.parse("https://tv.cctv.com"));
startActivity(diatil);}
//跳转到指定浏览器页面
else if (view.getId() == R.id.bt4) {
Intent it = new Intent(MainActivity.this, MainActivity2.class);
startActivity(it);}
//显式跳转
else if (view.getId() == R.id.bt5) {
Intent implictAction = new Intent("com.example.task1.MainActivity2");
//Intent implictAction = new Intent("com.example.button.intent.action.activity2");
startActivity(implictAction);
}
}
}
}
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<androidx.appcompat.widget.LinearLayoutCompat 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"
android:orientation="vertical"
tools:context=".MainActivity">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Hello World!"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<!-- 这是注释的用法 -->
<Button
android:id = "@+id/bt1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text = "Button 1"
tools:ignore="MissingConstraints"></Button>
<Button
android:id = "@+id/bt2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text = "Button 2"
tools:ignore="MissingConstraints"></Button>
<Button
android:id = "@+id/bt3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text = "Button 3"
tools:ignore="MissingConstraints"></Button>
<Button
android:id = "@+id/bt4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text = "Button 4"
tools:ignore="MissingConstraints"></Button>
<Button
android:id = "@+id/bt5"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text = "Button 5"
tools:ignore="MissingConstraints"></Button>
</androidx.appcompat.widget.LinearLayoutCompat>
MainActivity2.java
package com.example.task1;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
public class MainActivity2 extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main2);
}
activity_main2.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=".MainActivity2">
<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:textSize="30dp"
android:text = "Hello world"
tools:ignore="MissingConstraints"/>
</androidx.constraintlayout.widget.ConstraintLayout>
Androidmainfest.java
<?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:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/Theme.Task1"
tools:targetApi="31">
<activity
android:name=".MainActivity"
android:exported="true" />
<activity
android:name=".MainActivity2"
android:exported="true">
<intent-filter>
<action android:name="com.example.task1.MainActivity2" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
</application>
</manifest>
运行结果