由于没有窗口焦点而取消事件:MotionEvent [英] Cancelling event due to no window focus : MotionEvent

查看:87
本文介绍了由于没有窗口焦点而取消事件:MotionEvent的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是故事:

我正在尝试在Android Studio中构建一个猜谜游戏.用户必须猜测一个介于0到1000之间的数字.每次用户进行猜测时,计算机都会告诉您该猜测是小于实际数字(冷)还是大于实际数字(热).我决定让用户在editTextView中输入他的猜测.我将onFocus事件侦听器绑定到editTextView,以便当用户按Enter或轻按其他位置时,它将猜测提交给计算机.我在模拟器中运行了代码,并从控制台收到了一些消息,内容类似于以下内容:

I am trying to build a guessing game in Android Studio. Where the user has to guess a number that resides between 0 and 1000. Every time the user makes a guess, the computer will tell you if the guess is smaller than the actual number(Cold) or greater than the actual number(hot). I decided to have the user enter his guess inside of an editTextView. I binded an onFocus event listener to the editTextView so that when the user presses enter or taps somewhere else, it submits the guess to the computer. I ran the code in the emulator and I recieved some messages from the console say things similar to the following:

W/ViewRootImpl: Cancelling event due to no window focus: MotionEvent { action=ACTION_CANCEL, actionButton=0, id[0]=0, x[0]=605.52246, y[0]=969.4336, toolType[0]=TOOL_TYPE_FINGER, buttonState=0, metaState=0, flags=0x0, edgeFlags=0x0, pointerCount=1, historySize=0, eventTime=238238, downTime=235422, deviceId=0, source=0x1002 }

public class GameActivity extends AppCompatActivity implements View.OnFocusChangeListener {
    public final int MAXNUMBER = 1000;
    private int theAnswer; // stores the number the user has to guess
    public int guesses = 0;
    public EditText userGuess;
    public TextView hotOrCold;
    public String isHotOrCold(int userInput){
        return theAnswer < userInput?"Hot":"Cold"; // Utility to tell the user if his guess is greater than or less than the number.
    }
    public boolean isValidInput(int num){
        return num >= 0 && num <= MAXNUMBER;
    }
    public void resetGame(){
        guesses = 0;
        theAnswer = (int) (Math.random() * (MAXNUMBER + 1));
    }
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_game);

        userGuess = (EditText)findViewById(R.id.userGuess);
        hotOrCold = (TextView) findViewById(R.id.hotOrCold);
        resetGame();
        userGuess.setOnFocusChangeListener(this);
    }

    @Override
    public void onFocusChange(View v, boolean hasFocus) {
        /* When focus is lost check that the text field
        * has valid values.
        */
        Log.i("","RAN!");
        if (!hasFocus) {
            guesses++;
            int userInput = Integer.valueOf(userGuess.getText().toString()); // get user input
            Log.i("userInput is: ",userInput+"");
            if (isValidInput(userInput)){
                if (theAnswer == userInput){
                    Toast.makeText(getApplicationContext(),"Congrats! The number was: "+userInput+". You got it in " + guesses + " guesses!",Toast.LENGTH_LONG); // Tell user he got it right
                    hotOrCold.setText("Hot Or Cold"); // set text to default
                    resetGame();
                } else {
                    hotOrCold.setText(isHotOrCold(userInput)); // Tells user if he is hot or cold.
                }
            } else {
                // do nothing
            }
        } else {
            Log.i("Did Run","");
        }
    }
}

这是产生消息的代码.如果您需要更多信息或希望我详细介绍一些内容,请告诉我!

This is the code that produces the message. If you require any more information or would like me to elaborate on something, please let me know!

假设ID是正确的,并且活动中存在程序中的所有视图.每当用户按下Enter或更改焦点时,功能onFocusChange似乎都没有运行.

Assume Ids are correct, and all views that are in the program exist in the activity. It seems like the function onFocusChange is not running whenever the user presses enter or changes the focus.

当我打开edittext元素上的focusable属性时,出现了一个新错误.这是错误:

I have a new error that showed up when I turned on the focusable property on the edittext element. here is the error:

W/ResourceType: Failure getting entry for 0x01080346 (t=7 e=838) (error -75)
    [ 07-13 12:54:05.572  2725: 2725 I/         ]
    RAN!

仅在程序开始时发生

以下代码提供了代码链接到的活动.

The following code provides the activity that the code links to.

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context="com.example.alexander.guessinggame.GameActivity">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textAppearance="?android:attr/textAppearanceLarge"
        android:text="Hot or Cold"
        android:id="@+id/hotOrCold"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true"
        android:textIsSelectable="false"
        android:textSize="50sp" />

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textAppearance="?android:attr/textAppearanceSmall"
        android:text="*Hot:too big, cold:too small"
        android:id="@+id/textView3"
        android:layout_alignParentBottom="true"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true"
        android:textIsSelectable="false"
        android:textSize="17sp" />

    <EditText
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:inputType="number"
        android:ems="10"
        android:id="@+id/userGuess"
        android:layout_above="@+id/textView3"
        android:layout_centerHorizontal="true"
        android:layout_marginBottom="111dp"
        android:numeric="integer"
        android:selectAllOnFocus="true"
        android:editable="true"
        android:focusableInTouchMode="true"
        android:focusable="true"
        android:enabled="true"
        android:singleLine="true"
        android:clickable="true"
        android:maxLength="4" />

</RelativeLayout>

如果您有任何需要,请随时问我!

If there is anything that you need do not hesitate to ask me!

谁能告诉我以下错误是什么意思?当我单击Edittext框时,它们就会出现.

Can anyone tell me what the following errors mean? They come out when I click on the edittext box.

  1. W/EGL_emulation: eglSurfaceAttrib not implemented
  2. W/OpenGLRenderer: Failed to set EGL_SWAP_BEHAVIOR on surface 0xa9fc6300, error=EGL_SUCCESS
  3. E/Surface: getSlotFromBufferLocked: unknown buffer: 0xae3f3fb0
  1. W/EGL_emulation: eglSurfaceAttrib not implemented
  2. W/OpenGLRenderer: Failed to set EGL_SWAP_BEHAVIOR on surface 0xa9fc6300, error=EGL_SUCCESS
  3. E/Surface: getSlotFromBufferLocked: unknown buffer: 0xae3f3fb0

推荐答案

请按照以下步骤操作,并让我知道它是否有效

Please follow below steps and let me know if it works

  1. 覆盖要检查是否为Tab或按下Enter的dispatchKeyEvent.
  2. 如果按下Tab或Enter键,并且EditTextView具有焦点,请提交猜测.

如果您有任何疑问或需要代码段,请告诉我.

Let me know if you have any doubts or need a code snippet.

这篇关于由于没有窗口焦点而取消事件:MotionEvent的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