Android学习笔记 - Activity篇

1.Activity的主要作用
  Activity是界面、用户接口、控件窗口,负责程序与用户间进行交互

 

2.创建一个Activity需要的步骤
  1.一个Activity就是一个类,并且这个类要继承Activity
  2.需要复写(@override)onCreate方法,第一次运行就会运行此方法
  3.每一个Activity都需要在AndroidMainfest.xml文件中配置
  4.为Activity添加必要的控件(layout.xml)
  5.设置Activity使用的Layout文件(setContentView(R.layout.layoutID))

 

3.获取Layout文件中配置的控件
  setContentView(R.layout.layoutID)  //设置内容对应的而已文件ID
  TextView myText = (TextView)findViewById(R.id.myTextView);  //获取一个TextView
  myText.setText("First TextView");  //设置TextView显示文本

 

//布局示例

[html]  view plain copy
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  3.     android:layout_width="fill_parent"  
  4.     android:layout_height="fill_parent"  
  5.     android:orientation="vertical" >  
  6.   
  7.     <TextView  
  8.         android:layout_width="fill_parent"  
  9.         android:layout_height="wrap_content"  
  10.         android:text="@string/hello" />  
  11.   
  12. </LinearLayout>  

 

//类示例

[java]  view plain copy
  1. public class HelloActivity extends Activity {  
  2.     /** Called when the activity is first created. */  
  3.     @Override  
  4.     public void onCreate(Bundle savedInstanceState) {  
  5.         super.onCreate(savedInstanceState);  
  6.         setContentView(R.layout.hello);  
  7.     }  
  8. }  




Activity生命周期篇
 

1.Activity生命周期七大函数
 //当Activity第一次被创建时调用,用于设置布局文件,绑定监听器
 protected void OnCreate(){}
 //当Activity看到后调用
 protected void OnStart(){} 
 //当Activity能够获取用户焦点时调用
 protected void OnResume(){}
 //当应用程序启动了另外一个Activity时调用,用于保存当前页面的数据
 protected void OnPause(){}
 //当Activity处于不可见的情况下调用,如果是对话框则不会调用 
 protected void OnStop(){} 
 //当回退键回到原来的Activity时调用,此时不需要OnCreate
 protected void OnRestart(){} 
 //当Activity被销毁时调用(如:调用Finish方法、资源回收、后退键)
 protected void OnDestory(){}


2.调用顺序(两个Activity,分别是1.2.)
 启动程序:1.OnCreate -> 1.OnStart -> 1.OnResume
 事件切换:1.OnPause -> 2.OnCreate -> 2.OnStart -> 2.OnResume -> 1.OnStop
 点击后退:2.OnPause -> 1.OnRestart -> 1.OnStart -> 1.OnResume -> 2.OnStop -> 2.OnDestory

3.Task
 用于存放Activity的一个栈(Stack),先入后出,回退也是这原理
 一个应用程序有一个Task,手机端显示的永远是栈里最顶部的Activity
 回退后,被弹出的Activity将被销毁

 

4.窗口方式的Activity(对话框)
 在AndroidManifest.xml中,在Activity节点中增加属性 android:theme="@android:style/Theme.Dialog"

 

5.资源回收
 系统会在资源不足时,对Activity资源进行回收
 被回收的Activity是处于以下状态:OnPause OnStop OnDestory





单选按钮组件


单个选中 : 一组单选按钮定义在一个RadioGroup中, 这一组RadioButton只能有一个被选中;

设置监听 : 可以给RadioGroup设置OnCheckedChangeListener监听器, 当出现选项改变的时候, 可以调用被选中的RadioButton的id, 然后执行相应方法;

指定id : RadioButton必须为每个单选按钮指定id, 否则将无法激活回调方法;


代码示例 

XML源码 : 

