ここに物語があります:ウィンドウフォーカスなしのイベントキャンセル:MotionEvent
私はAndroid Studioで推測ゲームを構築しようとしています。ユーザーが推測するたびに、推測が実際の数値(Cold)よりも小さいか、または実際の数値(Hot)よりも大きいかどうかがコンピュータによって通知されます。私はユーザーにeditTextViewの中に自分の推測を入力させることに決めました。 onFocus
イベントリスナーをeditTextViewにバインドして、ユーザーが他の場所に入るかタップすると推測をコンピューターに送信するようにしました。私はエミュレータでコードを実行し、私は次のようなことを言うのコンソールからいくつかのメッセージを受け取っ:
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","");
}
}
}
これは、メッセージを生成するコードです。何か詳しい情報が必要な場合や、何かを詳しく説明したい場合は、私に知らせてください!
IDが正しいことと、プログラム内にあるすべてのビューがアクティビティに存在すると仮定します。ユーザーがEnterキーを押すかフォーカスを変更するたびに、機能onFocusChange
が実行されていないようです。
EDIT: edittext要素でフォーカス可能なプロパティを有効にしたときに表示される新しいエラーがあります。ここでのエラーは、次のとおりです。
W/ResourceType: Failure getting entry for 0x01080346 (t=7 e=838) (error -75)
[ 07-13 12:54:05.572 2725: 2725 I/ ]
RAN!
それが唯一の次のコードは、そのコードのリンクにアクティビティを提供するプログラム
の開始時にONCE起こります。
<?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>
もしあなたが必要とするものがあれば、私に尋ねてください!
誰でも次のエラーの意味を教えていただけますか? edittextボックスをクリックすると、それらが出てきます。
W/EGL_emulation: eglSurfaceAttrib not implemented
W/OpenGLRenderer: Failed to set EGL_SWAP_BEHAVIOR on surface 0xa9fc6300, error=EGL_SUCCESS
E/Surface: getSlotFromBufferLocked: unknown buffer: 0xae3f3fb0