[笔记][andriod开发]Activity的生命周期、常见布局、常见控件(单选,多选,弹出框,进度条,列表)

【01.07~08】Activity的生命周期


1、Activity的七个生命周期
OnCreate
OnStart
OnResume
OnPause
OnStop
OnRestart
OnDestroy

有个传说中的示意图~:



教程里依旧是例子来说明的,这里就不搞了,基本上理解起来还是很方便的
1、创建两个Activity,Activity01和Activity02
2、启动Activity01,生命周期如下:
OnCreate()     //这个方法也复写过很多次了~需要呈现在这个Activity的控件注册和相关监听动作绑定都是在这个方法中实现
OnStart()
OnResume()
3、点击Activity01中的按钮,到达Activity02
此时,两个Activity的生命周期如下:
Activity01--->OnPause()
Activity02--->OnCreate()
Activity02--->OnStart()
Activity02--->OnResume()
Activity01--->OnStop()
*由于Activity02的出现完全遮盖了Activity01,就会有OnStop这个生命周期存在;如果说Activity02是一个非完全遮盖的Activity,比如一个对话框形式的Activity,那么Activity01的OnStop()周期则不会出现
*OnPause()时,我们可以在这里存储用户之前输入的信息和动作等,以便用户回到此处能继续刚才的操作
4、在Activity02点击返回按钮,则会回到Activity01
此时,两个Activity的生命周期如下:
Activity02--->OnPause()
Activity01--->OnRestart()
Activity01--->OnStart()
Activity01--->OnResume()
Activity02--->OnStop()
Activity02--->OnDestroy()
也就是说,返回之后,Activity02被销毁了

生命周期还是很好理解的~
下面说的是Task,一个堆栈形式的任务管理形式,将所有的Activity放在一个栈中,以后进先出的原理,在栈顶的永远都是当前呈现的Activity
接着说了一下对话框形式的Activity要怎么写~
也就是在AndroidMainfest.xml里注册Activity时,设置属性
android:theme   =  "  @android:style/Theme.Dialog  "

把上一次的计算器拿出来,设置一下,则如下:

在这种情况下,Activity01是没有OnStop()这个生命周期的

以上~

