android 取消焦点事件,android – 由于没有窗口焦点而取消事件:MotionEvent

这是故事:

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

W / ViewRootImpl:由于没有窗口焦点而取消事件: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","");

}

}

}

这是生成消息的代码.如果您需要更多信息或希望我详细说明,请告诉我们!

假设Ids是正确的,并且活动中存在程序中的所有视图.似乎只要用户按Enter键或更改焦点,onFocusChange函数就不会运行.

编辑:

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

W/ResourceType: Failure getting entry for 0x01080346 (t=7 e=838) (error -75)

[ 07-13 12:54:05.572 2725: 2725 I/ ]

RAN!

它只在程序开始时发生

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

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">

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" />

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" />

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" />

如果你需要什么,请不要犹豫,问我!

谁能告诉我以下错误是什么意思?当我点击edittext框时它们出来了.

> W / EGL_emulation:eglSurfaceAttrib未实现

> W / OpenGLRenderer:无法在表面0xa9fc6300上设置EGL_SWAP_BEHAVIOR,错误= EGL_SUCCESS

> E / Surface:getSlotFromBufferLocked:未知缓冲区:0xae3f3fb0

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值