Android基础之Acticity初步了解

一:Acticity初步了解

Acticity程序是andriod的基本组成单元。

1.       andriod程序的开发

2.       掌握Android项目的各个主要组成部分及作用

3.       掌握andrioid程序的主要开发模式

4.       掌握activityandroidManifext.xml文件的配置

内容:

1.1Android项目基础

 

 

在本程序之中,先观察java程序类

package com.example.wwwww;

 

import android.app.Activity;

import android.os.Bundle;

 

public class MyActivity extends Activity {

    /**

     * Called when the activity is first created.

     */

    @Override

    public void onCreate(Bundle savedInstanceState) {

 

        super.onCreate(savedInstanceState);

        setContentView(R.layout.main);

    }

}

MyActivityActivity的子类,可以参考API

ActivityContext类的子类,Context表示的是一个上下文,指的就是Android上下文

 

Activity就是一个基本的组成单元,而Android项目之中会包含多个activity程序,通过这些程序可以完成一个个的界面显示及事件处理

package com.example.wwwww;

 

import android.app.Activity;

import android.os.Bundle;

 

public class MyActivity extends Activity {

  

    @Override

    public void onCreate(Bundle savedInstanceState) {

 

        super.onCreate(savedInstanceState);//生命周期方法

        setContentView(R.layout.main);//设置要使用的布局管理器

    }

}

项目中的文件夹的作用进行解释:

Src :里面存放的是activity程序,或者是以后的其他组件,在此文件夹之中建立类的时候一定要注意,包名称不能是一级;

Gen:此文件夹是自动生成的用户不可以修改,即:保存的是所有ID

 R.java:保存着所有的资源程序,次文件自动编写。在日后的程序之中,程序不会直接与所有的配置文件操作,都是通过映射名称找到的,这一点非常符合于java中的key = value 的操作形式;

 Android2.3.3:此为现在使用的Android的开发版本;

Assests:存放一些大型的资源文件,例如:图片、音乐、文字等等,但是一般不用;

Res:存放一些资源文件使用,例如:图片、音乐、文字等等;

 drawable-hdpi :存放图片的文件夹,存放高清图片 480x800480x854

drawable-ldpi  :存放图片的文件夹,存放低分辨率的图片240x320

drawable-mdpi :存放图片的文件夹,存放中等分辨率的图片320x480

drawable-xhdpi   存放图片的文件夹,存放的分辨率图片为至少是960x720

layout:存放所有的布局管理器的配置信息;

values:存放所有的文字信息,这一点的设计也非常符合于MVC设计模式;

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

<resources>

    <string name="app_name">wwwww</string>

</resources>

此名称是在手机的应用程序栏中显示的提示信息。即:以后所编写的所有项目中,都应该在strings.xml文件之中编写相应的提示信息,但是考虑到本书的篇幅问题,以及大家的理解问题,所以很多时候会直接在需要的地方编写文字,但是如果是开发的时候一定要将它编写在strings.xml文件之中,而在strings.xml文件中编写的提示信息,都会自动的在R.java文件中自动注册。

补充:关于values文件夹的内容:

  随着日后的学习,在此文件夹之中会建立许多的子文件,除了strings.xml文件这个基本的内容之外,还可能存在其他的信息:arrays.xmlcolor.xmlstyles.xml,这些名称都是约定俗称的;

以上的几个文件夹都是在建立项目的时候自动的在res文件中出现的,除了这几个文件夹之外,在res文件夹之中以后也会增加如下的几个子文件夹:

  Res\raw:存放一些原生文件例如:视频、音乐;

  Res\xml:存放一些xml文件配置信息;

  Rex\anim:用于进行动画效果配置的文件夹;

AndroidManifest.xml 文件是整个Android项目之中最重要的文件

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

<manifest   ->对应着的是根元素

 xmlns:android=http://schemas.android.com/apk/res/android  ->对应使用的schema

          package="com.example.wwwww" ->表示程序所在的包名称

          android:versionCode="1"   -应用程序的版本号

          android:versionName="1.0">  -显示给用户的名称

    <uses-sdk android:minSdkVersion="18"/>  -此为应用程序所对应的最低SDK版本