*这节课介绍到的SDK中的Docs,如果你用的是4.1以上的,可能会出现docs页面载入非常缓慢的情况,是因为天朝墙了某哥之后,有个js文件一直请求不到,于是就卡住了o(╯□╰)o。
网上搜了一下,有个批处理的
更简单的是,如果你的浏览器有相关的屏蔽插件,比如FireFox的Adblock插件,将这个js添加到Adblock的屏蔽名单中即可(http://www.google.com/jsapi),本地打开又恢复了速度~


【01.09-01.11】Activity的布局

1、LinearLayout 线性布局,有前端基础的看这个应该很容易,分为水平布局和垂直布局= =其余不多说了。。
2、TableLayout 表格布局
嵌套如下
TabelLayout
     TableRow
          TextView(或者其他控件,也可以
TableLayout有一个属性为android:stretchColumns="1",也就是说以第1列自动拉伸(列序号是从0开始的,从视觉上看应该是第二列),别的列自适应内容宽度
3、RelativeLayout 相对布局

呃,这三课有前端经验的基本不用看,非常好理解,主要在于属性较多,使用时多查查文档

【01.12】常用控件2~
1、RadioGroup和RadioButton
2、CheckBox
3、Toast(谷歌太吃货……

目标:


下面是制作步骤[´・ω・`]
1、创建一个工程,并将控件都部署上去,用的是线性布局
<  LinearLayout   xmlns:android  = "http://schemas.android.com/apk/res/android"
     xmlns:tools =   "http://schemas.android.com/tools"
     android:orientation =   "vertical"
     android:layout_width =   "match_parent"
     android:layout_height =   "match_parent"   >
     < TextView
            android:id  = "@+id/textView1"
            android:layout_width  = "wrap_content"
            android:layout_height  = "wrap_content"
            android:text  = "@string/show_label"   />
     < RadioGroup
         android:id  = "@+id/sexgroup"
         android:layout_width  = "fill_parent"
         android:layout_height  = "wrap_content"
           android:orientation   =  "horizontal"          >
         <  RadioButton
             android:id  = "@+id/famale"
             android:layout_width  = "wrap_content"
             android:layout_height  = "wrap_content"
             android:text  = "@string/radio_famale"
             />
         <  RadioButton
             android:id  = "@+id/male"
             android:layout_width  = "wrap_content"
             android:layout_height  = "wrap_content"
             android:text  = "@string/radio_male"
             />
     </ RadioGroup   >
     < CheckBox
         android:id  = "@+id/read"
         android:layout_width  = "fill_parent"
         android:layout_height  = "wrap_content"
         android:text  = "@string/checkbox_read"   />
     < CheckBox
         android:id  = "@+id/sleep"
         android:layout_width  = "fill_parent"
         android:layout_height  = "wrap_content"
         android:text  = "@string/checkbox_sleep"   />
     < CheckBox
         android:id  = "@+id/eat"
         android:layout_width  = "fill_parent"
         android:layout_height  = "wrap_content"
         android:text  = "@string/checkbox_eat"   />
</  LinearLayout >
2、在Activity07中的控件创建和事件注册
package com.example.activity07;

import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.CompoundButton.OnCheckedChangeListener;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.Toast;

public class Activity07 extends Activity {
     //声明
     private RadioGroup radioSex = null;
     private RadioButton radioFamale = null;
     private RadioButton radioMale = null;
     private CheckBox read = null;
     private CheckBox sleep = null;
     private CheckBox eat = null;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity07);
        
        //获得控件对象
        radioSex = (RadioGroup)findViewById(R.id.sexgroup);
        radioFamale = (RadioButton)findViewById(R.id.famale);
        radioMale = (RadioButton)findViewById(R.id.male);
        read = (CheckBox)findViewById(R.id.read);
        sleep = (CheckBox)findViewById(R.id.sleep);
        eat = (CheckBox)findViewById(R.id.eat);
        
        //RadioGroup对象的监听器
        radioSex.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
               //一个匿名内部类
               public void onCheckedChanged(RadioGroup group, int checkedId) {
                    // TODO Auto-generated method stub
                    if(radioFamale.getId() == checkedId){
                         Toast.makeText(Activity07.this, "famale", Toast.LENGTH_SHORT).show();
                    }
                    else if(radioMale.getId() == checkedId){
                         Toast.makeText(Activity07.this, "male", Toast.LENGTH_SHORT).show();
                    }
               }
          });
        
        //Checkbox对象的监听器
        read.setOnCheckedChangeListener(new checkboxclick());
        sleep.setOnCheckedChangeListener(new checkboxclick());
        eat.setOnCheckedChangeListener(new checkboxclick());
    }
    
    class checkboxclick implements OnCheckedChangeListener{
         public void onCheckedChanged(CompoundButton buttonView,
                   boolean isChecked) {
              // TODO Auto-generated method stub
              if(isChecked){
                    Toast.makeText(Activity07.this, "checked", Toast.LENGTH_SHORT).show();
               }
               else{
                    Toast.makeText(Activity07.this, "false", Toast.LENGTH_SHORT).show();
               }
         }
    }
    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.activity07, menu);
        return true;
    }
}


结果

写的时候借助于提示信息,还是比较容易的,主要是相关监听器可能不是很清楚,所以还是多查查Api哦

【01.13】常见控件3

1、ProgressBar
2、ListView

ProgressBar实例
1、创建一个应用,并布局
<  LinearLayout   xmlns:android  = "http://schemas.android.com/apk/res/android"
     xmlns:tools =   "http://schemas.android.com/tools"
     android:layout_width =   "match_parent"
     android:layout_height =   "match_parent"
     android:orientation =   "vertical"   >

     < ProgressBar
         android:id  = "@+id/progressOne"
         style =   "?android:attr/progressBarStyle"
         android:layout_width  = "wrap_content"
         android:layout_height  = "wrap_content"
         android:visibility  = "gone"   />
     < ProgressBar
         android:id  = "@+id/progressTwo"
         style =   "?android:attr/progressBarStyleHorizontal"
         android:layout_width  = "wrap_content"
         android:layout_height  = "wrap_content"
         android:visibility  = "gone"
         android:max  = "10"   />
     < Button
         android:id  = "@+id/mybutton"
         android:layout_width  = "wrap_content"
         android:layout_height  = "wrap_content"
         android:text  = "@string/button"  />
</  LinearLayout >
需要注意到几个属性
style,进度条的样式不止这两种- -可以从可视布局看到,起码有四种
android:max是进度条的最大值,也可以在函数中设置
2、对象创建和事件绑定
package com.example.activity08;
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.*;
public class ProgressActivity extends Activity {
       private ProgressBar pone = null;
       private ProgressBar ptwo = null;
       private Button mybt = null;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super .onCreate(savedInstanceState);
        setContentView(R.layout. activity_progress );
       
        //创建控件对象
        pone = (ProgressBar)findViewById(R.id. progressOne);
        ptwo = (ProgressBar)findViewById(R.id. progressTwo);
        mybt = (Button)findViewById(R.id. mybutton);
       
        //绑定对应监听器
        mybt .setOnClickListener( new buttonClick());
    }
    private int i = 0;
    class buttonClick implements OnClickListener{
       public void onClick(View v) {
             // TODO Auto-generated method stub
             if (i ==0){
                   //显示进度条
                   pone .setVisibility(View. VISIBLE);
                   ptwo .setVisibility(View. VISIBLE);
            }
             else if ( i<10){
                   //转圈圈进度 默认如下,不需要写
                   //pone.setProgress(i);
                   //横向进度条,包括第一进度,和第二进度,可以区分不同任务的进度
                   ptwo .setProgress(i );
                   ptwo .setSecondaryProgress(i +1);
            }
             else {
                   pone .setVisibility(View. INVISIBLE);
                   ptwo .setVisibility(View. INVISIBLE);
            }
             i ++;
      }
      }
    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu. activity_progress , menu);
        return true ;
    }
}

下面是至今为止遇到的最复杂的一个控件~ListView
1、照旧,创建一个应用,这个控件是需要分成两个部分来处理,一个是主界面,一个是ListView里的单个对象
activity_listview.xml
<  LinearLayout   xmlns:android  = "http://schemas.android.com/apk/res/android"
     xmlns:tools =   "http://schemas.android.com/tools"
     android:layout_width =   "fill_parent"
     android:layout_height =   "fill_parent"
     android:orientation =   "vertical"   >
     < ListView
        android:id  = "@id/android:list"
        android:layout_width  = "fill_parent"
        android:layout_height  = "wrap_content"
        android:drawSelectorOnTop  = "false"
        android:scrollbars  = "vertical"   >
    </ ListView   >
</  LinearLayout >
这里一个新的属性
android:drawSelectorOnTop ="false"
这是一个长按下会用到的属性
如果设置成true则如下

设置成false的话,背景色会在文字下面,文字可见

item.xml
<  LinearLayout   xmlns:android  = "http://schemas.android.com/apk/res/android"
     xmlns:tools =   "http://schemas.android.com/tools"
     android:layout_width =   "fill_parent"
     android:layout_height =   "fill_parent"
     android:paddingLeft =   "10dip"
     android:paddingRight =   "10dip"
     android:paddingTop =   "1dip"
     android:paddingBottom =   "1dip"
     android:orientation =   "horizontal"   >
     < TextView
         android:id  = "@+id/item_name"
         android:layout_width  = "80dip"
         android:layout_height  = "30dip"
         android:textSize  = "20sp"
         android:singleLine  = "true"   />
     < TextView
         android:id  = "@+id/item_price"
         android:layout_height  = "fill_parent"
         android:layout_width  = "fill_parent"
         android:gravity  = "right"
         android:textSize  = "20sp"   />
</  LinearLayout >

2、在ListviewActivity.java中
package com.example.hellolistview;
import java.util.ArrayList;
import java.util.HashMap;
import android.os.Bundle;
import android.app.ListActivity;
import android.view.View;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import android.widget.Toast;
public class ListviewActivity extends ListActivity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
       // TODO Auto-generated method stub
       super.onCreate(savedInstanceState);
      setContentView(R.layout. activity_listview );
      
       //创建一个list,用另外一种视角看,相当于JSON数据的样子ORZ
      ArrayList<HashMap<String,String>> list=
                   new ArrayList<HashMap<String,String>>();
       //创建list中的每个数据对对象
      HashMap<String, String> item1 = new HashMap<String, String>();
      HashMap<String, String> item2 = new HashMap<String, String>();
      HashMap<String, String> item3 = new HashMap<String, String>();
      
       //给数据对赋值
      item1.put( "item_name" , "面包" );
      item1.put( "item_price" ,"$10" );
      item2.put( "item_name" , "蛋糕" );
      item2.put( "item_price" ,"$15" );
      item3.put( "item_name" , "布丁" );
      item3.put( "item_price" ,"$20" );
      
       //将数据添加到list中
      list.add(item1);
      list.add(item2);
      list.add(item3);
      
       //创建一个数据适配器,参数分别是对象,数据来源,数据显示 xml对象,键值对象,显示键值的控件对象
      SimpleAdapter adapter = new SimpleAdapter(
                   this , list,
                  R.layout. item ,
                   new String[] {"item_name" , "item_price"},
                   new int [] {R.id. item_name,R.id. item_price });
       //将数据加到listview 控件中
      setListAdapter(adapter);
    }
    @Override
    protected void onListItemClick(ListView l, View v, int position, long id) {
       // TODO Auto-generated method stub
       super.onListItemClick(l, v, position, id);
      Toast. makeText(ListviewActivity. this, id+ "," +position, Toast.LENGTH_SHORT ).show();
    }
}


需要注意的是,这次继承于ListActivity类,而不是一贯的Activity= =
实际上setContentView写不写无所谓,因为最后是由适配器来创建这个对象的
理解list还是有点绕,多想想JSON和图,应该能快点消化,
首先需要创建一个ArrayList对象,再创建对象中的每个数据对象,对应数据键值赋值后放入ArrayList对象中;再由适配器将数据和显示资源联系起来,并加载到主界面中

运行如下







评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值