[html]  view plain copy 在CODE上查看代码片 派生到我的代码片
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  3.     android:layout_width="match_parent"  
  4.     android:layout_height="match_parent"  
  5.     android:orientation="vertical" >  
  6.       
  7.     <TextView   
  8.         android:layout_width="wrap_content"  
  9.         android:layout_height="wrap_content"  
  10.         android:text="性别 : "  
  11.         android:textSize="15pt"/>  
  12.       
  13.     <RadioGroup   
  14.         android:id="@+id/radio_group"  
  15.         android:orientation="vertical"  
  16.         android:layout_height="wrap_content"  
  17.         android:layout_width="fill_parent">  
  18.           
  19.         <RadioButton   
  20.             android:id="@+id/nan"  
  21.             android:layout_width="fill_parent"  
  22.             android:layout_height="wrap_content"  
  23.             android:text="男"/>  
  24.           
  25.         <RadioButton   
  26.             android:id="@+id/nv"  
  27.             android:layout_width="fill_parent"  
  28.             android:layout_height="wrap_content"  
  29.             android:text="女"/>  
  30.           
  31.         <RadioButton   
  32.             android:id="@+id/renyao"  
  33.             android:layout_width="fill_parent"  
  34.             android:layout_height="wrap_content"  
  35.             android:text="人妖"/>  
  36.           
  37.         <RadioButton   
  38.             android:id="@+id/yaoren"  
  39.             android:layout_width="fill_parent"  
  40.             android:layout_height="wrap_content"  
  41.             android:text="妖人"/>  
  42.           
  43.     </RadioGroup>  
  44.   
  45. </LinearLayout>  

Activity源码  : 

[java]  view plain copy 在CODE上查看代码片 派生到我的代码片
  1. package shuliang.han.button;  
  2.   
  3. import android.app.Activity;  
  4. import android.os.Bundle;  
  5. import android.widget.RadioGroup;  
  6. import android.widget.Toast;  
  7. import android.widget.RadioGroup.OnCheckedChangeListener;  
  8.   
  9. public class RadioButtonActivity extends Activity {  
  10.   
  11.     RadioGroup radioGroup;  
  12.       
  13.     @Override  
  14.     protected void onCreate(Bundle savedInstanceState) {  
  15.         super.onCreate(savedInstanceState);  
  16.         setContentView(R.layout.radio_button);  
  17.           
  18.         radioGroup = (RadioGroup) findViewById(R.id.radio_group);  
  19.           
  20.         radioGroup.setOnCheckedChangeListener(new OnCheckedChangeListener() {  
  21.               
  22.             @Override  
  23.             public void onCheckedChanged(RadioGroup group, int checkedId) {  
  24.                 switch (checkedId) {  
  25.                 case R.id.nan:  
  26.                     Toast.makeText(getApplicationContext(), "男", Toast.LENGTH_LONG).show();  
  27.                     break;  
  28.                 case R.id.nv:  
  29.                     Toast.makeText(getApplicationContext(), "女", Toast.LENGTH_LONG).show();  
  30.                     break;  
  31.                 case R.id.renyao:  
  32.                     Toast.makeText(getApplicationContext(), "人妖", Toast.LENGTH_LONG).show();  
  33.                     break;  
  34.                 case R.id.yaoren:  
  35.                     Toast.makeText(getApplicationContext(), "妖人", Toast.LENGTH_LONG).show();  
  36.                     break;  
  37.                 default:  
  38.                     break;  
  39.                 }  
  40.             }  
  41.         } );  
  42.     }  
  43.       
  44.       
  45.       
  46. }  


效果图  : 


.

.


四. 复选框CheckBox组件


CheckBox复选框可以同时勾选几个选项 : 

代码示例 : 

[html]  view plain copy 在CODE上查看代码片 派生到我的代码片
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  3.     android:layout_width="match_parent"  
  4.     android:layout_height="match_parent"  
  5.     android:orientation="vertical" >  
  6.       
  7.     <TextView   
  8.         android:layout_height="wrap_content"  
  9.         android:layout_width="fill_parent"  
  10.         android:text="爱好"/>  
  11.       
  12.     <CheckBox   
  13.         android:id="@+id/smoke"  
  14.         android:layout_height="wrap_content"  
  15.         android:layout_width="fill_parent"  
  16.         android:text="抽烟"  
  17.         android:checked="true"/>  
  18.       
  19.     <CheckBox   
  20.         android:id="@+id/drink"  
  21.         android:layout_height="wrap_content"  
  22.         android:layout_width="fill_parent"  
  23.         android:text="喝酒"/>  
  24.       
  25.     <CheckBox   
  26.         android:id="@+id/head"  
  27.         android:layout_height="wrap_content"  
  28.         android:layout_width="fill_parent"  
  29.         android:text="烫头"/>  
  30.   
  31. </LinearLayout>  

