Android之Activity初识

本文使用版本 Adndroid2.2

本文主要介绍Activity的初步知识,上一篇文章我们也用到了Activity,也简单介绍了Activity ,今天我们更加深入的学习,Activity是Android非常重要的组件之一。

Activity 可以理解为 一个界面,和网页那样的一个界面 。

  • Activity的启动流程
  • Activity与布局文件之间的关系
  • 在Activity中获取代表控件对象

首先我们看张图

这个是Android启动一个应用程序的基本流程,

Android操作系统 要启动一个应用程序,首先会到AndroidManifest.xml文件中读取默认启动Activity,找到默认启动的Activity之后生成对象,生成完对象之后会调用当前Activity的onCreate方法,在onCreate方法中读取xml布局文件,显示什么内容由布局文件决定。这些事情都是由Android操作系统来启动。Android启动一个Activity的基本流程。

 看到这里你也许会问,布局文件是什么。先别急,马上 就知道了。先看我们上一篇的Helloworld运行结果

图片所对应的代码

RelativeLayout:相对布局,

Android中 有5种布局,相对布局是用的最多的一种,以后是详细介绍。

TextView:文本视图,这个不陌生吧;一个控件显示的 Hello world!就是Textview的功劳。android:layout_width:Textview的宽度,android:layout_height:Textview的高度,android:text:Textview显示的内容

这样也太简单了吧;我们设置Helloworld的背景、字体大小看看,Demo用上一篇中的用到的;

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context=".MainActivity" >

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textSize="20dp"
        android:background="#ff00ff"
        android:text="@string/hello_world" />

</RelativeLayout>

我们把字体大小改为 20dp;字体颜色:#ff00ff; 运行看看结果吧;注意:布局文件是 xml格式,如果不懂xml的 请自己去查看资料

正如我们所想的那样。字体,颜色都改变了。布局文件中包括各种控件。布局文件写的什么内容,Activity将显示什么内容。

 在看看我们的src下面的java文件,我们可没有动,

现在我们打开看 看。

package com.zhoucj.helloworlddemo;

import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;

public class MainActivity extends Activity
{

    @Override
    protected void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        //用来显示布局文件
        setContentView(R.layout.activity_main);//这里关联那个布局文件,则显示那个文件的内容
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu)
    {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }

}

 如果我们要动态改变Activity中的内容 ,而不是写死在布局文件里面。这个时候我们就要编写java代码了。

package com.zhoucj.helloworlddemo;

import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.widget.TextView;

public class MainActivity extends Activity
{

    @Override
    protected void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        //用来显示布局文件
        setContentView(R.layout.activity_main);//这里关联那个布局文件,则显示那个文件的内容
        
        //通过 findViewById()找到 布局文件中Textview对象
        TextView textView=(TextView)findViewById(R.id.textview1);
        //设置Textview所显示的内容
        textView.setText("大家好!");
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu)
    {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }

}
java代码
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context=".MainActivity" >

    <TextView
        android:id="@+id/textview1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textSize="20dp"
        android:background="#ff00ff"
        android:text="@string/hello_world" />

</RelativeLayout>

 布局文件,多加了一个 id

运行结果如下:

这就是动态改变Activity中显示的内容,布局文件中显示的是:Helloworld ,而运行结果却是:大家好!;通过java代码动态改变Activity中显示的内容,是不是很简单。

作者:zhoucj

出处:http://www.cnblogs.com/zhoujian315/archive/2013/06/08/3127193.html

原创作品,欢迎转载,转载是请说明出处

原文地址:https://www.cnblogs.com/zhoujian315/p/3127193.html