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;
}
}