简体   繁体   English

Android:FloatingActionButton Click事件不起作用

[英]Android: FloatingActionButton Click Event not working

I have been implementing a FloatingActionButton but somehow hasn't been responding at all. 我一直在实现FloatingActionButton但是某种程度上根本没有响应。 There's no error, nothing happens. 没有错误,没有任何反应。

Here is respective Java and xml code: 这是各自的Javaxml代码:

I have replaced the code in Click event with a Log Event 我已将Log Event的代码替换为Log Event

FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
fab.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View view) {
        Log.e("ABCD","ABCD");
    }
});

activity_main.xml activity_main.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"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true"
tools:context="edu.ahduni.seas.gyapak.MainActivity">

<android.support.design.widget.AppBarLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:theme="@style/AppTheme.AppBarOverlay">

    <android.support.v7.widget.Toolbar
        android:id="@+id/toolbar"
        android:layout_width="match_parent"
        android:layout_height="?attr/actionBarSize"
        android:background="?attr/colorPrimary"
        app:popupTheme="@style/AppTheme.PopupOverlay" />
</android.support.design.widget.AppBarLayout>

<include layout="@layout/content_main" />

<android.support.design.widget.FloatingActionButton
    android:id="@+id/fab"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_gravity="bottom|end"
    android:layout_margin="@dimen/fab_margin"
    android:clickable="true"
    android:src="@android:drawable/ic_dialog_email" />

</android.support.design.widget.CoordinatorLayout>

content_main.xml content_main.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:id="@+id/textlayout"
tools:context="edu.ahduni.seas.gyapak.MainActivity">

    <GridView xmlns:android="http://schemas.android.com/apk/res/android"
        android:id="@+id/grid"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:columnWidth="90dp"
        android:numColumns="auto_fit"
        android:verticalSpacing="10dp"
        android:horizontalSpacing="10dp"
        android:stretchMode="columnWidth"
        android:gravity="center"
        android:padding="10dp" />
</LinearLayout>

Lets put the Toolbar and AppbarLayout below the fab , and see what happens: 让我们将ToolbarAppbarLayout放置在fab下方,看看会发生什么:

<?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"
        android:id="@+id/root"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:fitsSystemWindows="true"
        android:orientation="vertical">

        <LinearLayout
            android:id="@+id/content"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:orientation="vertical"
            app:layout_behavior="@string/appbar_scrolling_view_behavior">
                <!-- include  your layout here -->
        </LinearLayout>

        <!-- when the screen is loading, in your case optional -->
        <LinearLayout
            android:id="@+id/progress"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:orientation="vertical"
            android:visibility="gone">
            <include layout="@layout/layout_loading"></include>
        </LinearLayout>

        <!-- when the screen is empty no data, in your case optional -->
        <LinearLayout
            android:id="@+id/empty"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:orientation="vertical"
            android:visibility="gone">
            <include layout="@layout/layout_empty"></include>
        </LinearLayout>

        <!-- a fab -->
        <android.support.design.widget.FloatingActionButton
            android:id="@+id/fab"
            app:backgroundTint="#009688"
            style="@style/FabStyle"
            android:layout_gravity="bottom|end"
            android:layout_marginBottom="20dp"
            android:visibility="invisible" />

        <!-- the toolbar layout -->
        <android.support.design.widget.AppBarLayout
            android:id="@+id/app_bar_layout"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar">

            <android.support.v7.widget.Toolbar
                android:id="@+id/toolbar"
                android:layout_width="match_parent"
                android:layout_height="?attr/actionBarSize"
                android:title="@string/app_name"
                app:layout_collapseMode="pin"
                app:popupTheme="@style/ThemeOverlay.AppCompat.Dark" />

        </android.support.design.widget.AppBarLayout>
    </android.support.design.widget.CoordinatorLayout>

First check the fab click without the content, then try it with the content in content linear layout. 首先检查没有内容的晶圆厂单击,然后以内容线性布局的内容进行尝试。

I modified your layout to this (removed all the content) and the click is working 我将您的布局修改为此(删除了所有内容),并且点击有效

<?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"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:fitsSystemWindows="true">



    <!--<include layout="@layout/content_main" />-->

    <android.support.design.widget.FloatingActionButton
        android:id="@+id/fab"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_margin="20dp"
        android:layout_gravity="bottom|end"
        android:clickable="true"
        android:src="@android:drawable/ic_dialog_email"
        app:backgroundTint="#FF0000"
        app:rippleColor="#FFF" />



    <android.support.design.widget.AppBarLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content">

        <android.support.v7.widget.Toolbar
            android:id="@+id/toolbar"
            android:layout_width="match_parent"
            android:layout_height="?attr/actionBarSize"
            android:background="?attr/colorPrimary" />

    </android.support.design.widget.AppBarLayout>

</android.support.design.widget.CoordinatorLayout>

So your grid view must be taking all the click passthroughs, let me check and get back again. 因此,您的网格视图必须使用所有的点击传递,让我检查一下并再次返回。

[Edit] It is not GridView after all. [编辑]毕竟不是GridView There was one unnecessary line in the code. 代码中没有多余的一行。 Voila found your problem, remove this line on your onPostExecute() of ParseActivity 瞧发现你的问题,解除您此行onPostExecute()ParseActivity

MainActivity.this.setContentView(R.layout.activity_main);

near Line number 286 probably. 可能在286号线附近。

this sets the layout again and doesn't register the on click listener, thats why when it was in the layout file (DownloadZip), it was working. 这将再次设置布局,并且不注册单击侦听器,这就是为什么当它位于布局文件(DownloadZip)中时可以正常工作的原因。

If it is necessary for you then reinit the layout ie find the fab and put another setOnClickListener. 如果有必要,请重新初始化布局,即找到fab并放置另一个setOnClickListener。 you could put those code in a method and call them on the places you need to reinit layout. 您可以将这些代码放入方法中,然后在需要重新初始化布局的位置上调用它们。

Replace the code of activity_main with these : 用以下代码替换activity_main的代码:

  <?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"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:fitsSystemWindows="true">

        <android.support.design.widget.AppBarLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            >

            <android.support.v7.widget.Toolbar
                android:id="@+id/toolbar"
                android:layout_width="match_parent"
                android:layout_height="?attr/actionBarSize"
                android:background="?attr/colorPrimary"></android.support.v7.widget.Toolbar>

        </android.support.design.widget.AppBarLayout>

        <include layout="@layout/content_main" />

        <android.support.design.widget.FloatingActionButton
            android:id="@+id/fab"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="bottom|end"
            android:layout_margin="20dp"
            android:src="@android:drawable/ic_dialog_email" />

    </android.support.design.widget.CoordinatorLayout>

The code is right. 该代码是正确的。 Are you sure that you are override 您确定自己被覆盖了吗

protected void onCreate(@Nullable Bundle savedInstanceState)

instead of 代替

public void onCreate(Bundle savedInstanceState, PersistableBundle persistentState)

?

Example: 例:

@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_incoming_call);

    FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fabCall);
    fab.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Log.d("mylog" , "it happens...");
        }
    });
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM