Android 工具栏中的ImageButton未响应单击事件
我想在工具栏上放一个ImageButton,它是一个徽标,并对其进行配置,以便用户点击它,它会将他们带到一个网站 我能够在所有3个片段上正确显示徽标,但如果单击徽标,则不会发生任何事情 活动_main中的xml如下所示:Android 工具栏中的ImageButton未响应单击事件,android,onclicklistener,android-toolbar,buttonclick,android-imagebutton,Android,Onclicklistener,Android Toolbar,Buttonclick,Android Imagebutton,我想在工具栏上放一个ImageButton,它是一个徽标,并对其进行配置,以便用户点击它,它会将他们带到一个网站 我能够在所有3个片段上正确显示徽标,但如果单击徽标,则不会发生任何事情 活动_main中的xml如下所示: <?xml version="1.0" encoding="utf-8"?><android.support.design.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/a
<?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" />
我还实现了和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);
}
});
}
但什么也没发生。没有识别任何正在单击的徽标。它是否因为在工具栏中而没有响应
非常感谢。您必须在
活动中实施onClick
方法GoToLogo
,而不是在片段中 现在尝试注释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);
}
试试这个:
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
}
});
}
删除你的
android:onClick="GoToLogo"
和GoToLogo方法,您不需要加倍定义onClick行为的方式
然后尝试查找对应的视图,在活动中而不是片段中执行此操作更有意义。您发布的布局xml文件来自框架或活动?@Bruno,它是活动的xml,因此在活动oncreate方法中生成onclickListener。不是activitycreated方法的片段。@Bruno,我试过了,但它没有响应任何单击。谢谢。我也试过了,但仍然没有任何效果。不应该在GoToLogo方法中再次设置onClickListener。删除该侦听器,然后在单击按钮后写下要做的事情。是的,尝试过,但图像上的单击甚至没有注册。尝试将android:clickable=“true”
添加到ImageButton
。尝试过,但仍然无法识别单击事件。谢谢。它甚至没有注册click.ifImageButton ImageButton=(ImageButton)工具栏.findViewById(R.id.back)代码>可能有助于检索视图。
android:onClick="GoToLogo"