【问题标题】:ImageButton in toolbar not responding to Click event工具栏中的 ImageButton 没有响应 Click 事件
【发布时间】:2018-01-17 13:36:09
【问题描述】:

我想在工具栏中放置一个 ImageButton,它是一个徽标,并对其进行配置,以便用户点击它时,它会将他们带到一个网站。

我能够在所有 3 个片段上正确显示徽标,但如果我单击徽标,则没有任何反应。

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:id="@+id/main_content"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true"
>

<android.support.design.widget.AppBarLayout
    android:id="@+id/appbar"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:paddingTop="@dimen/appbar_padding_top"
    android:theme="@style/AppTheme">

    <android.support.v7.widget.Toolbar
        android:id="@+id/toolbar"
        android:layout_width="match_parent"
        android:layout_height="?attr/actionBarSize"
        android:background="?attr/colorPrimary"
        app:layout_scrollFlags="scroll|enterAlways"
        app:popupTheme="@style/AppTheme">

        <ImageButton
            android:id="@+id/logo"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center"
            android:src="@drawable/jclogo"
            android:onClick="GoToLogo"/>

    </android.support.v7.widget.Toolbar>

    <android.support.design.widget.TabLayout
        android:id="@+id/tabs"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />

</android.support.design.widget.AppBarLayout>

<android.support.v4.view.ViewPager
    android:id="@+id/container"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    app:layout_behavior="@string/appbar_scrolling_view_behavior" />

然后在 Fragment 中的 OnActivityCreated 中设置 OnClickListener:

 public void onActivityCreated(Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    super.onActivityCreated(savedInstanceState);
    myLogo = (ImageButton) getActivity().findViewById(R.id.logo);
    myLogo.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            Uri uri = Uri.parse("http://somewebsite.co.uk");
            Intent intent = new Intent(Intent.ACTION_VIEW, uri);
            startActivity(intent);
        }
    });
}

我也实现了 OnClick 方法:

    public void GoToLogo(View v) {
    v.setOnClickListener(new View.OnClickListener() {
        public void onClick(View v) {
            Uri uri = Uri.parse("http://somewebsite.co.uk"); 
            Intent intent = new Intent(Intent.ACTION_VIEW, uri);
            startActivity(intent);

        }
    });
}

但是什么也没发生。无法识别任何被点击的标志。是不是因为它在工具栏中而没有响应?

非常感谢。

【问题讨论】:

  • 您发布的布局xml文件来自framgments或activity?
  • @Bruno,是Activity的xml
  • 所以在活动oncreate方法中创建onclickListener。不是片段 OnActivityCreated 方法。
  • @Bruno,我试过了,它没有响应任何点击。

标签: android onclicklistener android-toolbar buttonclick android-imagebutton


【解决方案1】:

您必须在您的Activity 中实现onClick 方法GoToLogo,而不是在Fragment 中。

【讨论】:

  • 谢谢。我也试过了,还是不行。
  • 您不应在 GoToLogo 方法中再次设置 onClickListener。删除那个监听器,然后写下点击按钮后要做什么。
  • 是的,试过了,连点击图片都没有注册。
  • 尝试将android:clickable="true" 添加到ImageButton
  • 试过了,还是不能识别点击事件。
【解决方案2】:

现在尝试注释掉onCreate() 中的onClickListener 并将GoToLogo() 方法更改为此,看看是否可行

public void GoToLogo(View v) {
        Uri uri = Uri.parse("http://somewebsite.co.uk"); 
        Intent intent = new Intent(Intent.ACTION_VIEW, uri);
        startActivity(intent);

    }

【讨论】:

  • 谢谢。它甚至没有注册点击。
【解决方案3】:

试试这个:

private ImageButton button;
public void onActivityCreated(Bundle savedInstanceState) {  
    super.onActivityCreated(savedInstanceState);
    View view = getView();
    button  = (ImageButton) view.findViewById(R.id.buttonId);
    button.setOnClickListener(new OnClickListener() {           
        @Override
        public void onClick(View v) {
            //code stuff   
        }
    }); 
}

【讨论】:

  • 如果ImageButton imageButton = (ImageButton) toolbar.findViewById(R.id.back); 可能有助于检索视图。
【解决方案4】:

删除你的

android:onClick="GoToLogo"

和 GoToLogo 方法一样,您不需要将定义 onClick 行为的方式加倍。

然后尝试findView对应,在你的Activity而不是你的fragment中这样做更有意义..

【讨论】:

    猜你喜欢
    • 2020-10-23
    • 2017-04-06
    • 1970-01-01
    • 1970-01-01
    • 2012-09-07
    • 1970-01-01
    • 2021-01-28
    • 1970-01-01
    • 2019-02-19
    相关资源
    最近更新 更多