您的位置:首页 > 移动开发 > Android开发

Android学习记录第一篇

2012-06-03 15:12 204 查看
建立HelloWorld工程,版本android4.0。

修改技巧:使用ALT+/ 来提示补全! 使用Ctrl+1 来建议修改错误!

学习内容:

1. gen目录下R.java文件是系统自动生成,默认有attr,drawble,layout,string 4个静态类,每次添加资源时会自动生成,这是UI与代码沟通的桥梁!java 代码中引用资源格式:R.resource_type.resource_name,xml布局文件中引用格式:@[package:]type/name,使用自己包下资源可省略包名,如访问android系统中资源时,包名android不可 省略。布局文件中需要给组件添加id时使用格式:@+id/string_name,这里‘+’表示在R.java文件的内部静态类中添加静态变量“string_name”,以使代码中根据id获获取相应组件。

2. layout与控件布局属性,layout_gravity是设置控件在容器内对齐方式。layout_width和layout_height设置填充方式,可设置为根据内容填充大小等。线性布局LineLayout里的控件排列方式设置(orientation属性):水平或者垂直排列。绝对坐标定义的Absolute布局,使用绝对坐标定位控件,AbsoluteLayout里面的控件都以layout_x
、layout_y来定义其位置。相对位置布局RelativeLayout,设置里面控件的排列属性。固定帧方式布局FrameLayout,布局里所有的控件都被放到布局的左上角,一层覆盖一层。

3. 控件与变量的绑定以及控件事件的监听。

修改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:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="@string/hello" />
    <EditText
        android:text="EditText01"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:id="@+id/edtInput"/>
    <LinearLayout 
        android:id="@+id/lineLayout01"
        android:layout_height="wrap_content"
        android:layout_width="fill_parent"
        android:gravity="center_horizontal">
	    <Button 
	        android:id="@+id/btnShow"
	        android:layout_height="wrap_content"
	        android:layout_width="wrap_content"
	        android:text="显示"/>
	    <Button
	        android:id="@+id/btnClear"
	        android:layout_height="wrap_content"
	        android:layout_width="wrap_content"
	        android:text="清除"/>	    
    </LinearLayout>       

</LinearLayout>


修改HelloWorldActivity.java如下

package com.zombie.hello;

import android.app.Activity;
import android.app.AlertDialog;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;

public class HelloWorldActivity extends Activity {
    /** Called when the activity is first created. */
	Button btnShow;
	Button btnClear;
	EditText edtInput;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        
        // 控件与变量的绑定
        btnShow = (Button)findViewById(R.id.btnShow);
        btnClear = (Button)findViewById(R.id.btnClear);
        edtInput = (EditText)findViewById(R.id.edtInput);
        
        // 添加按钮事件响应
        btnShow.setOnClickListener(new ClickListener());
        btnClear.setOnClickListener(new ClickListener());  
    }
    class ClickListener implements OnClickListener {
		@Override
		public void onClick(View v) {
			// TODO Auto-generated method stub
			if( v == btnShow ) {
				new AlertDialog.Builder(HelloWorldActivity.this)
				.setIcon(android.R.drawable.ic_dialog_alert)
				.setTitle("信息")
				.setMessage(edtInput.getText())
				.show();					
			}
			else if( v == btnClear ) {
				edtInput.setText("Hello world, android!");
			}
		}        	
    }
}


运行效果:

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: