按钮仅在相继使用时才起作用

时间:2018-10-05 06:44:03

标签: java android

所以我在一个活动中有多个按钮,我只能一个接一个地使用它们。我已经读过我需要对(OnClickListener)部分做一些事情,但是我不确定该怎么做或如何做。任何帮助是极大的赞赏。代码如下:

import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;

public class Main2Activity extends AppCompatActivity {

private Button B3;
private Button B4;
private Button B5;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main2);

    B3 = findViewById(R.id.button3);

    B3.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {

            moveToDanceScheduleMenu();
        }
    });
}
private void moveToDanceScheduleMenu () {

    Intent intent = new Intent(Main2Activity.this, DanceScheduleMenu.class);
    startActivity(intent);

    B4 = findViewById(R.id.button10);

    B4.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {

            moveToWhatsOn();
        }
    });
}
private void moveToWhatsOn () {

    Intent intent = new Intent(Main2Activity.this, WhatsOn.class);
    startActivity(intent);

    B5 = findViewById(R.id.button11);

    B5.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {

            moveToMainResultsMenu();
        }
    });
}

编辑:谢谢你们目前提供的所有帮助,所以当我回到家时,我会进行测试。它们看起来与我读过的非常相似,因此可以正常工作。谢谢!

6 个答案:

答案 0 :(得分:2)

您正在按顺序设置点击侦听器。如果您想随时单击所有按钮,请将侦听器移至onCreate()

protected void onCreate() {
    B3.setOnClickListener(){};
    B4.setOnClickListner(){};
    // etc.
}

点击监听器就是它–它“监听”点击。例如,创建活动时,您的B3是唯一的一个监听对象,因此所有其他按钮都将忽略您对它们的单击。单击B3时,将调用moveToDanceScheduleMenu()B4开始收听。

我希望这可以使事情变得清晰起来。


完整代码:

import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;

public class Main2Activity extends AppCompatActivity {
    private Button B3;
    private Button B4;
    private Button B5;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main2);

        B3 = findViewById(R.id.button3);
        B3.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                moveToDanceScheduleMenu();
            }
        });

        B4 = findViewById(R.id.button10);
        B4.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                moveToWhatsOn();
            }
        });

        B5 = findViewById(R.id.button11);
        B5.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                moveToMainResultsMenu();
            }
        });
    }
    private void moveToDanceScheduleMenu () {
        Intent intent = new Intent(Main2Activity.this, DanceScheduleMenu.class);
        startActivity(intent);
    }

    private void moveToWhatsOn () {
        Intent intent = new Intent(Main2Activity.this, WhatsOn.class);
        startActivity(intent);
    }
}

答案 1 :(得分:1)

您可以像这样将所有代码放入OnCreate()方法中

import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;

public class Main2Activity extends AppCompatActivity {

    private Button B3;
    private Button B4;
    private Button B5;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main2);

        B3 = findViewById(R.id.button3);
        B4 = findViewById(R.id.button10);
        B5 = findViewById(R.id.button11);

        B3.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(Main2Activity.this, DanceScheduleMenu.class);
                startActivity(intent);
            }
        });

        B4.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(Main2Activity.this, WhatsOn.class);
                startActivity(intent);
            }
        });

        B5.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                moveToMainResultsMenu();
            }
        });

    }

答案 2 :(得分:1)

您正在按顺序使Button依赖于其他Button。 在oncreate中尝试绑定按钮及其OnclickListners。

@Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main2);

            B3 = findViewById(R.id.button3);
            B4 = findViewById(R.id.button10);
            B5 = findViewById(R.id.button11);


            B3.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {

                    moveToDanceScheduleMenu();
                }
            });

            B4.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {

                    moveToWhatsOn();
                }
            });

            B5.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {

                    moveToMainResultsMenu();
                }
            }); 
        }

答案 3 :(得分:0)

我认为这段代码是可行的

public class Main2Activity extends AppCompatActivity {

private Button B3;
private Button B4;
private Button B5;
private a=false,b=false;
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main2);

    B3 = findViewById(R.id.button3);

    B3.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {

            moveToDanceScheduleMenu();
             a=true ;
        }
    });
}
private void moveToDanceScheduleMenu () {

    Intent intent = new Intent(Main2Activity.this, DanceScheduleMenu.class);
    startActivity(intent);


}
 B4 = findViewById(R.id.button10);

    B4.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
          if(a==true ){
            moveToWhatsOn();
           } 
        }
    });
