技术开发 频道

开发人员比较Windows Mobile与OPhone

  GUI开发 从一个HelloWorld开始

  1:先说Android,当我们使用向导创建了一个新的Android工程之后,在包浏览中可以看到以下的文件。

  我们要编辑的文件分布在 src和res下,包括HelloWorld.java和 main.xml , strings.xml.

  这个HelloWorld就继承自Activity(Android Framework里面最重要的一个类, 我们简单地理解为它是一个UI的容器,直接跟用户打交道最前端的类。对于Windows mobile了的程序员来讲,简单的理解就是 Activity+View=Form.

  还有一个R.java,这个类是系统根据res文件夹中的内容自动为你生成的,大家不要修改它.我们先讲一下res文件夹,在这一点上,wm和 Anroid很相似,res是resources的缩写,顾名思义,你程序中所需要的文字,图片,布局文件等等资源都是放在这个文件夹下面的,你现在看到这个文件夹下面有

  drawable - 这个是放图片的

  layout - 这个是放布局文件的

  values - 下面放字符串(strings.xml ),颜色(colors.xml ),数组(arrays.xml )

  Android 帮我们把这些资源都管理起来,内容资源化的作用是很明显的,做国际化方便了,使用同一个资源的时候也方便也更节省空间(全局的引用),res文件夹中内容变化,R.java都会重新编译同步更新,所以这个类不需要你去手动更新了。

  最后是AndroidManifest.xml. 你每次添加一个Acivity都需要在这个文件中描述一下。

  看一下代码:

public class HelloWolrd extends Activity {
    
/** Called when the activity is first created. */
    @Override
    
public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        
//指定这个Activity的界面布局,如果不指定,运行起来是默认空白的,如何布局后面讲述
        setContentView(R.layout.main);      
      
//这句话就是用来获取layout中设置的界面控件对象的,这个id是在button中指定的
           android:id
="@+id/button_normal"
        Button btn
=(Button)this.findViewById(R.id.button_normal);
        
        
//为btn添加响应函数
        btn.setOnClickListener(
new OnClickListener(){

            
public void onClick(View arg0) {
                TextView tv
=(TextView)this.findeViewbyId(R.id.text);
                        tv.setText(R.id.hello);
            }
            
            
        }
        );
    }
}

布局文件mani.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/text"
    android:layout_width
="fill_parent"
    android:layout_height
="wrap_content"
    android:text
="@string/hello"
    
/>
<Button android:id="@+id/button_normal"
            android:text
="@string/clickme"
            android:layout_width
="wrap_content"
            android:layout_height
="wrap_content" />
</LinearLayout>




    

 

0
相关文章