美文网首页
001.认识 Android 中的 Activity 组件

001.认识 Android 中的 Activity 组件

作者: 胖先森 | 来源:发表于2017-05-15 22:57 被阅读0次

1.Activity 是什么?

个人理解Activity就是一个界面而已,类似于我们学习的HTML页面

新建一个Android项目


入门示例.gif

可以通过F4查看源码

2.Activity绑定自定义视图

加载自定义视图.gif
public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        //setContentView(R.layout.activity_main);
        //调用自定义的Activity
        setContentView(R.layout.my_layout);
    }
}

布局文件:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
              android:orientation="vertical"
              android:layout_width="match_parent"
              android:layout_height="match_parent">

    <Button
        android:text="Button"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/button"/>

    <Button
        android:text="Button"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/button2"/>
</LinearLayout>

3.如何启动另一个Activity

调用另一个Activity.gif
package com.hanpang.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);
        //调用自定义的Activity
        //setContentView(R.layout.my_layout);

        //激活另一个Activity
        findViewById(R.id.start).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                //激活
                startActivity(new Intent(MainActivity.this,AnotherActivity.class));
                //尝试打开一个浏览器 内置的Activity
                //startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse("http://www.baidu.com")));
            }
        });

    }
}

打开浏览器

内置浏览器.gif

相关文章

网友评论

      本文标题:001.认识 Android 中的 Activity 组件

      本文链接:https://www.haomeiwen.com/subject/gwhkxxtx.html