java - Android 按钮变为可见时不可点击

标签 java android xml android-fragments android-button

您好,我在此处找不到任何可能的答案后发布了此内容。

我在 fragment 中有一个不可见的 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();
    }

}

你们知道是什么导致了这个奇怪的问题吗?

最佳答案

您可以尝试使用 requestFocusFromTouch()

Call this to try to give focus to a specific view or to one of its descendants.

 msgLayout.setVisibility(View.VISIBLE);
 creatSaleBtn = (Button) view.findViewById(R.id.btnAddSale);
 creatSaleBtn.requestFocusFromTouch();

关于java - Android 按钮变为可见时不可点击,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61443565/

相关文章:

java - 如何使用response.path计算响应(Json)的总和?

java - 将函数输出保存到 Firebase 和 Android 的变量

xml - 是否可以使用 xsd 数据类型(例如 xsd :string) and xml language attribute simultaneously in OWL2/RDF?

java - 从资源中读取 XML 文件

java - 单链表的 addToRear 方法

java - @ControllerAdvice,如何获取调用该方法的类?

java - Java 中文件存在的锁定

android - 应用程序在同一型号的两部手机中显示不同的文字大小

java.lang.IllegalStateException : ViewPager 错误

java - 将字节保存到 xml 文件。无效的 Unicode 字符