Android 由于没有窗口焦点而取消事件:MotionEvent
故事是这样的: 我正试图在Android Studio中构建一个猜谜游戏。其中,用户必须猜测一个介于0和1000之间的数字。每次用户进行猜测时,计算机都会告诉您猜测值是小于实际值(冷)还是大于实际值(热)。我决定让用户在editTextView中输入他的猜测。我将一个Android 由于没有窗口焦点而取消事件:MotionEvent,android,android-studio,Android,Android Studio,故事是这样的: 我正试图在Android Studio中构建一个猜谜游戏。其中,用户必须猜测一个介于0和1000之间的数字。每次用户进行猜测时,计算机都会告诉您猜测值是小于实际值(冷)还是大于实际值(热)。我决定让用户在editTextView中输入他的猜测。我将一个onFocus事件监听器绑定到editTextView,这样当用户按下enter键或点击其他位置时,它会将猜测提交给计算机。我在emulator中运行了代码,从控制台收到一些消息,内容类似于以下内容: W/ViewRootImpl:
onFocus
事件监听器绑定到editTextView,这样当用户按下enter键或点击其他位置时,它会将猜测提交给计算机。我在emulator中运行了代码,从控制台收到一些消息,内容类似于以下内容:
W/ViewRootImpl:由于没有窗口焦点而取消事件:运动事件{action=action\u CANCEL,actionButton=0,id[0]=0,x[0]=605.52246,y[0]=969.4336,工具类型[0]=TOOL\u TYPE\u FINGER,按钮状态=0,标志=0x0,边标记=0x0,指针计数=1,历史大小=0,事件时间=238238,停机时间=235422,设备id=0,源代码=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","");
}
}
}
它只在程序开始时发生一次
以下代码提供了代码链接到的活动
<?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>
如果您有什么需要,请尽管问我
谁能告诉我下面的错误是什么意思?当我点击编辑文本框时,它们就会出来
W/EGL\u仿真:未实现eglSurfaceAttrib
W/OpenGLRenderer:未能在表面0xa9fc6300上设置EGL_交换行为,错误=EGL_成功
E/Surface:getSlotFromBufferLocked:未知缓冲区:0xae3f3fb0
onKeyUp(int keyCode, KeyEvent event)
请遵循以下步骤,并让我知道它是否有效
如果您有任何疑问或需要代码片段,请告诉我。您好,您有任何解决方案吗
onKeyUp(int keyCode, KeyEvent event)