The Main Function of Activity and its Basic Usage
本文由发表于6年前 | Android | 暂无评论 |  被围观 3,527 views+

What is Activity?

The application's user interface.
A container for all controls.

An activity is a single, focused thing that the user can do. Almost all activities interact with the user, so the Activity class takes care of creating a window for you in which you can place your UI withsetContentView(View). While activities are often presented to the user as full-screen windows, they can also be used in other ways: as floating windows (via a theme with windowIsFloating set) or embedded inside of another activity (using ActivityGroup).

To create an Activity class ,do the following step:
Step 1:Create a class,and inherits from Activity.
Step 2: Override some inherited methods like onCreate() and onPause().
Step 3:Register each Activity class in the AndroidMainFest.xml.
Step 4:Make a layout.xml file,general each Activity class require one layout.xml.
Step 5:Add necessary controls into the layout.xml.
The below is an example:


ActivityTest.class:

public class ActivityTest extends Activity {
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        TextView textView1 = (TextView)findViewById(R.id.myTextView);
        textView1.setText("Hello world!!!");

    }
}
AndroidManifest.xml:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
      package="com.itzhai.activity"
      android:versionCode="1"
      android:versionName="1.0">
    <application android:icon="@drawable/icon" android:label="@string/app_name">
        <activity android:name=".ActivityTest"
                  android:label="@string/app_name">
              <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>
    <uses-sdk android:minSdkVersion="4" />
</manifest>


mail.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    >
<TextView
	android:id="@+id/myTextView"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    />
</LinearLayout>


Using Control in the ActivityTest:

setContentView(R.layout.main);
TextView textView1 = (TextView)findViewById(R.id.myTextView);
textView1.setText("Hello world!!!");
除了文章中有特别说明,均为IT宅原创文章,转载请以链接形式注明出处。
本文链接:http://www.itzhai.com/the-main-function-of-activity-and-its-basic-usage.html
关键字: ,
arthinking Java技术交流群:280755654,入门群:428693174 more
分享到:
 
2011 7/12
文章评论
    没有评论
给我留言

有人回复时邮件通知我
Android的相关文章
随机文章 本月热门 热评
1 Linux下sed,awk,grep,cut,find等常用命令介绍 2011/6/11
2 Struts2 Hibernate Spring 整合的基本流程和步骤及其相关配置细节 2011/7/23
3 JSP的运行机制与原理 2011/6/17
4 Java基础笔记 – 内部类 静态内部类 成员内部类 局部内部类 匿名内部类 2011/11/3
5 JVM笔记 – 自动内存管理机制(调优案例分析与实战) 2014/11/30
6 org.apache.el.parser.ParseException JSTL中EL表达式错误提示 2011/7/30
友情推荐 更多
破博客 文官洗碗安天下,武将打怪定乾坤。多么美好的年代,思之令人泪落。
Mr.5's Life 白天是一名程序员,晚上就是个有抱负的探索者
行知-追寻技术之美 关注大数据,分布式系统
我爱编程 编程成长轨迹
Cynthia's Blog 学习笔记 知识总结 思考感悟
 
猜您喜欢
欢迎关注我的公众号 IT宅
关于IT宅 文章归档

IT宅中的文章除了标题注明转载或有特别说明的文章,均为IT宅的技术知识总结,学习笔记或随笔。如果喜欢,请使用文章下面提供的分享组件。转载请注明出处并加入文章的原链接。 感谢大家的支持。

联系我们:admin@itzhai.com

Theme by arthinking. Copyright © 2011-2015 IT宅.com 保留所有权利.