Android开发-打开另一个Activity或打开网页

效果图:

                  


MainActivity:

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);

        findViewById(R.id.startAnotherActivity).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                //启动另一个Activity
                startActivity(new Intent(MainActivity.this, AnotherActivity.class));
            }
        });

        findViewById(R.id.startUri).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                //打开一个网址
                startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse("https://m.baidu.com/")));
            }
        });
    }
}

activity_main:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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">

    <Button
        android:id="@+id/startAnotherActivity"
        android:text="启动另一个Activity"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />

    <Button
        android:id="@+id/startUri"
        android:text="打开一个网址"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />

</LinearLayout>

activity_main:

<?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=".AnotherActivity">

    <TextView
        android:text="这是另一个Activity"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />

</RelativeLayout>




猜你喜欢

转载自blog.csdn.net/lyp_1020k/article/details/79866363