gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-02 08:39:56 24 4
gpt4 key购买 nike

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

我在 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/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com