【问题标题】:LongClickListener not workingLongClickListener 不工作
【发布时间】: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


【解决方案1】:

代替:

View viev = getLayoutInflater().inflate(R.layout.fragment_page1, lay, false);

试试这个:

View viev = LayoutInflater.from(this).inflate(R.layout.fragment_page1, lay, false);

【讨论】:

  • 日志是否在这一行打印了Log.i("OK","Listener set on button : " + IDs[i]);
  • yes ;I/OK:监听器设置在按钮上:2131624091 I/OK:监听器设置在按钮上:2131624092 I/OK:监听器设置在按钮上:2131624093 I/OK:监听器设置在按钮上:2131624094 I/OK:监听器设置在按钮上:2131624095 I/OK:监听器设置在按钮上:2131624096
【解决方案2】:

试试这个;

@Override
public boolean onLongClick(View v) {
    Log.e("ID", ""+v.getId());
    return true;
}

然后,检查这里的视图 ID 是否与按钮中的一个相同。

【讨论】:

  • 我在onLongClick中写了Toast消息,但是当我长按时,没有消息显示
  • 我在想,为什么你使用'Button button = (Button) viev.findViewById(IDs[i]);'按钮是不是太多了?尝试首先将 'onLongClickListener()' 设置为单个按钮,其 id 为 'Button button = (Button) viev.findViewById(R.id.yourButtonId);'
  • @Journy,我的意思是,onLongClick 不起作用,不要调用
  • 不不,我想说的是,只需将 OnLongClickListener 设置为使用 .xml(layout) 文件中分配的相同 ID 创建和解析的单个按钮。我的意思是说,检查数组'IDs[]'是否有问题。
  • IDs 数组为真,如果 id 以“button”开头,则设置为 R.id 文件夹中的 id。
猜你喜欢
  • 1970-01-01
  • 2016-11-27
  • 1970-01-01
  • 2010-11-02
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多