2016-11-25 79 views
2

我有一個浮動操作按鈕,我打算在片段上顯示一個活動。當波紋可見時,FAB不會響應點擊。 我搜索了類似的問題,但我發現的答案沒有奏效。浮動操作按鈕沒有響應點擊

這是我的FAB佈局。

<android.support.design.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android" 
xmlns:app="http://schemas.android.com/apk/res-auto" 
android:layout_width="match_parent" 
android:layout_height="match_parent" 
app:theme="@style/MyTheme"> 

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:id="@+id/MainActivityFragmentContainer" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent"> 

</FrameLayout> 

<android.support.design.widget.FloatingActionButton 
    android:id="@+id/homeFAB" 
    android:layout_width="65dp" 
    android:layout_height="65dp" 
    android:layout_gravity="bottom|center" 
    android:layout_margin="16dp" 
    android:src="@drawable/ic_add_image" 
    android:onClick="runThis" 
    app:layout_anchor="@id/MainActivityFragmentContainer" 
    app:layout_anchorGravity="bottom|center" /> 

我重新排列按鈕的順序在協調佈局內的佈局,但沒有奏效。 然後我將android:onclick="runThis"添加到XML中的FAB和活動中的方法。

public void runThis(View view) { 
    Toast.makeText(SlidingMenuActivity.this, "CLICKED", Toast.LENGTH_LONG).show(); 
} 

獲得以下錯誤:

java.lang.IllegalArgumentException: Expected receiver of type xxx.xxx.SlidingMenuActivity, but got android.support.v7.internal.view.ContextThemeWrapper

請幫我找出我錯過了什麼。 謝謝

+0

嘗試view.getContext()代替SlidingMenuActivity.this – poss

+0

@poss好吧,我想試試,讓你知道會發生什麼 – Zet

+1

相關谷歌代碼討論[這裏](https://code.google。 COM/p/android/issues/detail?id = 174871) –

回答

1

請用下面的代碼嘗試,而不是SlidingMenuActivitygetApplicationContext()。它應該工作。

FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab); 
    fab.setOnClickListener(new View.OnClickListener() { 
     @Override 
     public void onClick(View view) { 
      Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG) 
        .setAction("Action", null).show(); 
     } 
    }); 
1

您正在使用的片段,你應該使用點擊,而不是在類似這樣的XML定義的onClick監聽.... 膨脹晶圓廠上的onCreate視圖...

FloatingActionButton myFab = (FloatingActionButton) myView.findViewById(R.id.homeFAB); 
myFab.setOnClickListener(new View.OnClickListener() { 
    public void onClick(View v) { 
     doyourThing(); 
    } 
}); 

或 更換SlidingActivity.this與getActivity()

+0

我認爲他真的使用Activity,但這應該顯然工作。 –