首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 移动开发 > Android >

Android开发学习札记之一

2012-08-01 
Android开发学习笔记之一学习android也有一段时间了,之前由于各种原因停了一段时间,回来再继续学的时候发

Android开发学习笔记之一
   学习android也有一段时间了,之前由于各种原因停了一段时间,回来再继续学的时候发现有些东西就生疏了,所以决定把自己学习的历程纪录下来,一方面加深理解,一方面便于以后再回来看。

   如下图就是android的系统构架  

   从图中我们可以看到android系统是依靠linux提供的核心服务,这包括安全、内存管理、进程管理、网络、硬件驱动等等与硬件相关的服务。
   我们再从底层往上看,可以看到存在于linux内核之上的是程序库和android的运行程序。libraries即是程序库,它们主要通过Android应用程序框架为开发者提供服务和支持。核心库有:
<activity android:name=".UITestActivity" android:label="@string/app_name" > <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity>
    所以我们可以看出基本的定义申明格式是

<activity             android:name=".*(Activity类名)"            android:label="@string/next_name">        </activity>

    现在我们已经有了一个基本的页面了但是页面上只有hello World这样的字眼。那么就让我们加上一些东西吧。
    首先我们在页面上加上一个文本框和2个按钮。在android中要在页面上加入按钮等构件,需要先在res/layout/*.xml中设置。*.xml取决于Activity中setContentView()中的对象。
    在这里我们使用LinearLayout,线性布局。具体设置如下
<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:layout_width="fill_parent"        android:layout_height="wrap_content"        android:text="@string/hello" />    <EditText        android:id="@+id/text"        android:layout_width="fill_parent"        android:layout_height="wrap_content"        android:text="@string/text" />    <LinearLayout        xmlns:android="http://schemas.android.com/apk/res/android"        android:id="@+id/linearlayout1"        android:layout_width="fill_parent"        android:layout_height="wrap_content"        android:gravity="center" >        <Button            android:id="@+id/showButton"            android:layout_width="wrap_content"            android:layout_height="wrap_content"            android:text="@string/showButton" />        <Button            android:id="@+id/clearButton"            android:layout_width="wrap_content"            android:layout_height="wrap_content"            android:text="@string/clearButton" />        </LinearLayout></LinearLayout>

    这里使用了2重线性布局。其中的fill_parent为强制性地使构件扩展,以填充布局单元内尽可能多的空间。而wrap_content为强制性地使视图扩展以显示全部内容。
    然后在UITestActivity.java中加上java代码
showButton = (Button) findViewById(R.id.showButton);clearButton = (Button) findViewById(R.id.clearButton);text = (EditText) findViewById(R.id.text);

    这段代码的意义是取得视图中的构件,以便操作。
    然后我们设置它们的监听器对象实现功能。
//设置show按钮的监听器对象showButton.setOnClickListener(new View.OnClickListener() {public void onClick(View v) {new AlertDialog.Builder(UITestActivity.this).setTitle("imfomation")//设置标题.setIcon(android.R.drawable.ic_dialog_map)//设置图标.setMessage(text.getText()).show();//设置其内容并显示//创建一个AlertDialog并将text中的内容输出到其中}});//设置clear按钮的监听器对象clearButton.setOnClickListener(new View.OnClickListener() {public void onClick(View v) {text.setText("");}});

    到这儿其实还有问题,大家可以看到在定义新的Activity时有一个android:label="@string/*,在设置视图的构件时也有android:id="@+id/*,这些东西都还是在报错。其实将它们删了,程序也能再ADK上运行,但是你会发现文本中没有字符,按钮上没有标签。它们就是代表了*这个字符串的值,而这个值是在/res/values/strings.xml中定义声明的。如下:
<resources>    <string name="hello">Hello World, UITestActivity!</string>    <string name="app_name">TestPage1</string><string name="text">input you want</string><string name="showButton">show</string><string name="clearButton">clear</string>    </resources>

   
    这样我们就完成了一个简单的android程序。

热点排行