Android onCreate 详解

在AndroidManifest.xml文件中的 intent-filter 元素中有这么两句:

<activity android:name=".MainActivity">
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
</activity>

当写好的应用发布到手机上之后,当双击”抽屉“里该应用的图标时,系统会将这个点击事件包装成一个Intent,该Intent包含两个参数,如上所述的两个参数action, category
被传递给应用之后,在应用的功能清单文件中寻找与该意图匹配的意图过滤器,如果匹配成功,找到相匹配的意图过滤器所在的Activity元素,再根据
activity 元素的”name“属性来寻找其对应的Activity类。接着Android操作系统创建该Activity类的实例对象,对象创建完成之后,会执行到该类的onCreate方法,
此onCreate方法是重写父类Activity的onCreate方法而实现的。onCreate方法用来初始化Activity实例对象。如下是AndroidUIdemo.java类中的onCreate方法的代码:

  @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.caculator_layout);

        Log.d(TAG,"debug");
        Log.i(TAG,"info");
        Log.w(TAG,"warm");
        Log.e(TAG,"error");
        Log.v(TAG,"verbose");

        //找控件
        initView();

        //设置点击事件
        initClickEvent();

    }

一、 super.onCreate(savedInstanceState)

其中super.onCreate(savedInstanceState)的作用是调用其父类Activity的onCreate方法来实现对界面的图画绘制工作。
在实现自己定义的Activity子类的onCreate方法时一定要记得调用该方法,以确保能够绘制界面。

super.onCreate主要是加载一些组件。

第一件事情便是super.onCreate(savedInstanceState),其实这条语句放在子类中的onCreate方法中的任何位置都可,问题只是
super.onCreate(savedInstanceState)必须要被执行,所以,最好也就是放在第一行,看起来比较明确。

在TargetActivity中重写了onCreate方法,而在Activity中的onCreate方法中已经对一些基本的基础进行了操作,如果子类没有super.onCraete方法的调用,会导致子类中的onCreate方法功能不全,从而出现错误。

savedInstanceState

在activity的生命周期中,只要离开了可见阶段,或者说失去了焦点,activity就很可能被进程终止了!,被KILL掉了,,这时候,就需要有种机制,能保存当时的状态,这就是savedInstanceState的作用。
当一个Activity在PAUSE时,被kill之前,它可以调用
onSaveInstanceState()来保存当前activity的状态信息(在paused状态时,要被KILLED的时候)。用来保存状态信息的Bundle会同时传给两个method,即onRestoreInstanceState() and onCreate().

二、setContentView(R.layout.caculator_layout)

作用:加载一个界面。
该方法中传入的参数是”R.layout.caculator_layout“,其含义为R.java类中静态内部类layout的静态常量caculator_layout的值,
而该值是一个指向res目录下的layout子目录下的caculator_layout.xml文件的标识符。因此代表着显示caculator_layout.xml所定义的画面。

三、两种点击事件

 /*
    *设置点击事件
 */
    private void initClickEvent(){
        //第一种设置方式
        mCancel.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v)
            {
                Log.d(TAG, "content=="+((TextView)v).getText() );
            }
        });

        //第二种设置方式
        mPlusOrMinus.setOnClickListener(this);
        mPlus.setOnClickListener(this);
    }

 @Override
    public void onClick(View v) {
        //如果有多个空间设置点击事件,我们这里需要统一处理的话,需要判断是
        //哪一个控件
        if(v == mPlusOrMinus){
            Log.d(TAG, "点击了mPlusOrMinus" + ((TextView)v).getText().toString());
        }else if(v == mPlus){
            //同样的方式去判断
        }

        //另一种方式及时用switch来判断id

        //先拿到id
        int id = v.getId();
        switch(id){
            case R.id.tv_number_one:
                //one这个内容被点击了,就在这里处理

                break;
            case R.id.tv_equal:
                //处理等号事件
                break;
        }

    }

控件初始化

  /*
    *在这里找到控件
     */
    private  void initView()
    {
        mCancel = (TextView) this.findViewById(R.id.tv_cancel);
        mPlusOrMinus = (TextView) this.findViewById(R.id.tv_plus_or_minus);
        mMod = (TextView) this.findViewById(R.id.tv_mod);
        mDivider = (TextView) this.findViewById(R.id.tv_divider);
        mOne = (TextView) this.findViewById(R.id.tv_number_one);
        mTwo = (TextView) this.findViewById(R.id.tv_number_two);
        mThree = (TextView) this.findViewById(R.id.tv_number_three);
        mTime = (TextView) this.findViewById(R.id.tv_time);
        mFour = (TextView) this.findViewById(R.id.tv_number_four);
        mFive = (TextView) this.findViewById(R.id.tv_number_five);
        mSix = (TextView) this.findViewById(R.id.tv_number_six);
        mMinus = (TextView) this.findViewById(R.id.tv_minus);
        mSeven = (TextView) this.findViewById(R.id.tv_number_seven);
        mEight = (TextView) this.findViewById(R.id.tv_number_eight);
        mNine = (TextView) this.findViewById(R.id.tv_number_nine);
        mPlus = (TextView) this.findViewById(R.id.tv_plus);
        mZero = (TextView) this.findViewById(R.id.tv_number_zero);
    }
  • 29
    点赞
  • 104
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值