Android基础概念
2012-05-16 19:08 Rollen Holt 阅读(1417) 评论(1) 收藏 举报最近为了应付老外的Android作业,所以不得不开始学习一下Android。在这里不得不吐槽一下学校,悄悄的将我们选的嵌入式换成Android,而不通知我们。
先简单的介绍一下Android中UI组件的一些通用属性吧:
android:id 是指定控件的位置标识,在java程序中可以通过findViewById(“id”)来获取指定的android界面组件。
android:layout_width和android:layout_height用来指定界面组件的宽度和高度,如果这个属性值设置为fill_parent的话,也就是说明这个组件和其父组件具有相同的宽度,如果属性值设置为warp_content的话,说明这个组件的宽度或者高度刚刚能包裹他的内容。
android之所以采用XML文件来定义用户界面,我觉的就是为了解耦。使得java程序专门负责业务逻辑实现。
使用Eclipse开发Android应用比较简单,一般只需要做大体两件事情:使用main.xml定义用户界面,然后使用java编写业务逻辑实现。
下面大致说说几个主要目录的作用,如图:
下面来建立一个基本的例子来看看,首先使用eclipse建立一个android项目,然后修改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/show"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="" />
	
    <!-- 设置按钮的文本为“单击我” -->
    <Button
        android:id="@+id/ok"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="单击我" />
</LinearLayout>
然后设置HelloWorldActivity.java文件内容如下:
package hello.com;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;
import android.widget.LinearLayout;
public class HelloWorldActivity extends Activity {
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        //设置使用main.xml文件定义的界面布局
        setContentView(R.layout.main);
        //获取UI界面中id为ok的按钮
        Button button=(Button)findViewById(R.id.ok);
        //为按钮绑定一个时间监听器
        button.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				//或许UI界面中id为show的文本框
				final TextView show=(TextView)findViewById(R.id.show);
				//改变文本框的内容
				show.setText("Hello Android "+new java.util.Date());
				
			}
		});
     
       
    }
}
好了,然后运行,结果如下图:
==============================================================================
本博客已经废弃,不在维护。新博客地址:http://wenchao.ren
我喜欢程序员,他们单纯、固执、容易体会到成就感;面对压力,能够挑灯夜战不眠不休;面对困难,能够迎难而上挑战自我。他
们也会感到困惑与傍徨,但每个程序员的心中都有一个比尔盖茨或是乔布斯的梦想“用智慧开创属于自己的事业”。我想说的是,其
实我是一个程序员
==============================================================================
 
 
                    
                     
                    
                 
                    
                

 
                
            
         浙公网安备 33010602011771号
浙公网安备 33010602011771号