Android学习记录第一篇

建立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!");
			}
		}        	
    }
}


运行效果:

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值