我有这个简单的代码:
public class MainActivity extends ActionBarActivity {
public TextView textView;
StringBuilder builder = new StringBuilder();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
textView = (TextView) findViewById(R.id.touch_textView);
textView.setText("Hello");
textView.setOnTouchListener(new View.OnTouchListener() {
String TAG = "onTouchListener";
@Override
public boolean onTouch(View v, MotionEvent event) {
Log.d(TAG,"Triggered");
// TODO Auto-generated method stub
switch(event.getAction())
{
case MotionEvent.ACTION_DOWN:
Log.d(TAG,"Down");
builder.append("down, ");
break;
case MotionEvent.ACTION_MOVE:
builder.append("move, ");
Log.d(TAG,"Move");
break;
case MotionEvent.ACTION_CANCEL:
builder.append("cancel, ");
Log.d(TAG,"Cancel");
break;
case MotionEvent.ACTION_UP:
builder.append("up, ");
Log.d(TAG,"Up");
break;
}
builder.append(event.getX());
builder.append(", ");
builder.append(event.getY());
String text = builder.toString();
Log.d("TouchTest", text);
textView.setText(text);
return true;
}
});
}
}
......这个简单的布局:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="com.example.touchhandling.MainActivity">
<TextView
android:id="@+id/touch_textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
</RelativeLayout>
我的问题是我的TextView的onTouchListener没有收到触摸事件,而是我在Logcat中看到了这个:
ViewRoot的触摸事件:ACTION_UP
似乎我的FrameLayout正在窃取触摸事件。我怎样才能做到这一点?孩子应该先参加活动吗?
谢谢,任何提示都表示赞赏!
答案 0 :(得分:0)
尝试textView.setClickable(true)
。
答案 1 :(得分:0)
public class TestMainActivity extends ActionBarActivity
{
public TextView textView;
StringBuilder builder = new StringBuilder();
Context context;
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
context = this;
if (savedInstanceState == null)
{
getSupportFragmentManager().beginTransaction().add(R.id.container, new PlaceholderFragment()).commit();
}
}
@Override
public boolean onCreateOptionsMenu(Menu menu)
{
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item)
{
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
if (id == R.id.action_settings)
{
return true;
}
return super.onOptionsItemSelected(item);
}
/**
* A placeholder fragment containing a simple view.
*/
public static class PlaceholderFragment extends Fragment
{
public TextView textView;
StringBuilder builder = new StringBuilder();
Context context;
public PlaceholderFragment()
{
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
{
context = this.getActivity();
View rootView = inflater.inflate(R.layout.fragment_main, container, false);
textView = (TextView) rootView.findViewById(R.id.touch_textView);
textView.setText("Hello");
// context = this;
textView.setOnTouchListener(new View.OnTouchListener()
{
String TAG = "onTouchListener";
@Override
public boolean onTouch(View v, MotionEvent event)
{
Log.d(TAG, "Triggered");
Toast.makeText(context, "Triggered textView", "Triggered textView".length()).show();
// TODO Auto-generated method stub
switch (event.getAction())
{
case MotionEvent.ACTION_DOWN:
Log.d(TAG, "Down");
builder.append("down, ");
break;
case MotionEvent.ACTION_MOVE:
builder.append("move, ");
Log.d(TAG, "Move");
break;
case MotionEvent.ACTION_CANCEL:
builder.append("cancel, ");
Log.d(TAG, "Cancel");
break;
case MotionEvent.ACTION_UP:
builder.append("up, ");
Log.d(TAG, "Up");
break;
}
builder.append(event.getX());
builder.append(", ");
builder.append(event.getY());
String text = builder.toString();
Log.d("TouchTest", text);
textView.setText(text);
return true;
}
});
return rootView;
}
}
}
和fragment_layout.xml:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/touch_parent"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="com.example.stacktest.MainActivity$PlaceholderFragment" >
<TextView
android:id="@+id/touch_textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/hello_world" />
</RelativeLayout>