【发布时间】:2017-02-14 09:51:33
【问题描述】:
我想为片段中的每个按钮添加 longClickListener。为此,我编写了这段代码,我在 OnCreate() 中调用它。我的项目是选项卡式活动。
我在主要活动中实现了 OnLongClickListener。
public class MainActivity extends AppCompatActivity implements View.OnLongClickListener
我在 MainActivity.java 中也有 onLongClick 方法
@Override
public boolean onLongClick(View v) {.......}
这是我在 onCreate() 中的方法;
public void InflateFragments(){
final RelativeLayout lay = (RelativeLayout) findViewById(R.id.Page1Lay);
View viev = getLayoutInflater().inflate(R.layout.fragment_page1, lay, false);
for(int i = 0; i < IDs.length; i++){
Button button = (Button) viev.findViewById(IDs[i]);
if(button != null)
button.setOnLongClickListener(this);
Log.i("OK","Listener set on button : " + IDs[i]);
}
}
IDs,是一个包含我的按钮 id 的 int 数组。此代码可以访问我的片段中的每个按钮并为每个按钮设置 OnLongClickListener,因为没有异常或任何错误。我可以设置 Log.i()控制台上的消息。
但是当我在虚拟设备上运行这段代码时,当我长按时没有任何反应。我不知道为什么。
我的按钮在布局中,我的每个按钮都有;
android:longClickable="true"
我的 main_activity.xml ;
<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
xmlns:ads="http://schemas.android.com/apk/res-auto"
android:id="@+id/main_content"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true"
tools:context="com.example.aslan.rte3.MainActivity">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:weightSum="1">
<android.support.design.widget.AppBarLayout
android:id="@+id/appbar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingTop="@dimen/appbar_padding_top"
android:theme="@style/AppTheme.AppBarOverlay"
android:background="@android:color/black">
<android.support.design.widget.TabLayout
android:id="@+id/tabs"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:tabGravity="center"
app:tabMode="scrollable" />
</android.support.design.widget.AppBarLayout>
<android.support.v4.view.ViewPager
android:id="@+id/container"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
app:layout_behavior="@string/appbar_scrolling_view_behavior"
android:background="#E0DFDE">
</android.support.v4.view.ViewPager>
<FrameLayout
android:layout_width="match_parent"
android:layout_height="50dp"
android:background="#E0DFDE">
<com.google.android.gms.ads.AdView
android:id="@+id/adView"
android:layout_width="match_parent"
android:layout_height="match_parent"
ads:adSize="SMART_BANNER"
ads:adUnitId="..."
android:layout_alignParentBottom="true"
android:layout_alignParentStart="true"
android:layout_alignParentLeft="true" />
</FrameLayout>
</LinearLayout>
</android.support.design.widget.CoordinatorLayout>
【问题讨论】:
-
尝试使用 button.setOnLongClickListener(getActivity());
-
您是否尝试过在 onLongClick() 中输入日志并查看它是否已触发?
-
可以添加
activity_main.xml的内容吗? -
@AritraRoy 我试过了,我看到了 :)
-
@raktale 我无法调用 getActivity()。我怎么称呼它?
标签: android button onclicklistener onlongclicklistener