[Android按钮在显示时不可点击

问题描述 投票:0回答:1

嗨,我在这里找不到任何可能的答案后发表了这篇文章。

我在一个片段中有一个不可见的LinearLayout LL1,当recylerView数据为空时,该片段变为可见,该LL1上有一个按钮。问题仅仅是按钮不可点击!我尝试以其他方式设置侦听器,但仍然无法正常工作。这是下面的代码:

这是xml文件:

 <android.support.design.widget.CoordinatorLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".activity.main.MainActivity"
    android:layout_marginTop="3dp">

    <LinearLayout
        android:id="@+id/msg_emty_layout"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:gravity="center"
        android:visibility="gone"
        android:orientation="vertical">

        <ImageView
            android:layout_width="100dp"
            android:layout_height="100dp"
            android:src="@drawable/ic_empty_box" />
        <TextView
            android:layout_marginTop="10dp"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Aucune vente n'a été trouvée !"
            android:textSize="15dp"
            android:layout_gravity="center_horizontal"
            android:textColor="@color/greydark"/>
        <Button
            android:id="@+id/btnAddSale"
            android:layout_marginTop="5dp"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Créer une vente"
            android:textColor="@color/whiteTextColor"
            android:background="@drawable/centre_button">

        </Button>

    </LinearLayout>

    <android.support.v4.widget.SwipeRefreshLayout
        android:id="@+id/swipe_refresh_db"
        android:layout_width="match_parent"
        android:layout_height="wrap_content">

        <android.support.v7.widget.RecyclerView
            android:id="@+id/recycler_view_sale_list"
            android:layout_width="match_parent"
            android:layout_height="match_parent"/>

    </android.support.v4.widget.SwipeRefreshLayout>

    <android.support.design.widget.FloatingActionButton
        android:id="@+id/fab_add_db"
        android:layout_margin="20dp"
        android:layout_gravity="bottom|end"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:background="@color/colorAccesGreen"
        android:src="@drawable/ic_edit"/>


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

这是java:

   private void generateSaleList(ArrayList<Sale> saleArrayList, View view) {
    if(saleArrayList.isEmpty()){
        getActivity().setTitle("Ventes sur portable");
        msgLayout.setVisibility(View.VISIBLE);
        creatSaleBtn = (Button) view.findViewById(R.id.btnAddSale);
        creatSaleBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Toast.makeText(getActivity(), "Button clicked !", Toast.LENGTH_LONG).show();
            }
        });
        creatSaleBtn.setClickable(true);

    }else{
        msgLayout.setVisibility(View.GONE);
        recyclerView_db = view.findViewById(R.id.recycler_view_sale_list);
        adapter_db = new SaleAdapter((ArrayList<Sale>) Utils.sortArray(saleArrayList),this,1,getContext(),this);
        RecyclerView.LayoutManager layoutManager = new LinearLayoutManager(getActivity());
        recyclerView_db.setLayoutManager(layoutManager);
        recyclerView_db.setAdapter(adapter_db);
        adapter_db.notifyDataSetChanged();
    }

}

你们看到什么导致了这个奇怪的问题吗?

java android xml android-fragments android-button
1个回答
0
投票

您可以尝试使用requestFocusFromTouch()

调用此选项可将焦点移至特定视图或其某一视图后代。

 msgLayout.setVisibility(View.VISIBLE);
 creatSaleBtn = (Button) view.findViewById(R.id.btnAddSale);
 creatSaleBtn.requestFocusFromTouch();
© www.soinside.com 2019 - 2024. All rights reserved.