Android:FloatingActionButton单击事件不起作用

时间:2016-09-29 09:40:36

标签: java android xml

我一直在实施FloatingActionButton但不知何故一直没有做出回应。没有错误,没有任何反应。

以下是Javaxml代码:

我已使用Log Event

替换Click事件中的代码
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中

<?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

<?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>

3 个答案:

答案 0 :(得分:2)

让我们将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>

首先检查没有内容的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"
    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>

因此,您的网格视图必须占用所有点击直通,让我检查并再次返回。

<强> [编辑] 毕竟它不是GridView。代码中有一条不必要的行。 Voila发现了您的问题,请在onPostExecute() ParseActivity

上删除此行
MainActivity.this.setContentView(R.layout.activity_main);

可能在第286行附近。

这会再次设置布局,并且不会注册on click侦听器,这就是为什么当它在布局文件(DownloadZip)中时,它正在工作。

如果有必要,请重新启动布局,即找到fab并放入另一个setOnClickListener。您可以将这些代码放在方法中,并在需要重新布局的位置调用它们。

答案 1 :(得分:1)

将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>

答案 2 :(得分:0)

代码是对的。您确定要覆盖

public void onCreate(Bundle savedInstanceState, PersistableBundle persistentState)

而不是

@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..."); } }); }

示例:

        MbMessage outMessage = new MbMessage();
        //copy message headers if required 
        MbElement oRoot = outMessage.getRootElement();  
        MbElement oBody = oRoot.createElementAsLastChild(MbBLOB.PARSER_NAME);  
        oBody.createElementAsLastChild(MbElement.TYPE_NAME_VALUE, "BLOB", yourXmlAsByteArray);
        MbMessageAssembly outAssembly = new MbMessageAssembly(inAssembly, inAssembly.getLocalEnvironment(), inAssembly.getExceptionList(), outMessage);