Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲
 Android教程網 >> Android系統教程 >> Android開發教程 >> Android自定義View探索(1)—生命周期

Android自定義View探索(1)—生命周期

編輯:Android開發教程

Android自定義View探索(一)—生命周期

Activity代碼:

public class FiveActivity extends AppCompatActivity {

    private MyView myView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        Log.e("log", "Activity生命周期:onCreate");
        setContentView(R.layout.activity_five);
        initView();
    }

    private void initView() {
        myView = (MyView) findViewById(R.id.my_view);
    }

    @Override
    protected void onStart() {
        super.onStart();
        Log.e("log", "Activity生命周期:onStart");
    }


    @Override
    protected void onResume() {
        super.onResume();
        Log.e("log", "Activity生命周期:onResume");
    }

    @Override
    protected void onRestart() {
        super.onRestart();
        Log.e("log", "Activity生命周期:onRestart");
    }


    @Override
    protected void onPause() {
        super.onPause();
        Log.e("log", "Activity生命周期:onPause");
    }

    @Override
    protected void onStop() {
        super.onStop();
        Log.e("log", "Activity生命周期:onStop");
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        Log.e("log", "Activity生命周期:onDestroy");
    }
}

自定義View代碼:

public class MyView extends View {
    public MyView(Context context, AttributeSet attrs) {
        super(context, attrs);
        Log.e("log", "onCreate");
    }

    @Override
    protected void onFinishInflate() {
        super.onFinishInflate();
        Log.e("log", "onFinishInflate");
    }


    @Override
    protected void onAttachedToWindow() {
        super.onAttachedToWindow();
        Log.e("log", "onAttachedToWindow");
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
        Log.e("log", "onMeasure");

    }

    @Override
    protected void onSizeChanged(int w, int h, int oldw, int oldh) {
        super.onSizeChanged(w, h, oldw, oldh);
        Log.e("log", "onSizeChanged");
    }


    @Override
    protected void onLayout(boolean changed, int l, int t, int r, int b) {
        super.onLayout(changed, l, t, r, b);
        Log.e("log", "onLayout");

    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        Log.e("log", "onDraw");
    }


    @Override
    public void onWindowFocusChanged(boolean hasWindowFocus) {
        super.onWindowFocusChanged(hasWindowFocus);
        Log.e("log", "onWindowFocusChanged" + "  " + hasWindowFocus);

    }
}

 


 

  1. 上一頁:
  2. 下一頁:
熱門文章
閱讀排行版
Copyright © Android教程網 All Rights Reserved