目標:完成相應Activity的跳轉。
1.通過顯式/隱式跳轉到下一個界面
2.實現打電話、發短信、打開瀏覽器功能
效果:
Activity的生命週期如下圖
1.目錄
2.activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
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:id="@+id/head1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="18sp"
android:text="自定義跳轉!"
/>
<Button
android:id="@+id/button_explicit"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@+id/head1"
android:layout_margin="10dp"
android:onClick="click_explicit"
android:text="顯式跳轉"/>
<Button
android:id="@+id/button_implicit"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@+id/button_explicit"
android:layout_margin="5dp"
android:onClick="click_implicit"
android:text="隱式跳轉"/>
<TextView
android:id="@+id/head2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@+id/button_implicit"
android:layout_marginTop="10dp"
android:textSize="18sp"
android:text="系統工具跳轉!"
/>
<Button
android:id="@+id/button_call"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@+id/head2"
android:layout_margin="10dp"
android:onClick="click_call"
android:text="打電話"/>
<Button
android:id="@+id/button_browser"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@+id/button_call"
android:layout_margin="5dp"
android:onClick="click_browser"
android:text="打開瀏覽器"/>
<Button
android:id="@+id/button_message"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@+id/button_browser"
android:layout_margin="10dp"
android:onClick="click_message"
android:text="發短信"/>
</RelativeLayout>
2.MainActivity.java
package cn.qjnu.wxf;
import android.content.Intent;
import android.net.Uri;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
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 click_explicit(View view) {
Intent intent = new Intent(this,secondActivity.class);
startActivity(intent);
}
//隱式跳轉
public void click_implicit(View view) {
Intent intent = new Intent();
intent.setAction("cn.qjnu.activeity.THIRD_ACTIVITY");
startActivity(intent);
}
//打電話
public void click_call(View view) {
Intent intent = new Intent();
intent.setAction(Intent.ACTION_DIAL);
intent.setData(Uri.parse("tel:10086"));
startActivity(intent);
}
//瀏覽器
public void click_browser(View view) {
Intent intent = new Intent();
intent.setAction(Intent.ACTION_VIEW);
intent.setData(Uri.parse("https://www.baidu.com"));
startActivity(intent);
}
//發短信
public void click_message(View view) {
Intent intent = new Intent();
intent.setAction(Intent.ACTION_SENDTO);
intent.setData(Uri.parse("smsto:10086"));
intent.putExtra("sms_body","The SMS text");
startActivity(intent);
}
}
3.AndroidManifest.xml
4.其他頁面可以不做任何修改,但是爲了能夠辨別不同的頁面,你可以自己添加一下文字標識。
最後對以上的部分代碼解釋:
如果有問題留言我們一起討論!