step by step.
学习视频:【(学完必会)Android studio基础,从入门到精通,学完小白也能会-哔哩哔哩】 https://b23.tv/UaWngO0
目录
续上篇:
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>