【问题标题】:Only make it clickable if....?只有在......时才使其可点击?
【发布时间】:2015-11-15 20:52:11
【问题描述】:

我有一个应用程序,您可以在其中单击背景,它会从可绘制对象更改为另一个背景,但我只想在用户单击我调用的按钮时使其可单击,“我想单击它”。

那么,如何编写如下代码:

If user click on button1 2 times, make layout clickable
else
not make layout clickable

所以,我想以某种方式存储点击,并强制我的应用记住它,我还想计算点击次数。

Android 的哪一章有助于我理解这一点?感谢您的帮助,对于这个愚蠢的问题,我们深表歉意:)

【问题讨论】:

    标签: android button click


    【解决方案1】:

    您可以让计数器在点击时递增,然后在计数器达到您想要的值时禁用组件

    【讨论】:

      【解决方案2】:
      int count = 0;
      
      Button button = (Button) findViewById(R.id.i_want_to_click_it);
      
      button.setOnClickListener(new View.OnClickListener() {
          public void onClick(View v) {
              if (++count == 2) {
                  // make stuff clickable here on 2nd click
              }
              // if you also want to make things unclickable if there 
              // are more than 2 clicks, add the else{} condition
              else {
                  // make stuff unclickable here
              }
          }
      });
      

      【讨论】:

        【解决方案3】:

        您可以通过在按钮上注册onClickListener 来检测用户何时单击按钮。在该回调中,您可以计算它被单击的次数并将该信息存储在变量中。如果您的应用程序可以从纵向模式更改为横向模式,请不要忘记将变量存储在onSavedInstanceState 中,然后在onCreate 中检索它,因为更改布局模式会破坏活动并重建它,这将重置您的变量。我突出显示要搜索的关键字。

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2013-02-14
          • 2023-03-20
          • 2019-05-01
          • 1970-01-01
          • 2012-12-25
          • 2018-07-18
          • 1970-01-01
          • 2021-09-09
          相关资源
          最近更新 更多