閱讀127 返回首頁    go 阿裏雲 go 技術社區[雲棲]


android View的一些學習記錄

1,View是所有控件的父類

2,View可以關聯很多個listenner。就像一個女孩有很多個男人對她好一樣。有為她賣苦力的窮叼絲,也有她拿得出手的高副帥。當女孩發生需要搬宿舍的事件的時候,這個時候窮叼絲監聽到了該事件的發生,於是幫搬宿舍,當她需要去看電影和開房的時候,高副帥男友監聽到了該事件,於是就跟她完成了一個很美好的夜晚。


View 的事件監聽,舉個例子


package com.example.listviewitem.widgets;
 
import android.content.Context;
import android.graphics.Canvas;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.View;
 
/**
 * 在自定義的View中定義三個監聽器
 */
public class MyView extends View {
 
    private OnDownActionListener mDown = null;
    private OnMoveActionListener mMove = null;
    private OnUpActionListener mUp = null;
 
    public MyView(Context context) {
        super(context);
    }
 
    public MyView(Context context, AttributeSet attrs) {
        super(context, attrs);
        // TODO Auto-generated constructor stub
    }
 
    @Override
    protected void onDraw(Canvas canvas) {
        // TODO Auto-generated method stub
        super.onDraw(canvas);
    }
 
    @Override
    public boolean onTouchEvent(MotionEvent event) {
        // TODO Auto-generated method stub
        int x, y;
        if (event.getAction() == MotionEvent.ACTION_DOWN) {
            x = (int) event.getX();
            y = (int) event.getY();
            if (mDown != null) {
                mDown.OnDown(x, y);
            }
            return true; // 隻有返回true這個控件的move和up才會響應
        } else if (event.getAction() == MotionEvent.ACTION_MOVE) {
            x = (int) event.getX();
            y = (int) event.getY();
            if (mMove != null) {
                mMove.OnMove(x, y);
            }
        } else if (event.getAction() == MotionEvent.ACTION_UP) {
            x = (int) event.getX();
            y = (int) event.getY();
            if (mUp != null) {
                mUp.OnUp(x, y);
            }
        }
        return super.onTouchEvent(event);
    }
 
    // 為每個接口設置監聽器
    public void setOnDownActionListener(OnDownActionListener down) {
        mDown = down;
    }
 
    public void setOnMoveActionListener(OnMoveActionListener move) {
        mMove = move;
    }
 
    public void setOnUpActionListener(OnUpActionListener up) {
        mUp = up;
    }
 
    // 定義三個接口
    public interface OnDownActionListener {
        public void OnDown(int x, int y);
    }
 
    public interface OnMoveActionListener {
        public void OnMove(int x, int y);
    }
 
    public interface OnUpActionListener {
        public void OnUp(int x, int y);
    }
}

然後在layout的xml文件中加入該自定義控件

<!--?xml version="1.0" encoding="utf-8"?-->
<linearlayout xmlns:andro android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical">
 
    <com.example.listviewitem.widgets.myview android: android:layout_width="wrap_content" android:layout_height="wrap_content" android:background="@drawable/area_point_bg">
 
</com.example.listviewitem.widgets.myview></linearlayout>

然後用一個activity來用它

package com.example.listviewitem;
 
import com.example.listviewitem.widgets.MyView;
import com.example.listviewitem.widgets.MyView.OnDownActionListener;
import com.example.listviewitem.widgets.MyView.OnMoveActionListener;
import com.example.listviewitem.widgets.MyView.OnUpActionListener;
 
import android.app.Activity;
import android.os.Bundle;
 
public class TestListener extends Activity {
 
    private MyView view;
 
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
        setContentView(R.layout.listener);
        view = (MyView) findViewById(R.id.my_view);
        view.setOnDownActionListener(new OnDownActionListener() {
 
            @Override
            public void OnDown(int x, int y) {
                // TODO Auto-generated method stub
                System.out.println("down x = " + x + " y = " + y);
            }
        });
 
        view.setOnMoveActionListener(new OnMoveActionListener() {
 
            @Override
            public void OnMove(int x, int y) {
                // TODO Auto-generated method stub
                System.out.println("move x = " + x + " y = " + y);
            }
        });
 
        view.setOnUpActionListener(new OnUpActionListener() {
 
            @Override
            public void OnUp(int x, int y) {
                // TODO Auto-generated method stub
                System.out.println("up x = " + x + " y = " + y);
            }
        });
    }
}


這樣就完成了一個自定義控件的時間監聽。














最後更新:2017-04-03 05:39:57

  上一篇:go 【#define PI acos(-1.0)】【非凸包】poj 2365 Rope
  下一篇:go yarn架構 及 client提交任務過程講解