<application   -配置所有的应用程序

android:label="@string/app_name"  表示应用程序提示的信息

android:icon="@drawable/ic_launcher">  使用图标

        <activity  表示配置一个activity程序,如果有需要可以编写多个此节点

 android:name="MyActivity"  ->对应的activity程序的名称

                  android:label="@string/app_name"> 表示应用程序提示的信息,使用的strings.xml

            <intent-filter>  表示的是过滤器

                <action android:name="android.intent.action.MAIN"/>

                <category android:name="android.intent.category.LAUNCHER"/>

            </intent-filter>

        </activity>

</application>

<uses-oermission   / >  表示应用的授权

</manifest>

但是一般在基础学习的前半部分,此文件基本上不用做太大的修改,而唯一修改最多的地方就是main.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:layout_width="fill_parent"

            android:layout_height="wrap_content"

            android:text="Hello World,  binbinbin MyActivity"

            />

 

    <TextView   此为文本显示组件,显示提示信息的

            android:layout_width="fill_parent"  指的是此组件的宽度为当前屏幕的宽度

            android:layout_height="wrap_content" 组件的高度为文字的高度

            android:text="Hello World,  xuhui MyActivity" 组件的默认显示文字,此时为strings.xml

            /> 

</LinearLayout>

 

以后的所有组件都要在此处进行配置,或者是通过程序代码完成

 

 

1.2扩充的第一个Android项目

 在之前所有使用的项目区都是由系统为用户自动建立完成的,那么用户现在肯定要根据自己的需要对项目进行修改及扩充。

Activity和布局文件之间的联系非常的紧密,即:可以通过activity取得组件(但是需要配置ID),也可以使用activity通过程序动态生成组件。

快捷键:ALT+/ 进行自动提示

 

<TextView

            android:id="@+id/mytext"

            android:layout_width="fill_parent"

            android:layout_height="wrap_content"

            android:text="Hello World,  xuhui MyActivity"

            />

    <Button

            android:id="@+id/mytext"

            android:layout_width="fill_parent"

            android:layout_height="wrap_content"/>

现在配置了两个新的组件,这两个组件都存在了ID,而在以后的activity程序之中会直接使用此组件进行操作,而且一旦定义了组件之后,所有的内容也会自动的在R.java文件中生成一个引用的ID

下面最有意思的地方在于可以直接通过activity操作。

使用findViewById()方法可以根据R.java中定义的ID的数字取得相应的组件

package com.example.wwwww;

 

import android.app.Activity;

import android.os.Bundle;

import android.widget.Button;

import android.widget.TextView;

 

public class MyActivity extends Activity {

 

    @Override

    public void onCreate(Bundle savedInstanceState) {

 

        super.onCreate(savedInstanceState);//生命周期方法

        setContentView(R.layout.main);//设置要使用的布局管理器

        TextView text = (TextView)super.findViewById(R.id.mytext);  //取得TextView组件

        text.setText("石家庄邮电职业技术学院"); //设置显示文字

        Button but = (Button) super.findViewById(R.id.mytext); //取得组件

        but.setText("按我,不过没有。");

    }

}

MVC设计模式中,应该将所有的提示信息在配置文件之中完成,以上是将具体的文字信息直接写在显示的组件之中,下面进行部分的修改。

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

<resources>

    <string name="app_name">徐辉</string>

    <string name="info">石家庄邮电职业技术学院</string>

    <string name="msg">按我,不过没有</string>

</resources>

肯定现在在R.java文件中也会自动的进行信息文字的注册ID

现在如果要进行显示,可以直接修改activity程序:

package com.example.wwwww;

 

import android.app.Activity;

import android.os.Bundle;

import android.widget.Button;

import android.widget.TextView;

 

public class MyActivity extends Activity {

 

    @Override

    public void onCreate(Bundle savedInstanceState) {

 

        super.onCreate(savedInstanceState);//生命周期方法

        setContentView(R.layout.main);//设置要使用的布局管理器

        TextView text = (TextView)super.findViewById(R.id.mytext);  //取得TextView组件

        text.setText(R.string.info); //设置显示文字

        Button but = (Button) super.findViewById(R.id.mytext); //取得组件

        but.setText(R.string.msg);

    }

}

 

