老师怎么在xml中为Fragment设置点击事件

老师怎么在xml中为Fragment设置点击事件

//ShopFragment

public class ShopFragmet extends Fragment {
   private ImageView shop_back;
   private TextView loction,outshop,shop_default;

   public ShopFragmet() {
       // 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_shop_fragmet, container, false);
       shop_back=view.findViewById(R.id.shop_back);
       loction=view.findViewById(R.id.loction);
       outshop=view.findViewById(R.id.outshop);
       shop_default=view.findViewById(R.id.shop_default);
       shop_back.setOnClickListener(new View.OnClickListener() {
           @Override
           public void onClick(View v) {
               startActivity(new Intent(getContext(), MainActivity.class));
           }
       });


       return view;
   }
   
   
   //对TextView控件设置点击事件
   public void shop_myClick(){



   }




}

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
   xmlns:tools="http://schemas.android.com/tools"
   android:layout_width="match_parent"
   android:layout_height="match_parent"
   android:orientation="vertical"
   tools:context=".fragment.ShopFragmet">
   <LinearLayout
       android:layout_weight="1"
       android:background="#47F8D6"
       android:orientation="horizontal"
       android:layout_width="match_parent"
       android:layout_height="0dp">
       <ImageView
           android:id="@+id/shop_back"
           android:layout_marginLeft="20dp"
           android:src="@drawable/back"
           android:layout_gravity="center_vertical"
           android:layout_width="wrap_content"
           android:layout_height="wrap_content"/>
       <TextView
           android:id="@+id/title"
           android:padding="20dp"
           android:text="商家"
           android:background="#47F8D6"
           android:gravity="center_vertical"
           android:textSize="28sp"
           android:textStyle="bold"
           android:layout_width="match_parent"
           android:layout_height="80dp"/>

   </LinearLayout>
   <LinearLayout
       android:layout_weight="1"
       android:orientation="horizontal"
       android:layout_width="match_parent"
       android:layout_height="0dp">
       <TextView
           android:id="@+id/loction"
           android:padding="20dp"
           android:onClick="shop_myClick"
           android:text="本地商家"
           android:layout_weight="1"
           android:gravity="center_vertical"
           android:textSize="20sp"
           android:layout_width="wrap_content"
           android:layout_height="wrap_content"/>
       <TextView
           android:id="@+id/outshop"
           android:padding="20dp"
           android:text="外地商家"
           android:onClick="shop_myClick"
           android:gravity="center_vertical"
           android:textSize="20sp"
           android:layout_width="wrap_content"
android:layout_weight="1"
           android:layout_height="wrap_content"/>
       <TextView
           android:id="@+id/shop_default"
           android:padding="20dp"
           android:text="默认 排序"
           android:onClick="shop_myClick"
           android:gravity="center_vertical"
           android:textSize="20sp"
           android:layout_width="wrap_content"
android:layout_weight="1"
           android:layout_height="wrap_content"/>


   </LinearLayout>
   <FrameLayout
       android:id="@+id/shop_container"
       android:layout_weight="7"
       android:layout_width="match_parent"
       android:layout_height="0dp">



   </FrameLayout>


</LinearLayout>

正在回答 回答被采纳积分+1

登陆购买课程后可参与讨论,去登陆

1回答
好帮手慕雪 2020-04-16 09:59:42

你是说切换不同的fragment吗?那是为下面的按钮“主页”,“购物”,“我的”设置监听事件的,不是为fragment设置监听事件的。以上三个按钮监听到点击事件后就replace一下fragment就行了。祝:学习愉快

  • 提问者 慕雪2495638 #1
    不是,是xml中 的TextView控件直接设置点击事件,我这样设置了不行 <TextView android:id="@+id/loction" android:padding="20dp" android:onClick="shop_myClick"/> android:onClick="shop_myClick"---这个
    2020-04-16 10:09:41
  • 好帮手慕雪 回复 提问者 慕雪2495638 #2
    public void shop_myClick()改为 public void shop_myClick(View v) 需有固定的形参。
    2020-04-16 10:40:12
问题已解决,确定采纳
还有疑问,暂不采纳

恭喜解决一个难题,获得1积分~

来为老师/同学的回答评分吧

0 星
请稍等 ...
微信客服

购课补贴
联系客服咨询优惠详情

帮助反馈 APP下载

慕课网APP
您的移动学习伙伴

公众号

扫描二维码
关注慕课网微信公众号

在线咨询

领取优惠

免费试听

领取大纲

扫描二维码,添加
你的专属老师
插入代码