我只需要处理TextView的长按事件(并在此事件上显示popup菜单)。但在简单点击的情况下,我应该将此事件转移到托管此视图的布局。如何不在同一时间处理View.onClick()并处理View.onLongClick()?
如何实现这一目标?
public class MainActivity extends Activity implements OnLongClickListener, OnClickListener {
TextView mTextView;
RelativeLayout mTopLayout;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mTextView = (TextView) findViewById(R.id.mTextView);
mTopLayout = (RelativeLayout) findViewById(R.id.mTopLayout);
mTextView.setOnLongClickListener(this);
mTopLayout.setOnClickListener(this);
mTextView.setOnClickListener(this);
}
@Override
public void onClick(View v) {
Toast.makeText(this, v.getClass().getName() + " clicked!", Toast.LENGTH_SHORT).show();
if (v.getId() == R.id.mTextView) {
((View) v.getParent()).performClick();
}
}
...
}
这样我们就会收到两个连续的点击事件,并且在android中会有两个标准的点击声音。不好。
是否有一些简单的方法来实现这一目标?
这里是XML:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/mTopLayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity" >
<TextView
android:id="@+id/mTextView"
android:layout_width="match_parent"
android:layout_height="200dp"
android:background="#FFFF0000"
android:clickable="false"
android:focusable="false"
android:text="@string/hello_world" />
</RelativeLayout>
编辑: 我想补充细节。如果我不会设置onClickListener,那么在TextView中进行截图没有任何效果,并且此事件被mTextView消耗(即使可点击和可聚焦在xml中设置为false)并且不会传递给mTopLayout
可能是GestureDetecter在这里有帮助吗? – Prizoff 2013-03-26 13:25:20
我正在做类似的事情! http://stackoverflow.com/questions/16621070/get-context-of-popupmenu-like-contextmenu – toobsco42 2013-05-18 16:07:19