【问题标题】:Fab should hide or unhide through java when some action is performed执行某些操作时,Fab 应通过 java 隐藏或取消隐藏
【发布时间】:2023-03-23 15:18:01
【问题描述】:

我采用了一个浮动操作按钮,最初它的可见性消失了,通过 xml。完成一些付款后,我在网络视图中打开,成功后,该工厂应该对处于另一个活动中的用户可见。如何做到这一点?

【问题讨论】:

    标签: android floating-action-button


    【解决方案1】:

    您必须在代码中获得对 FAB 的引用。然后你可以使用 setVisibility() 来隐藏或显示按钮。

    public class SomeActivity extends AppCompatActivity {
    
         private View mFloatingActionButton;
    
         @Override
         public void onCreate(Bundle savedInstaces) {
             super.onCreate(savedinstances);
    
             this.setContentView(R.layout.your_layout_here);
             this.mFloatingActionButton = this.findViewById(R.id.floatingActionButton);
    
         }
    
         // This is some method in your code called when you want to show the button
         public void onSomeActoion() {
             // This makes the FAB visible
             this.mFloatingActionButton.setVisibility(View.VISIBLE);
    
         }
    }
    

    【讨论】:

      【解决方案2】:

      在支付活动中,您可以在SharedPreferences 中设置一个布尔标志,例如paymentSuccesfultruefalse,在另一个活动中,从 sharedPreference 中检索值并尝试设置可见性在 fab 基于 sharedPreference 的结果。即

      if (paymentSuccessful) {
        fab.setVisibility(View.VISIBLE);
      } else {
        fab.setVisibility(View.GONE);
      }
      

      确保在从 sharedPreference 获取值后设置可见性。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2010-09-29
        • 1970-01-01
        • 2021-06-07
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2020-07-07
        相关资源
        最近更新 更多