如何在ImageButton中输入多个事件?

时间:2018-11-06 12:04:11

标签: java android android-studio

所以我有这段代码,上下滑动按钮效果很好,但是我想通过单击imagebutton使其在多个按钮上起作用:

Beranda.java

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_beranda);
    tombolSejarah = findViewById(R.id.tombolsejarah);
}


public void slideUpDown(final View view) {
     if (!isPanelShown()) {
         // Show the panel
         Animation bottomUp = AnimationUtils.loadAnimation(this,
                 R.anim.buttom_up);
         tombolSejarah.startAnimation(bottomUp);
         tombolSejarah.setVisibility(View.VISIBLE);
     }else {
         // Sembunyikan
         Animation bottomDown = AnimationUtils.loadAnimation(this,
                 R.anim.buttom_down);
         tombolSejarah.startAnimation(bottomDown);
         tombolSejarah.setVisibility(View.GONE);
    }
}


private boolean isPanelShown() {
        return tombolSejarah.getVisibility() == View.VISIBLE;
}

我的图像按钮:tombolsejarah,tombolkawasan,tombolsuku,tombolkebudayaan,tombolbahasa。

问题是如何实现?

3 个答案:

答案 0 :(得分:0)

尝试一下。如果这不是您想要的,我可能需要您澄清您的问题。

对您的活动实施OnClickListener

public class MyActivity extends AppCompact implements OnClickListener{}

在您的onCreate中找到这些按钮并设置onClickListener()

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_beranda);
    tombolSejarah = findViewById(R.id.tombolsejarah);
    tombolSejarah.setOnClickListener(this);
    tombolkawasan = findViewById(R.id.tombolkawasan);
    tombolkawasan.setOnClickListener(this);
    tombolsuku = findViewById(R.id.tombolsuku );
    tombolsuku.setOnClickListener(this);
  .....
}

覆盖onClick()

    @Override
    public void onClick(View v) {
        slideUpDown(v);
    }

对于slideUpDown(view

public void slideUpDown(final View view) {//* <<<<watch at this view
    // the point here is, DO show animation on this view
    //not on tombolSejarah button.

 if (!isPanelShown()) {
     // Show the panel
     Animation bottomUp = AnimationUtils.loadAnimation(this,
             R.anim.buttom_up);
     view.startAnimation(bottomUp);
     view.setVisibility(View.VISIBLE);
 }else {
     // Sembunyikan
     Animation bottomDown = AnimationUtils.loadAnimation(this,
             R.anim.buttom_down);
     view.startAnimation(bottomDown);
     view.setVisibility(View.GONE);
}
}

答案 1 :(得分:0)

这可能是您想要的:

    public void slideUpDown(final View view) {
        if (!isPanelShown()) {

            Animation bottomUp = AnimationUtils.loadAnimation(this,
                R.anim.buttom_up);
            view.startAnimation(bottomUp);
            view.setVisibility(View.VISIBLE);
        } else {

            Animation bottomDown = AnimationUtils.loadAnimation(this,
                R.anim.buttom_down);
            view.startAnimation(bottomDown);
            view.setVisibility(View.GONE);
        }
    }

将要滑入的按钮传递到此功能。

希望有帮助。

答案 2 :(得分:0)

一个选项是将按钮置于一个单独的父级布局(RelativeLayoutLinearLayout)中,以动画形式上下滑动按钮(这是指按钮的行为方式相同)在imageButton上单击)。 您可以将父视图命名为animator,然后在该view上执行代码中的操作。

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_beranda);
    tombolSejarah = findViewById(R.id.tombolsejarah);

    //the parent view to handle animations when button is clicked
    animator = findViewById(R.id.animator);

    //Listening for click events
    animator.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            slideUpDown(v)
        }
    }
}


public void slideUpDown(final View view) {
    if (!isPanelShown()) {
        // Show the panel
        Animation bottomUp = AnimationUtils.loadAnimation(this,
             R.anim.buttom_up);
        //tombolSejarah.startAnimation(bottomUp);
        //tombolSejarah.setVisibility(View.VISIBLE);
        //animating view
        animator.startAnimation(bottomUp);
        animator.setVisibility(View.VISIBLE);
    }else {
        // Sembunyikan
        Animation bottomDown = AnimationUtils.loadAnimation(this,
             R.anim.buttom_down);
        //tombolSejarah.startAnimation(bottomDown);
        //tombolSejarah.setVisibility(View.GONE);
        //animating view
        animator.startAnimation(bottomUp);
        animator.setVisibility(View.VISIBLE);
   }
}


private boolean isPanelShown() {
    return tombolSejarah.getVisibility() == View.VISIBLE;
}