简体   繁体   English

如何设置OnClickListener(Android)

[英]How To Set OnClickListener (Android)

I know this is a very simple and maybe stupid question but I am having trouble putting an OnClickListener for my Floating Action Button. 我知道这是一个非常简单但也许是愚蠢的问题,但是我在为浮动动作按钮添加OnClickListener时遇到了麻烦。 I have never done one for a button I did not make myself (Using a library) and I just need some help. 我从未做过一个按钮,我没有自己制作(使用图书馆),我只需要一些帮助。 Here is the code that I am using: https://gist.github.com/VirusThePanda/20320afd303150c02ea5 这是我正在使用的代码: https//gist.github.com/VirusThePanda/20320afd303150c02ea5

Here is the library that I am using for the button, and the source code I am using for the navigation bar. 这是我用于按钮的库,以及我用于导航栏的源代码。 This might come in handy as it has a placeholder fragment that may be attached to the main activity. 这可能会派上用场,因为它有一个可以附加到主要活动的占位符片段。

Source: android-toolbar-with-drawer (From GitHub) Library: android-floating-action-button (From GitHub) 来源:android-toolbar-with-drawer(来自GitHub)库:android-floating-action-button(来自GitHub)

Again, I am sorry for the very simple question. 我再次对这个非常简单的问题感到抱歉。 I am still learning Java and XML. 我还在学习Java和XML。 Thanks for any help :) 谢谢你的帮助 :)

Try this : 试试这个 :

<com.getbase.floatingactionbutton.FloatingActionsMenu
        android:id="@+id/multiple_actions"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="bottom|end"
        android:onClick="fabClicked"
        fab:fab_addButtonColorNormal="#ec407a"
        fab:fab_addButtonColorPressed="#d81b60"
        fab:fab_labelStyle="@style/menu_labels_style"
        android:layout_marginBottom="16dp"
        android:layout_marginRight="16dp"
        android:layout_marginEnd="16dp"/>

Then, in your activity file (ie. in class custom_motivation), define the function fabClicked as : 然后,在您的活动文件中(即在类custom_motivation中),将函数fabClicked定义为:

...
public void fabClicked(View v){
  // write your code here ..
}
...

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

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