【发布时间】:2013-03-26 13:17:50
【问题描述】:
我只需要处理 TextView 的长按事件(并在此事件上显示弹出菜单)。但如果只是简单的点击,我应该将此事件转移到托管此视图的布局。
如何做到这一点?
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 中的 clcking 没有任何效果,并且此事件由 mTextView 消耗(即使在 xml 中可单击和可聚焦设置为 false)并且不会传递给 mTopLayout
【问题讨论】:
-
GestureDetecter 可能会在这里提供帮助?
-
我在这里做类似的事情!!! stackoverflow.com/questions/16621070/…
标签: java android onclick onlongclicklistener