效果图  : 




五. ToggleButton组件


组件介绍 : 该组件外形与按钮相似, 该按钮组件的底部有一个带颜色线条, 当checked属性为true的时候, 该线条显示颜色, checked属性为false的时候, 盖线条不显示颜色;

文本显示 : 当android:checked属性为true的时候, 显示android:textOn文本, 反之显示android:textOff文本;

重要的XML属性 : 

-- 是否选中 : android:checked, 值为true, 或者false;

-- 选中文本 : android:textOn, 字符串, 当checked属性为true的时候显示该文本;

-- 取消文本 : android:textOff, 字符串, 当checked属性为false的时候显示该文本;


代码示例 : 

XML代码 : 

[html]  view plain copy 在CODE上查看代码片 派生到我的代码片
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  3.     android:layout_width="match_parent"  
  4.     android:layout_height="match_parent"  
  5.     android:orientation="vertical" >  
  6.       
  7.     <ToggleButton   
  8.         android:id="@+id/toggle"  
  9.         android:layout_width="wrap_content"  
  10.         android:layout_height="wrap_content"  
  11.         android:textOn="为了联盟"  
  12.         android:textOff="为了部落"  
  13.         android:checked="true"/>  
  14.   
  15. </LinearLayout>  

Activity代码 : 

[java]  view plain copy 在CODE上查看代码片 派生到我的代码片
  1. package shuliang.han.button;  
  2.   
  3. import android.app.Activity;  
  4. import android.os.Bundle;  
  5. import android.widget.CompoundButton;  
  6. import android.widget.CompoundButton.OnCheckedChangeListener;  
  7. import android.widget.Toast;  
  8. import android.widget.ToggleButton;  
  9.   
  10. public class ToggleButtonActivity extends Activity {  
  11.   
  12.     @Override  
  13.     protected void onCreate(Bundle savedInstanceState) {  
  14.         super.onCreate(savedInstanceState);  
  15.         setContentView(R.layout.toggle_button);  
  16.           
  17.         ToggleButton toggleButton = (ToggleButton) findViewById(R.id.toggle);  
  18.           
  19.         toggleButton.setOnCheckedChangeListener(new OnCheckedChangeListener() {  
  20.               
  21.             @Override  
  22.             public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {  
  23.                 if(isChecked)  
  24.                     Toast.makeText(getApplicationContext(), "为了联盟", Toast.LENGTH_LONG).show();  
  25.                 else  
  26.                     Toast.makeText(getApplicationContext(), "为了部落", Toast.LENGTH_LONG).show();  
  27.             }  
  28.         });  
  29.     }  
  30.       
  31. }  

效果图  : 



六. Switch按钮


最低版本要求 : Switch组件需要最低的SDK版本是14;

Switch的XML属性 : 

-- 是否选中 : android:checked, 值为true 或者 false;

-- 最小宽度 : android:switchMinWidth, 设置开关的最小宽度;

-- 设置空白 : android:switchPadding, 设置开关 与 文本 之间的空白;

-- 文本样式 : android:switchTextAppearance, 设置文本的样式;

-- 选中文本 : android:textOn, android:checked为true的时候显示的文本;

-- 关闭文本 : android:textOff, android:checked为false的时候显示的文本;

-- 文本风格 : android:textStyle, 设置文本的风格, 可以是资源文件;

-- 开关按钮 : android:thumb, 值为int, 即R.id的资源, 设置开关的按钮;

-- 开关轨道 : android:track, 值为int, 即R.id的资源, 设置开关的轨道;

-- 字体风格 : android:typeface, 设置开关文本的字体风格;


