工具栏中的ImageButton对Click事件没有响应
我想将一个ImageButton放在工具栏中,它的一个徽标,并配置它,以便用户点击它,它将它们带到一个网站。工具栏中的ImageButton对Click事件没有响应
我能够在所有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" />
然后在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);
}
});
}
但没有任何反应。没有任何标识被点击。它没有响应,因为它在工具栏中?
非常感谢。
您必须在Activity
中执行onClick
方法GoToLogo
,而不是Fragment
。
谢谢。我也尝试过,它仍然没有做任何事情。 – user3079872
您不应该在GoToLogo方法内再次设置onClickListener。删除该监听器,然后在单击该按钮之后编写该做什么。 – Bob
是的,试过了,图像上的点击甚至没有注册。 – user3079872
现在尝试注释掉在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);
}
谢谢。它甚至没有注册点击。 – user3079872
试试这个:
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
}
});
}
我已经有这个实现,并没有点击注册。 – user3079872
如果'ImageButton imageButton =(ImageButton)toolbar.findViewById(R.id.back);'可能有助于检索视图。 –
删除您
android:onClick="GoToLogo"
和GoToLogo方法,你不要您无需加倍定义onClick行为的方式。
然后尝试findView相应的,它更有意义在你的活动,而不是你的片段做..
布局XML文件,过帐是从framgments或活动? –
@Bruno,它是活动的xml – user3079872
所以让onclickListener里面的活动创建方法。不片段OnActivityCreated方法。 –