简体   繁体   English

Android FloatingActionButton“ onclick”不起作用

[英]Android FloatingActionButton “onclick” does not work

I'm new to AndroidStudio and Java. 我是AndroidStudio和Java的新手。 I tried to use a FloatingActionButton to open another activity in my App but it does not work.. Here is the code of the Button in the XML file (the button itself works, I just have problems to link it correctly to the other activity): 我试图使用FloatingActionButton在我的应用程序中打开另一个活动,但是它不起作用。这是XML文件中Button的代码(按钮本身可以工作,我很难将其正确链接到另一个活动) :

<android.support.design.widget.FloatingActionButton
    android:id="@+id/FAB"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_gravity="bottom|right"
    android:layout_margin="16dp"
    android:onClick="openInputWindow"
    android:clickable="true"
    android:src="@drawable/ic_add_black_36dp"
    app:layout_anchorGravity="bottom|right|end" />

Code in the Java file: Java文件中的代码:

public class MainActivity extends AppCompatActivity {

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

public void openInputWindow (View view) {
    Intent intent = new Intent(this, AddElementActivity.class);
    startActivity(intent);

Many thanks for your help :) 非常感谢您的帮助 :)

You said the button works but just does not redirect to the activity. 您说该按钮有效,但不能重定向到该活动。 This implies that your activity cannot be found (Check your logcat) 这意味着找不到您的活动(请检查您的logcat)

Make sure the activity you want to navigate to is listed in the Manifest.xml (it's not important to add an intent filter) 确保您要导航到的活动已在Manifest.xml中列出(添加意图过滤器并不重要)

<activity android:name=".AddElementActivity"
          android:label="@string/app_name" />

I don't know how your complete xml looks like. 我不知道您的完整xml看起来如何。

But FloatingActionButton in the layout xml should be placed at the top to get touch events. 但是应将布局xml中的FloatingActionButton放在顶部,以获取触摸事件。

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

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