Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/209.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 在安卓系统中,当用户按下按钮时,手指会从按钮外移动_Android_Motionevent - Fatal编程技术网

Android 在安卓系统中,当用户按下按钮时,手指会从按钮外移动

Android 在安卓系统中,当用户按下按钮时,手指会从按钮外移动,android,motionevent,Android,Motionevent,假设我有一个注册按钮类: public class SignUp extends AppCompatButton { public SignUp(Context context) { this(context, null); } public SignUp(Context context, AttributeSet attrs) { super(context, attrs, android.support.v7.appcompat.R.attr.buttonStyle);

假设我有一个注册按钮类:

public class SignUp extends AppCompatButton {
public SignUp(Context context) {
    this(context, null);
}

public SignUp(Context context, AttributeSet attrs) {
    super(context, attrs, android.support.v7.appcompat.R.attr.buttonStyle);
    setFocusable(true);
    setFocusableInTouchMode(true);
}

@Override
public boolean onTouchEvent(MotionEvent event) {
    super.onTouchEvent(event);
    switch (event.getAction()) {
        case MotionEvent.ACTION_DOWN:
            requestFocus();
            this.setBackgroundColor(getResources().getColor(R.color.darkGreen));
            return true;
        case MotionEvent.ACTION_UP:
            this.setBackgroundColor(getResources().getColor(R.color.green));
            performClick();
            return true;
    }
    return false;
}

@Override
public boolean performClick() {
    super.performClick();
    return false;
    //TODO
}
我在我的应用程序中有一个名为“注册”的按钮,我在XML文件中这样声明:

        <com.example.John.myapplication.SignUp
        android:layout_width="150dp"
        android:layout_height="wrap_content"
        android:text="@string/sign_up"
        android:background="@color/green"
        android:textColor="@color/whiteText"
        app:layout_constraintTop_toBottomOf="@+id/verify_password"
        android:layout_marginTop="40dp"
        app:layout_constraintLeft_toRightOf="parent"
        app:layout_constraintRight_toLeftOf="parent"
        android:id="@+id/sign_up"
        android:textSize="20sp"
        />

现在,如果用户触摸注册按钮,按钮的颜色将为深绿色,当用户释放按钮时,按钮的颜色将再次为绿色。但我想添加这个功能,这样当用户触摸按钮,然后将手指从按钮中拉出时,按钮颜色会变为绿色,但我不能。 没有MotionEvent.ACTION\u外部,MotionEvent.ACTION\u取消并。。。作品 我该怎么办


我不想通过协调来检查手指是否在按钮外,因为当按钮呈椭圆形时,这是一项巨大的工作。

这里不需要听众(Action.DOWN不需要拖拽),请浏览本节,如果需要更多指导,请告诉我->


这->->似乎是一个很好的例子。

只需使用
选择器即可。

范例

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">

    <item android:state_selected="true" android:color="@color/colorTextPrimaryDark" />
    <item android:color="@color/colorAccent3" />

</selector>

您可以使用最新支持库中引入的最新组件。默认情况下,按钮的背景色是您的主题强调色

试试这个

<android.support.design.button.MaterialButton
    android:id="@+id/btn__next"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginEnd="8dp"
    android:layout_marginRight="8dp"
    android:layout_marginTop="8dp"
    app:backgroundTint="@color/colorAccent" /* Change colour for background */
    app:rippleColor="@color/colorAccent" /* Change colour for selecting button */
    android:text="@string/next" /> 

使用查看边界来比较触摸事件边界,从而共享修改后的代码

private Rect buttonBounds;

@Override
public boolean onTouchEvent(MotionEvent event) {
    super.onTouchEvent(event);
    switch (event.getAction()) {
         case MotionEvent.ACTION_DOWN:
             buttonBounds = new Rect(getLeft(), getTop(), getRight(), getBottom());
             requestFocus();
             this.setBackgroundColor(getResources().getColor(R.color.darkGreen));
             return true;
         case MotionEvent.ACTION_UP:
            this.setBackgroundColor(getResources().getColor(R.color.green));
            performClick();
            return true;
         case MotionEvent.ACTION_MOVE:
             if(!buttonBounds.contains(getLeft() + (int) event.getX(), getTop() + (int) event.getY())){
                // User moved outside bounds
                this.setBackgroundColor(getResources().getColor(R.color.green));
                return true;
             }

     }
     return false;
 }

我在drawable文件夹的XML文件中添加了选择器,但当我使用以下命令android:background=“@drawable/color\u selector”作为我的按钮背景色时,它会给出一个异常。