Java 以编程方式触发按钮单击

Java 以编程方式触发按钮单击,java,android,Java,Android,我的android studio应用程序出现问题。当按钮进入if-else语句但不起作用时,我希望通过编程触发对按钮的单击。你能帮帮我吗。这里是纽贝!这是我的密码 enemy_left.setOnTouchListener(new View.OnTouchListener() { @Override public boolean onTouch(View view, MotionEvent motionEvent) { if (motion

我的android studio应用程序出现问题。当按钮进入if-else语句但不起作用时,我希望通过编程触发对按钮的单击。你能帮帮我吗。这里是纽贝!这是我的密码

enemy_left.setOnTouchListener(new View.OnTouchListener() {
        @Override
        public boolean onTouch(View view, MotionEvent motionEvent) {
            if (motionEvent.getAction()==MotionEvent.ACTION_DOWN){
                run(1);
            }else if (motionEvent.getAction()==MotionEvent.ACTION_UP){
                standby(1);
            }
            enemy.setScaleX(-1f);
            float x=enemy.getX();
            float n= (float) 5.00;
            float v = x - n;
            enemy.setX(v);
            return true;
        }
    });
这是我想执行单击的if-else语句。。但不起作用

if(bida1<kontra1){
      enemy_left.performClick(); //does not work the perform click here... :(
  }

if(bida1点击按钮意味着执行一组指令。
我建议您使用如下方法编写button.onClick指令:

void codeToBeImplemented(){
      //Your Logic
}
然后在需要时单击按钮或以编程方式调用此方法

在单击按钮时调用方法:

在任何地方以编程方式调用该方法(以中为例)

if(bida1)请查看我们的,以帮助您提出一个好问题,从而得到一个好答案。
enemy_left.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v)
            {
                codeToBeImplemented();
            }
        });
if(bida1<kontra1){
    codeToBeImplemented();
}