Android04_Activity创建

一、Activity的主要作用

 

activity是android应用程序中的一个非常重要的应用程序与用户直接的接口。可以认为是一个控件的容器。

 

二、创建一个Actiity的方法

1.新建一个android project


         

2.创建一个activity的几个要点

1)一个activity就是一个类,并且要继承activity,是一个应用程序组件

 

2)要重写onCreate方法——当一个activity第一次运行的时候应用程序框架就会调用onCreate方法

 

3)每一个activity都会在manifest.xml中配置

 

4)向每一个activity中添加控件——layout/main.xml是布局文件,用来控制activity中有哪些控件及位置

 

    Activity01.java

 

package com.android.activity;

import android.app.Activity;
import android.os.Bundle;
import android.widget.Button;
import android.widget.TextView;

/**
 * @author Allorry Zhang
 */
public class Activity01 extends Activity {
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        //调用父类的onCreate方法
    	super.onCreate(savedInstanceState);
    	//读取main.xml布局配置文件
        setContentView(R.layout.main);
        
        //得到控件 findViewById返回的是View类型,它是所有控件的父类
        TextView textView = (TextView)findViewById(R.id.myTextView);
        textView.setText("我的第一个TextView!"); //不建议使用这种方法,最好用@string/...,使用strings.xml
        Button button = (Button)findViewById(R.id.myButton);
        button.setText("我的第一个Button");
    }
}

 

三、在AndroidManifest.xml文件当中注册应用Activity的方法

每一个activity都应该在manifest.xml中配置。

 

androidmanifest.xml

 

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
      package="com.android.activity"
      android:versionCode="1"
      android:versionName="1.0">
    <uses-sdk android:minSdkVersion="10" />
    <!-- 以下内容为向manifest中注册activity-->
    <application android:icon="@drawable/icon" android:label="@string/app_name">
        <activity android:name=".Activity01"
                  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>
</manifest>
 

四、在Activity当中添加控件的方法

 

main.xml

 

<?xml version="1.0" encoding="utf-8"?>
<!-- linearLayout表示是线性布局,Orientation为Vertical表示是从上到下的线性 -->
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    >
    <!-- android:id 用来设置当前这个控件的id,以便可以在activity中使用findViewById轻松的访问到 
    	   android:layout_width控件宽度,fill_parent表示填满它的父控件
    	   android:layout_height控件高度,wrap_content表示高度是内容的高度
    	   android:text直接设置控件的值,但直接写在这里没多大意义,一般都是另外设置的
    -->
	<TextView  
		android:id="@+id/myTextView"
	    android:layout_width="fill_parent" 
	    android:layout_height="wrap_content" 
	    />
	 <Button 
	 	android:id="@+id/myButton"
	 	android:layout_width="fill_parent"
	 	android:layout_height="wrap_content"
	 />
</LinearLayout>
 

运行结果:


       

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值