【问题标题】:Disable Button on click单击时禁用按钮
【发布时间】:2012-04-07 23:48:02
【问题描述】:

我是编程世界的新手,我的知识有限。如果我问任何错误,请原谅我。 我的问题是。

我正在创建一个具有开始和停止按钮的活动。当用户单击开始按钮时,必须启动服务;并且在 STOP 时服务必须停止。

现在我想在单击开始按钮时禁用我的 START 按钮(服务在单击 START 按钮时启动),当单击 STOP 按钮时,我希望将 START 按钮视为正常的可单击按钮。

我通过创建按钮对象使用了 .setEnabled(false)。 我需要帮助...在此先感谢

【问题讨论】:

  • 当你使用 .setEnabled(false) 时会发生什么?您能否更具体地说明您期望的输出和得到的结果
  • Nav-它被视为按钮被禁用。但我希望仅在按下该按钮时禁用该按钮
  • 嗯?您想在单击“开始”按钮时禁用它,对吗?当单击停止按钮时,您想禁用停止按钮并启用开始按钮,还是我都错了?

标签: android android-widget


【解决方案1】:
int count = 0;

if (count == 0) {
    stop.setEnabled(false);
    PlayButton.setEnabled(true);
}

@Override
public void onClick(View v) {
    switch(v.getId()) {
        case R.id.play:
            count++;
            play.setEnabled(false);
            Toast.makeText(this, "Button Disabled", Toast.LENGTH_LONG).show();
            Stopbutton.setEnabled(true);
            break;

        case R.id.stop:
            Toast.makeText(this, "Button Disabled", Toast.LENGTH_LONG).show();
            count--;    
            PlayButton.setEnabled(true);
            stop.setEnabled(false);
            break;        
    }
}

并检查此链接 How to disable an Android button?

【讨论】:

    【解决方案2】:

    你也可以试试:-

    用于按钮启用-

    button.setClickable(true); 
    

    按钮禁用-

    button.setClickable(false);
    

    【讨论】:

    • 谢谢,这正是我想要的,+1
    【解决方案3】:

    在 onclick 的正文中

    在按钮1被点击时禁用它

    public void onClick(View v) {
      if(v.getId() == R.id.button1)
       {
           Button btn = (Button)findViewById(R.id.buton1);
           btn.setEnabled(false);
    
       }
    
    }
    

    【讨论】:

      【解决方案4】:

      如果你想从另一个类中禁用它,你可以使用它,

      Button btn = ((MainActivity)context).findViewById(R.id.myButton);
      btn.setEnabled(false);    //or (true) to enable it
      

      您还必须在课程开始时声明“上下文”

      public class MyClass extends AppCompatActivity {    
          Context context;
      

      当我需要执行某个操作并且不希望用户继续点击按钮时,我通常在我的 onPreExecute 和 onPostExecute 中使用它。

      @Override
      protected void onPreExecute() {
          //some actions to be performed or set before executing task 
          Button btn = ((MainActivity)context).findViewById(R.id.myButton);
          btn.setEnabled(false);
      }
      
      @Override
      protected void onPostExecute() {
          //some actions to be performed or set after executing task 
          Button btn = ((MainActivity)context).findViewById(R.id.myButton);
          btn.setEnabled(true);
      }
      

      【讨论】:

        【解决方案5】:

        使用 kotlin,您可以在点击时禁用按钮,

        myButton.setOnClickListener {
            it.isClickable = false     // to disable clicking on button
            it.isEnabled = false       // to disable button
        }
        

        别忘了这里的view是it

        【讨论】:

          【解决方案6】:

          试试这个:

          MainActivity.java

          import android.app.Activity;
          import android.os.Bundle;
          import android.view.View.OnClickListener;
          import android.widget.Button;
          import android.view.View;
          
          public class MainActivity extends Activity {
          
          private Button start, stop;
          
          @Override
          public void onCreate(Bundle savedInstanceState){
              super.onCreate(savedInstanceState);
              setContentView(R.layout.main);
              start = (Button)findViewById(R.id.start);
              stop = (Button)findViewById(R.id.stop);
          
              start.setOnClickListener(new OnClickListener() {
                  public void onClick(View v) {
                      start.setVisibility(View.GONE);
                      /* do something else */
                  }
              });
          
              stop.setOnClickListener(new OnClickListener() {
                  public void onClick(View v) {
                      start.setVisibility(View.VISIBLE);
                      /* do something else */
                  }
              });
          }
          

          }

          还有你的布局main.xml

          <?xml version="1.0" encoding="utf-8"?>
          <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
          android:orientation="vertical"
          android:layout_width="fill_parent"
          android:layout_height="fill_parent"
          >
          <Button
          android:id="@+id/start"
          android:layout_width="fill_parent" 
          android:layout_height="wrap_content" 
          android:text="Start"
          android:visibility="visible"
          />
          <Button
          android:id="@+id/stop"
          android:layout_width="fill_parent" 
          android:layout_height="wrap_content" 
          android:text="Stop"
          android:visibility="visible"
          />
          

          【讨论】:

            【解决方案7】:

            如果你想让按钮在点击按钮后不可见,那么首先按照 vipin 的说法禁用它并添加这个.setVisibility(View.INVISIBLE); 这将在按钮点击后隐藏按钮,当你想再次让它可见时使用这个.setVisibility(View.VISIBLE);

            注意:如果您希望按钮不可见并且不希望它占用所需的布局空间,则可以使用View.GONE 而不是View.INVISIBLE

            我希望我很清楚。

            【讨论】:

              【解决方案8】:

              更优选的解决方案是,

              onclick(){
                btn.setEnabled(false);
                btn.setClickable(false);
                //yourwork
                myWork();
              }
              
              myWork(){
               //your tasks.
               btn.setEnabled(true);
               btn.setClickable(true);
              }
              

              【讨论】:

                【解决方案9】:

                为按钮初始化 onClickListener。在第一个按钮内只需将 setEnable() 设置为 false ..然后从第二个按钮单击侦听器将 setEnable 设置为 true

                享受

                【讨论】:

                  【解决方案10】:

                  您可以致电button.setOnClickListener(null); 取消事件监听器。此外,您可以更改背景可绘制对象以使其具有禁用效果。

                  PS:只有在其他方法无效时才尝试此解决方案。

                  【讨论】:

                  • 虽然更推荐的解决方案是使用 setEnabled(false) 方法。
                  【解决方案11】:
                  myButton.setEnabled(false);
                  
                  Timer buttonTimer = new Timer();
                  buttonTimer.schedule(new TimerTask() {
                  
                      @Override
                      public void run() {
                          runOnUiThread(new Runnable() {
                  
                              @Override
                              public void run() {
                                  myButton.setEnabled(true);
                              }
                          });
                      }
                  }, 5000);
                  

                  试试这个,效果很好

                  【讨论】:

                    猜你喜欢
                    • 1970-01-01
                    • 1970-01-01
                    • 1970-01-01
                    • 2019-08-15
                    • 2011-11-24
                    • 1970-01-01
                    • 1970-01-01
                    相关资源
                    最近更新 更多