如何在Android上正确关闭BottomSheet?

时间:2017-03-29 07:18:14

标签: android keydown onkeydown bottom-sheet

我在关闭BottomSheet时遇到问题,我设置KeyEvent.Keycode_BACK并检查BottomSheet是否未打开,并且已打开。我没有Collapse设置app: peekHeight = 0dpExpanded

@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {

    switch (keyCode){
        case KeyEvent.KEYCODE_BACK:

            if (bottomSheetBehavior.getState() != BottomSheetBehavior.STATE_HIDDEN) {
                bottomSheetBehavior.setState(BottomSheetBehavior.STATE_HIDDEN);
                break;
            }else if (bottomSheetBehavior.getState() == BottomSheetBehavior.STATE_HIDDEN){
                startActivity(new Intent(ShareMainActivity.this, MainActivity.class));
                finish();
            }
            break;
    }
    return super.onKeyDown(keyCode, event);
}

返回true时有效,但继续返回我的MainActivity,好像我没有else语句一样。我甚至把break放在上面。

我在想什么,因为它将从第一次按键关闭,它应该首先退出BottomSheet(打开),然后在第二次按下时退到我的MainActivity

点击View外的BottomSheet会隐藏MainActivity,并且不会回到我的package Grafiktest; import javax.swing.*; import java.awt.*; public class coolgrafik extends JFrame implements ActionListener { private static final long serialVersionUID = 1L; private JPanel panel; private JLabel label; public coolgrafik(){ //super(title); setSize(400,60); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); panel = new JPanel(); panel.setBackground(Color.GRAY); label = new JLabel("test"); panel.add(label); MyButton button = new MyButton(Color.green, Color.red, "RUN","STOP"); panel.add(button); button.addActionListener(this); add(panel); } public static void main(String[] args){ new coolgrafik().setVisible(true); //toggleState(Color.green, Color.red, "RUN","STOP"); } public void actionPerformed(ActionEvent e) { toggleState(Color.green, Color.red, "RUN","STOP"); } }

我试图通过Debug跟踪错误,它告诉我错误 - 与字节代码不匹配。

error

0 个答案:

没有答案