Event


import java.util.ArrayList;
import java.util.List;

import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnTouchListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ListView;
import android.widget.ScrollView;
import android.widget.TextView;
import android.widget.Toast;

public class MainActivity extends Activity {

    ScrollView sv;
    ListView lv;
    List<String> list;
    Button btn;
    MyTextView tv;

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

        btn = (Button) findViewById(R.id.btn);
        sv = (ScrollView) findViewById(R.id.sv);
        lv = (ListView) findViewById(R.id.lv);
        tv = (MyTextView) findViewById(R.id.tv);
        list = new ArrayList<String>();
        for (int i = 0; i < 20; i++) {
            list.add("條目" + i);
        }
        ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
                android.R.layout.simple_list_item_1, list);
        lv.setAdapter(adapter);

        btn.setOnTouchListener(new OnTouchListener() {

            @Override
            public boolean onTouch(View v, MotionEvent event) {

                return false;
            }
        });

        lv.setOnTouchListener(new OnTouchListener() {

            @Override
            public boolean onTouch(View v, MotionEvent event) {
                // TODO Auto-generated method stub
                // true:父控件不攔截事件
                v.getParent().requestDisallowInterceptTouchEvent(true);
                return false;
            }
        });

//        tv.setOnTouchListener(new OnTouchListener() {
//
//            @Override
//            public boolean onTouch(View v, MotionEvent event) {
//                switch (event.getAction()) {
//                case MotionEvent.ACTION_DOWN:
//
//                    break;
//                case MotionEvent.ACTION_UP:
//                    Toast.makeText(MainActivity.this, "tv被點擊了", 0).show();
//                    break;
//                case MotionEvent.ACTION_MOVE:
//
//                    break;
//
//                default:
//                    break;
//                }
//                return true;
//            }
//        });
        
        sv.setOnTouchListener(new OnTouchListener() {
            
            @Override
            public boolean onTouch(View v, MotionEvent event) {
                switch (event.getAction()) {
                case MotionEvent.ACTION_DOWN:

                    break;
                case MotionEvent.ACTION_UP:
                    Toast.makeText(MainActivity.this, "sv被點擊了", 0).show();
                    break;
                case MotionEvent.ACTION_MOVE:

                    break;

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

}

=============================================================


import android.annotation.SuppressLint;
import android.content.Context;
import android.util.AttributeSet;
import android.util.Log;
import android.view.MotionEvent;
import android.widget.TextView;
import android.widget.Toast;

@SuppressLint("NewApi") public class MyTextView extends TextView{
    
    Context context;

    public MyTextView(Context context) {
        super(context);
        this.context = context;
        // TODO Auto-generated constructor stub
    }
    

    public MyTextView(Context context, AttributeSet attrs, int defStyleAttr,
            int defStyleRes) {
        super(context, attrs, defStyleAttr, defStyleRes);
        // TODO Auto-generated constructor stub
    }



    public MyTextView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        // TODO Auto-generated constructor stub
    }



    public MyTextView(Context context, AttributeSet attrs) {
        super(context, attrs);
        // TODO Auto-generated constructor stub
    }



    @Override
    public boolean onTouchEvent(MotionEvent event) {
        
        switch (event.getAction()) {
        case MotionEvent.ACTION_DOWN:

            break;
        case MotionEvent.ACTION_UP:
            Log.i("=========================", "mtv被點擊了");
            break;
        case MotionEvent.ACTION_MOVE:

            break;

        default:
            break;
        }
        return false;
        
    }
}



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