代码示例 : 

XML源码 : 

[html]  view plain copy 在CODE上查看代码片 派生到我的代码片
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  3.     android:layout_width="match_parent"  
  4.     android:layout_height="match_parent"  
  5.     android:orientation="vertical" >  
  6.       
  7.     <Switch android:id="@+id/switch_button"  
  8.         android:layout_width="wrap_content"  
  9.         android:layout_height="wrap_content"  
  10.         android:textOff="为了部落"  
  11.         android:textOn="为了联盟"  
  12.         android:thumb="@drawable/check"  
  13.         android:checked="true"/>  
  14.   
  15. </LinearLayout>  

Activity代码  : 

[java]  view plain copy 在CODE上查看代码片 派生到我的代码片
  1. package shuliang.han.button;  
  2.   
  3. import android.app.Activity;  
  4. import android.os.Bundle;  
  5. import android.widget.CompoundButton;  
  6. import android.widget.CompoundButton.OnCheckedChangeListener;  
  7. import android.widget.Switch;  
  8. import android.widget.Toast;  
  9.   
  10. public class SwitchButtonActivity extends Activity {  
  11.   
  12.     @Override  
  13.     protected void onCreate(Bundle savedInstanceState) {  
  14.         super.onCreate(savedInstanceState);  
  15.         setContentView(R.layout.switch_button);  
  16.           
  17.         Switch switch1 = (Switch) findViewById(R.id.switch_button);  
  18.           
  19.         switch1.setOnCheckedChangeListener(new OnCheckedChangeListener() {  
  20.               
  21.             @Override  
  22.             public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {  
  23.                 if(isChecked)  
  24.                     Toast.makeText(getApplicationContext(), "为了联盟", Toast.LENGTH_LONG).show();  
  25.                 else  
  26.                     Toast.makeText(getApplicationContext(), "为了部落", Toast.LENGTH_LONG).show();  
  27.             }  
  28.         });  
  29.     }  
  30.   
  31.       
  32. }  

效果图  : 






view组件


  • TextView:文本显示控件
  • Button:按钮控件
  • EditText:可编辑的文本框控件
  • CheckBox:复选框控件
  • RadioGroup:单选框组件
  • Spinner:下拉列表框组件
  • DatePicker:日期选择控件
  • TimePicker:时间选择组件
  • ScrollView:滚动条组件
  • ProgressBar:进度条
  • SeekBar:拖动条
  • RatingBar:评分
  • ImageView:图片显示
  • ImageButton:图片按钮
  • AutoCompleteTextView:自动完成文本组件
  • Dialog:对话框组件
  • Toast:信息提示
  • Menu:菜单显示组件


View组件常用属性和操作方法
android:background public voidsetBackgroundResource()设置组件背景
android:clickable   public void setClickable()是否可以产生单击事件
android:contentDescription 。。。                  定义视图的内容描述
android:drawingCacheQuality                           定义绘图时所需要的缓冲区大小
android:focusable                                         设置是否可以获得焦点
android:focusableInTouchMode                          在触控模式下是否可以获得焦点
android:id                                                   设置组件ID
android:longClickable                                    设置长按事件是否可用
android:minHeight                                         定义视图最小高度
android:minWidth                                           定义视图最小宽度
android:padding                                            填充所有边缘
android:paddingBottom 填充下边缘
android:paddingLeft    填充左边缘
android:paddingRight 填充右边缘
android:paddingTop    填充上边缘
android:scaleX    设置X轴缩放
android:scaleY    设置Y轴缩放
android:scrollbarSize 设置滚动条大小
android:visibility 设置是否显示组件
android:layout_width 定义组件显示的宽度
android:layout_height 定义组件显示的高度
android:layout_gravity 组件文字的对齐方式
android:layout_margin 设置页边距
android:layout_marginTop 设置上边距
android:layout_marginBottom 设置下边距
android:layout_marginLeft 设置左边距
android:layout_marginRight 右边距
android:background 背景颜色

在main.xml文件中配置的属性信息,在Activity程序中也可以进行控制

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值