private void moveToWhatsOn () {

    Intent intent = new Intent(Main2Activity.this, WhatsOn.class);
    startActivity(intent);
        b=true ;
    B5 = findViewById(R.id.button11);
}

    B5.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
           if(b==true){
            moveToMainResultsMenu();
}
        }
    });
}

答案 4 :(得分:0)

如果只想在按下上一个按钮之后才使用某个按钮,则应该在onCreate()方法的开头禁用所有下一个按钮,并在以后按下上一个按钮时启用它们。

  B3.setEnabled(true);
  B4.setEnabled(false);
  B5.setEnabled(false);

然后启用onClick

之后的下一个按钮
        B3.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                moveToDanceScheduleMenu();
                B4.setEnabled(true);
            }
        });

对于您的代码

public class Main2Activity extends AppCompatActivity {

    private Button B3;
    private Button B4;
    private Button B5;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main2);

        B3 = findViewById(R.id.button3);
        B4 = findViewById(R.id.button10);
        B5 = findViewById(R.id.button11);



        B3.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                moveToDanceScheduleMenu();

            }
        });


        B4.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                moveToWhatsOn();
            }
        });


        B5.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                moveToMainResultsMenu();
            }
        });
    }

    private void moveToDanceScheduleMenu() {

        Intent intent = new Intent(Main2Activity.this, DanceScheduleMenu.class);
        startActivity(intent);


    }

    private void moveToWhatsOn() {

        Intent intent = new Intent(Main2Activity.this, WhatsOn.class);
        startActivity(intent);


    }
}

答案 5 :(得分:0)

有几种与按钮交互的方式:

第一种方法:类实现

public class Main2Activity extends AppCompatActivity implements OnClickListener {
private Button B3;
private Button B4;
private Button B5;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main2);

B3 = findViewById(R.id.button3);
B4 = findViewById(R.id.button10);
B5 = findViewById(R.id.button11);
B3.setOnClickListener(this);
B4.setOnClickListener(this);
B5.setOnClickListener(this);
}

public void onClick(View v){

 if(v.getId()==B3.getId()){
    moveToDanceScheduleMenu();
  }else if(v.getId()==B4.getId()){
    moveToWhatsOn();
  }else if(v.getId()==B5.getId()){
    moveToMainResultsMenu();
  }
}

private void moveToDanceScheduleMenu () {
Intent intent = new Intent(Main2Activity.this,DanceScheduleMenu.class);
startActivity(intent);
}

private void moveToWhatsOn () {
Intent intent = new Intent(Main2Activity.this, WhatsOn.class);
startActivity(intent);
}

第二种方法:私有变量(大块)

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main2);

findViewById(R.id.button3).setOnClickListener(mGlobal_OnClickListener);
findViewById(R.id.button10).setOnClickListener(mGlobal_OnClickListener);
findViewById(R.id.button11).setOnClickListener(mGlobal_OnClickListener);

}

//Global On click listener for all views
final OnClickListener mGlobal_OnClickListener = new OnClickListener() {
    public void onClick(final View v) {
        switch(v.getId()) {
            case R.id.button3:
            moveToDanceScheduleMenu();                   
            break;

            case R.id.button10:
            moveToWhatsOn();                   
            break;

            case R.id.button11:
            moveToDanceScheduleMenu();                   
            break;
        }
    }
};

private void moveToDanceScheduleMenu () {
Intent intent = new Intent(Main2Activity.this,DanceScheduleMenu.class);
startActivity(intent);
}

private void moveToWhatsOn () {
Intent intent = new Intent(Main2Activity.this, WhatsOn.class);
startActivity(intent);
}

第三种方法:在线(小块)

public class Main2Activity extends AppCompatActivity{

private Button B3;
private Button B4;
private Button B5;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main2);

 B3 = findViewById(R.id.button3);
 B4 = findViewById(R.id.button10);
 B5 = findViewById(R.id.button11);

 B3.setOnClickListener( new View.OnClickListener() {
 @Override
 public void onClick(View v) {
     moveToDanceScheduleMenu();
    }
  });

 B4.setOnClickListener( new View.OnClickListener() {
 @Override
 public void onClick(View v) {
     moveToWhatsOn();
   }
  });

 B5.setOnClickListener( new View.OnClickListener() {
 @Override
 public void onClick(View v) {
     moveToDanceScheduleMenu();
   }
  });

}

private void moveToDanceScheduleMenu () {
Intent intent = new Intent(Main2Activity.this,DanceScheduleMenu.class);
startActivity(intent);
}

private void moveToWhatsOn () {
Intent intent = new Intent(Main2Activity.this, WhatsOn.class);
startActivity(intent);
}