<?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:layout_width="fill_parent"

            android:layout_height="wrap_content"

            android:text="@string/app_name"

            />

 

    <TextView

            android:id="@+id/mytext"

            android:layout_width="fill_parent"

            android:layout_height="wrap_content"

            android:text="@string/info"

            />

    <Button

            android:id="@+id/mytext"

            android:layout_width="fill_parent"

            android:layout_height="wrap_content"

            android:text="@string/msg"/>

   

</LinearLayout>

 

以上的操作形式就是直接通过配置文件完成的。

1.3通过程序动态生成

 通过上面的程序应用可以发现,在整个的项目之中,所有的组件都可以通过一个类来表示所以那个时候就可以直接利用类的方式定义组件。

package com.example.wwwww;

 

import android.app.Activity;

import android.os.Bundle;

import android.widget.Button;

import android.widget.TextView;

 

public class MyActivity extends Activity {

 

    @Override

    public void onCreate(Bundle savedInstanceState) {

 

        super.onCreate(savedInstanceState);//生命周期方法

        TextView text = new TextView(this);//要根据上下文(Context)创建组件

        text.setText(super.getString(R.string.info));//通过strings.xml文件设置文字

        setContentView(text);//设置要使用的布局管理器

 

    }

}

 

相信此时很多的同学应该已经可以发现一个问题了,现在的配置只能配置一个组件,如果有多个呢?

package com.example.wwwww;

 

import android.app.Activity;

import android.os.Bundle;

import android.widget.Button;

import android.widget.LinearLayout;

import android.widget.TextView;

 

public class MyActivity extends Activity {

 

    @Override

    public void onCreate(Bundle savedInstanceState) {

 

        super.onCreate(savedInstanceState);//生命周期方法

        LinearLayout layout = new LinearLayout(this); //定义布局管理器

        TextView text = new TextView(this);//要根据上下文(Context)创建组件

        text.setText(super.getString(R.string.info));//通过strings.xml文件设置文字

        Button but = new Button(this); //定义按钮

        but.setText(super.getString(R.string.msg));//配置组件文字

        layout.addView(text);//向布局管理器中增加文本组件

        layout.addView(but);//向布局管理器中增加按钮组件

        setContentView(layout);//设置要使用的布局管理器

 

 

    }

}

 

 

如果要想设置方向就需要使用LinearLayout类中的方法

package com.example.wwwww;

 

import android.app.Activity;

import android.os.Bundle;

import android.widget.Button;

import android.widget.LinearLayout;

import android.widget.TextView;

 

public class MyActivity extends Activity {

 

    @Override

    public void onCreate(Bundle savedInstanceState) {

 

        super.onCreate(savedInstanceState);//生命周期方法

        LinearLayout layout = new LinearLayout(this); //定义布局管理器

        layout.setOrientation(LinearLayout.VERTICAL);//所有组件垂直摆放

        TextView text = new TextView(this);//要根据上下文(Context)创建组件

        text.setText(super.getString(R.string.info));//通过strings.xml文件设置文字

        Button but = new Button(this); //定义按钮

        but.setText(super.getString(R.string.msg));//配置组件文字

        layout.addView(text);//向布局管理器中增加文本组件

        layout.addView(but);//向布局管理器中增加按钮组件

        setContentView(layout);//设置要使用的布局管理器

 

 

    }

}

 全部字母大写就是定义了常量。

 

 

小结:

Android项目由若干个activity程序组成,每个activity都是一个java类;

一个Android项目中所有用到的资源都保存在res文件夹中;

Android中的组件需要在布局管理器中进行配置,之后在Activity程序中可以使用findViewByld()方法查找并进行控制;

在布局管理器中定义的每一组件都有其对应的操作类,用户可以直接实例化这些类的对象进行组件的定义显示;

标准的Android项目,所有的文字显示信息应该保存在strings.xml文件中保存。

 

由于受到本书篇幅的限制,以后所讲解的组件之中,文字信息就直接编写了,但是如果是开发之中必须在strings.xml文件中编写。

 

 

 

 

 

 

 

 

猜你喜欢

转载自h389301776.iteye.com/blog/2199506