android事件分析(三)——在分析的時候用的代碼

在分析這個Touch事件的時候,用的代碼附在這裏,希望能給大家幫助。

MainActivity:

package cn.yj3g.TL24_Event_test;

import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

public class TL24_Event_testActivity extends Activity {
    
	private LinearLayout outLayout;
	private LinearLayout innerLayout;
	private ImageView imageView;
	private TextView textView;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        outLayout = (LinearLayout) findViewById(R.id.out_layout);
        
        innerLayout = (LinearLayout) findViewById(R.id.inner_layout);
        imageView = (ImageView) findViewById(R.id.i_view);
        textView = (TextView) findViewById(R.id.t_view);
        textView.setText(R.string.text2);
        
        outLayout.setOnTouchListener(new View.OnTouchListener() {
			
			public boolean onTouch(View v, MotionEvent event) {
				int action = event.getAction();
				switch (action) {
				case MotionEvent.ACTION_DOWN:
					Log.v("TAG", "outLayout down");
					break;
				case MotionEvent.ACTION_MOVE:
					Log.v("TAG", "outLayout move");
					break;
				case MotionEvent.ACTION_UP:
					Log.v("TAG", "outLayout up");
					break;

				default:
					break;
				}
				return false;
			}
		});
        
        innerLayout.setOnTouchListener(new View.OnTouchListener() {
			
			public boolean onTouch(View v, MotionEvent event) {
				int action = event.getAction();
				switch (action) {
				case MotionEvent.ACTION_DOWN:
					Log.v("TAG", "innerLayout down");
					//return true;
					break;
				case MotionEvent.ACTION_MOVE:
					Log.v("TAG", "innerLayout move");
					//return true;
					break;
				case MotionEvent.ACTION_UP:
					Log.v("TAG", "innerLayout up");
					break;

				default:
					break;
				}
				return false;
			}
		});
        
        imageView.setOnTouchListener(new View.OnTouchListener() {
			
			public boolean onTouch(View v, MotionEvent event) {
				int action = event.getAction();
				switch (action) {
				case MotionEvent.ACTION_DOWN:
					Log.v("TAG", "imageView down");
					Log.v("TAG", "imageView x="+event.getX()+" rawX="+event.getRawX());
					//return true; //消費了
					break;
				case MotionEvent.ACTION_MOVE:
					Log.v("TAG", "imageView move");
					break;
				case MotionEvent.ACTION_UP:
					Log.v("TAG", "imageView up");
					break;
					//return true;
				default:
					break;
				}
				return false;
			}
		});
        
        textView.setOnTouchListener(new View.OnTouchListener() {
			
			public boolean onTouch(View v, MotionEvent event) {
				int action = event.getAction();
				switch (action) {
				case MotionEvent.ACTION_DOWN:
					Log.v("TAG", "textView down");
					break;
				case MotionEvent.ACTION_MOVE:
					Log.v("TAG", "textView move");
					break;
				case MotionEvent.ACTION_UP:
					Log.v("TAG", "textView up");
					//return true;
					break;

				default:
					break;
				}
				return false;
			}
		});      
    }

    
    @Override
    public boolean dispatchTouchEvent(MotionEvent ev) {
    	int action = ev.getAction();
		switch (action) {
		case MotionEvent.ACTION_DOWN:
			Log.v("TAG", "activity dispatch down");
			return true;
			//break;
		case MotionEvent.ACTION_MOVE:
			Log.v("TAG", "activity dispatch move");
			break;
		case MotionEvent.ACTION_UP:
			Log.v("TAG", "activity dispatch up");
			break;

		default:
			break;
		}
    	return super.dispatchTouchEvent(ev);
    }
    
	@Override
	public boolean onTouchEvent(MotionEvent event) {
		
		int action = event.getAction();
		switch (action) {
		case MotionEvent.ACTION_DOWN:
			Log.v("TAG", "activity down");
			break;
		case MotionEvent.ACTION_MOVE:
			Log.v("TAG", "activity move");
			break;
		case MotionEvent.ACTION_UP:
			Log.v("TAG", "activity up");
			break;

		default:
			break;
		}
		return super.onTouchEvent(event);
	}
}

附上代碼下載地址(CSDN資源,不要積分):

/*我個乖乖,上傳上去了,N久不顯示。看不到下載鏈接,下次再補上吧。。。


補上下載鏈接:http://download.csdn.net/detail/badboy1110/3635521

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章