如何在LinearLoyout ImageView中设置点击侦听器

时间:2019-06-05 14:00:53

标签: android android-linearlayout onclicklistener

我无法处理ImageView上的LinearLayout上的Listnerer 我一直在尝试 andorid:clickable andorid:focusable

onTouchListener

将类型更改为AppCompactImageView

我不知道片段或其他片段是否有问题,但我无能为力。

这是我的 结帐代码

<ScrollView
....
<LinearLayout
....
        <LinearLayout
            android:paddingTop="15dp"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:gravity="center"
            android:orientation="horizontal">

            <ImageView
                android:id="@+id/mail_icon_about"
                android:layout_width="50dp"
                android:layout_height="50dp"
                android:layout_gravity="center"
                android:contentDescription="@string/ikona_maila"
                app:srcCompat="@drawable/ic_mail"
                tools:srcCompat="@drawable/ic_mail" />

            <ImageView
                android:id="@+id/facebook_icon_about"
                android:layout_width="60dp"
                android:layout_height="60dp"
                android:layout_gravity="center"
                android:layout_marginStart="16dp"
                android:contentDescription="@string/ikona_facebook"
                app:srcCompat="@drawable/ic_facebook"
                tools:srcCompat="@drawable/ic_facebook" />

            <ImageView
                android:layout_marginStart="16dp"
                android:id="@+id/website_icon_about"
                android:layout_width="50dp"
                android:layout_height="50dp"
                android:contentDescription="@string/ikona_website"
                app:srcCompat="@drawable/ic_website"
                tools:srcCompat="@drawable/ic_website" />

        </LinearLayout>
</LinearLayout>


</ScrollView>

这是我的代码(是片段):P

    public class AboutFragment extends Fragment implements View.OnClickListener {

    ImageView mailImage;
    ImageView faceBookImage;
    ImageView webSiteImage;


    public AboutFragment() {
        // Required empty public constructor
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        View view = inflater.inflate(R.layout.fragment_about, container, false);

        mailImage=view.findViewById(R.id.mail_icon_about);
        faceBookImage=view.findViewById(R.id.facebook_icon_about);
        webSiteImage=view.findViewById(R.id.website_icon_about);

        mailImage.setOnClickListener(this);
        faceBookImage.setOnClickListener(this);
        webSiteImage.setOnClickListener(this);

        return view;
    }


    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.mail_icon_about:
                Toast.makeText(getContext(), "1", Toast.LENGTH_SHORT).show();
                Log.e("About","1");
                break;
            case R.id.facebook_icon_about:
                Toast.makeText(getActivity(), "2", Toast.LENGTH_SHORT).show();
                Log.e("About","2");
                break;
            case R.id.website_icon_about:
                Toast.makeText(getActivity(), "3", Toast.LENGTH_SHORT).show();
                Log.e("About","3");
                break;
        }

    }

2 个答案:

答案 0 :(得分:0)

尝试使用ImageButton代替ImageView

答案 1 :(得分:0)

在引用该XML文件的Activity.java中,创建一个ImageView:

ImageView imageview = findViewById(R.id.idofyourimageview);

然后设置一个点击侦听器:

imageview.setOnClickListener(new View.onClickListener(){
     @Override
     public void onClick(View v){
          //your code
     }
});