简体   繁体   English

Android-自定义按钮卡在“按下”位置

[英]Android - Custom Button gets stuck “Pressed”

I've created a custom button in my Android app that has basically two different views. 我在Android应用程序中创建了一个自定义按钮,该按钮基本上具有两个不同的视图。 There is an image for when the button isn't being pressed and another image for use while it is being pressed. 有一个图像用于未按下按钮,另一图像用于按下按钮。 Below is how I've implemented the button and how it responds to the user. 以下是我实现按钮的方式以及它对用户的响应。

private void registerListeners() {

    calcButton.setOnTouchListener(new View.OnTouchListener() {

        @Override
        public boolean onTouch(View v, MotionEvent event) {
            calcButton.requestFocusFromTouch();
            calcButton.setImageResource(R.drawable.calc_button_pressed);
            return false;
        }
    });

    calcButton.setOnFocusChangeListener(new View.OnFocusChangeListener() {

        @Override
        public void onFocusChange(View v, boolean hasFocus) {
            calcButton.setImageResource(R.drawable.calc_button_not_pressed);
        }
    });

    calcButton.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {           
            mathCalculation();
            calcButton.setImageResource(R.drawable.calc_button_not_pressed);
        }
    });
}

My problem is that there is a "bug" where if the user touches the button and drags their figure off the button the button stay pressed down. 我的问题是存在一个“错误”,如果用户触摸该按钮并将其图形拖离该按钮,该按钮将保持按下状态。 The one work around I've implemented above is the "setOnFocusChangeListener" so once the user select something it else it will pop back up. 我上面实现的一种解决方法是“ setOnFocusChangeListener”,因此一旦用户选择了其他内容,它就会弹出。

I want to have it so the button pops back out when the user drags there touched figure off the button. 我想要它,以便当用户将按钮拖到那里时,按钮弹出来。

All suggestions are greatly appreciated!!! 所有建议,不胜感激!!!

Thank you, 谢谢,

You don't need to write a separate button class to get that behavior, you can implement it through a drawable xml. 您无需编写单独的按钮类即可获得该行为,您可以通过可绘制的xml实现它。 Have this in your xml and set it as the background for your button: 在您的xml中保存此文件,并将其设置为按钮的背景:

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:state_pressed="true"
      android:drawable="@drawable/calc_button_pressed" />
    <item android:drawable="@drawable/calc_button_not_pressed" />
</selector>

In your onTouch method, check for the event action. 在您的onTouch方法中,检查事件操作。

  • If it's MotionEvent.ACTION_DOWN, then proceed as you have. 如果是MotionEvent.ACTION_DOWN,请照常进行。
  • If it's ACTION_UP or ACTION_CANCEL, then call calcButton.setImageResource(R.drawable.calc_button_not_pressed); 如果是ACTION_UP或ACTION_CANCEL,则调用calcButton.setImageResource(R.drawable.calc_button_not_pressed);

http://developer.android.com/reference/android/view/MotionEvent.html http://developer.android.com/reference/android/view/MotionEvent.html

You Could use use onTouch . 您可以使用use onTouch Then use the ACTION_OUTSIDE which is fired when the users touch moves out the the bound of the view. 然后使用当用户触摸时将触发的ACTION_OUTSIDE移出视图边界。

Edit 1: to be more specific: 编辑1:更具体:

OnTouchListener(MotionEvent e) {
   switch(e.getAction()) {
      case MotionEvent.ACTION_OUTSIDE: // switch the image if the button
    }
}

It's because the click event "happens" only when touching and releasing . 这是因为单击事件仅在触摸和释放时才“发生”。 If you're touching and moving outside that's not a click. 如果您要触摸并移到外面,那不是单击。

To do what you want use a StateListDrawable , it's made just for that. 要使用StateListDrawable完成您想要的操作 ,它就是为此而创建的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM