Floating Action Button does not raise OnClick event

假如想象 提交于 2019-12-06 05:39:55

问题


I have the Following layout:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:paddingBottom="@dimen/activity_vertical_margin"
android:id="@+id/rlMap">
    <org.mapsforge.map.android.view.MapView
        android:id="@+id/mapView"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:clickable="true"/>
    <ImageView
        android:layout_width="50dp"
        android:layout_height="50dp"
        android:id="@+id/windArrow"
        android:layout_alignParentTop="true"
        android:layout_alignParentEnd="true"
        android:layout_gravity="top|right|end"
        android:src="@drawable/arrow"
        android:layout_alignStart="@+id/fab" />
    <android.support.design.widget.FloatingActionButton
        android:id="@+id/fab"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="bottom|right|end"
        android:layout_alignParentBottom="true"
        android:layout_alignParentRight="true"
        android:src="@drawable/settings"
        android:clickable="true"/>
</RelativeLayout>

And the following code in the OnCreate method:

...
FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
    fab.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            Log.d(TAG,"FAB Setting Clicked");
            Intent i = new Intent(getApplicationContext(), AppPreferences.class);
            startActivity(i);
        }
    });
...

But when I press the button nothing happens, The intent does not run, and the log is not written.

The button animation is working and every time I press the button the following log message appears :

D/ViewRootImpl: ViewPostImeInputStage ACTION_DOWN

So there seems to be some reaction.

What am I doing wrong?


回答1:


Try this:

fab.bringToFront();

Should help.




回答2:


The solution suggested by @Tasos worked:

add a click event inside the xml e.g

android:onClick="runThis"

and then in the Activity add

public void runThis(View v) { ..... }



回答3:


I experienced the same problem when I failed to return the View object, but instead made another call to infalter.inflate like this:

View rootView = inflater.inflate(R.layout.fragment_main, container, false);
/* setup onClick handler */
return inflater.inflate(R.layout.fragment_main, container, false);

The fix was replacing the return with

return rootView;



回答4:


I actually found the following solution to deal with lost clicks

public class MainActivity extends AppCompatActivity implements View.OnClickListener 
{
    @Override
        public void onClick(View v) {
            Log.v(LOG_HEADER,"onClick");       
        }
}

public class SearchFragment extends Fragment
{
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
    {
        LinearLayout ll = (LinearLayout )inflater.inflate(R.layout.fragment_search, container, false);
        ll.setOnClickListener((MainActivity)getActivity());
        return ll;
    }
}


来源:https://stackoverflow.com/questions/33189833/floating-action-button-does-not-raise-onclick-event

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!