Android开发 - Activity初步

Activity初步

1.      Activity的主要作用

@学习记录:

1) Android应用中负责与用户交互的组件

2) 实际上是Window容器,用来存放各种控件

 

2.      创建一个Activity的方法

@学习记录:

1) 创建一个应用程序,样例:Activity01

2) 创建好一个应用程序后就创建好了一个Activity,不过这个Activity没有实际意义

代码清单:

//Activity01Activity.java
package com.Activity;

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

public class Activity01Activity extends Activity {
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);//调用父类的OnCreate
        setContentView(R.layout.main);	//引用布局文件   
    }
}


 

3)创建Activity的要点

package com.Activity;

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


/**
 * 创建Activity要点
 * 1.一个Activity就是一个类,并且这个类要继承Activity
 * 2.需要复写OCreate方法
 * 3.每一个Activity都需要在AndroidManifest.xml文件中进行配置
 * 4.为Activity添加必要的控件
 * @author Administrator
 *
 */
public class Activity01Activity extends Activity {
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);//调用父类的OnCreate
        setContentView(R.layout.main);	//引用布局文件
        TextView myTextView = (TextView)findViewById(R.id.myTestView);
        Button myButton = (Button)findViewById(R.id.myButton);
        myTextView.setText("myTextView");
        myButton.setText("myButton");
        
    }
}


3.      AndroidManifest.xml文件当中注册应用Activity的方法

@学习记录:

1) 首先做main.xml布局文件中注册id

修改main.xml源代码

<?xml version="1.0" encoding="utf-8"?>

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"

    android:layout_width="fill_parent"

    android:layout_height="fill_parent"

    android:orientation="vertical" >

 

//添加文本控件

    <TextView

         android:id="@+id/myTestView"       //注册文本控件的id

        android:layout_width="fill_parent"

        android:layout_height="wrap_content"

    />

       //添加按钮控件

    <Button

        android:id="@+id/myButton"              //注册按钮控件的id

        android:layout_width="fill_parent"

              android:layout_height="wrap_content"

              />

</LinearLayout>


 

 

2)R.java资源文件中会自动生成对应的id

代码如下:

/* AUTO-GENERATED FILE.  DO NOT MODIFY.

 *

 * This class was automatically generated by the

 * aapt tool from the resource data it found.  It

 * should not be modified by hand.

 */

 

package com.Activity;

 

public final class R {

    public static final class attr {

    }

    public static final class drawable {

        public static final int ic_launcher=0x7f020000;

    }

    public static final class id {                  //自动生成的id内部类

        public static final int myButton=0x7f050001;

        public static final int myTestView=0x7f050000;

    }

    public static final class layout {

        public static final int main=0x7f030000;

    }

    public static final class string {

        public static final int app_name=0x7f040001;

        public static final int hello=0x7f040000;

    }

}


 

 

3) 然后就是通过在Activity01Activity.java文件中调用findViewByid方法引用

因为findViewByid方法返回值是View类型,所以要强制转换为相应控件的类型

如:

TextViewmyTextView = (TextView)findViewById(R.id.myTestView);

Button myButton= (Button)findViewById(R.id.myButton);

 

4.Activity当中添加控件的方法

@学习记录:

 关于在Activity当中添加控件的方法在第三点已经说明:

//添加文本控件

    <TextView

         android:id="@+id/myTestView"       //注册文本控件的id

        android:layout_width="fill_parent"

        android:layout_height="wrap_content"

    />

       //添加按钮控件

    <Button

        android:id="@+id/myButton"              //注册按钮控件的id

        android:layout_width="fill_parent"

              android:layout_height="wrap_content"

              />


 

 

 

完成了以上的4步,一个简单的Activity就创建完成了。运行实例:

  • 2
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

小巫技术博客

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值