Ich habe ein Problem in Bezug auf die EditText. Ein einfaches Mathe-Spiel, bei dem der Spieler ein Beispiel wie folgt bekommt: 9 x 9 = __. Der leere Bereich (= EditText, nur auf Zahlen beschränkt) ist der Punkt, an dem der Spieler die korrekte Antwort aus der Gleichung ausfüllen muss und einen [CORRECT] -Knopf drücken muss, um die Gleichung zu korrigieren.EditText, wenn auf maxLines = 1 und Eingabe ist gedrückt es löscht und stürzt ab
Problem: ich zur Zeit android.maxLines = "1"
gesetzt haben. Jedes Mal, wenn der Spieler die Eingabetaste drückt, wird jeder geschriebene Text gelöscht. Wenn Sie erneut tippen und die Korrekt-Taste drücken, funktioniert die App nicht mehr. Solange Sie nicht die Eingabetaste drücken, funktioniert die App problemlos mit der Korrekt-Taste. Wie verhindere ich, dass es abstürzt/nicht mehr funktioniert? Aber verhindern Sie auch das Löschen von Zahlen, wenn Sie die Eingabetaste drücken.
XML-Datei:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/activity_play"
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.android.laboration2.EasyActivity">
<TextView
android:id="@+id/textEasyMultiply"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignTop="@+id/textEasyNumber1"
android:layout_centerHorizontal="true"
android:layout_gravity="start"
android:text="@string/multiply"
android:textAlignment="textStart"
android:textColor="@android:color/black"
android:textSize="40sp" />
<TextView
android:id="@+id/textEasyNumber1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_marginEnd="25dp"
android:layout_marginRight="25dp"
android:layout_marginTop="49dp"
android:layout_toLeftOf="@+id/answerButton"
android:layout_toStartOf="@+id/answerButton"
android:text="@string/number_1"
android:textAlignment="center"
android:textColor="@android:color/black"
android:textSize="50sp" />
<TextView
android:id="@+id/textEasyEqual"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/textEasyNumber1"
android:layout_centerHorizontal="true"
android:layout_marginTop="18dp"
android:text="@string/equal"
android:textAlignment="center"
android:textColor="@android:color/black"
android:textSize="50sp" />
<EditText
android:id="@+id/editTextEasyResult"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/textEasyEqual"
android:layout_centerHorizontal="true"
android:hint=" "
android:maxLines="1"
android:textAlignment="center"
android:textColor="@android:color/black"
android:textSize="50sp" />
<TextView
android:id="@+id/textEasyScore"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_gravity="start"
android:layout_marginBottom="31dp"
android:layout_toLeftOf="@+id/answerButton"
android:layout_toStartOf="@+id/answerButton"
android:text="@string/score_0"
android:textAlignment="textStart"
android:textColor="@android:color/black"
android:textSize="24sp" />
<Button
android:id="@+id/answerButton"
android:layout_width="120dp"
android:layout_height="wrap_content"
android:layout_below="@+id/editTextEasyResult"
android:layout_centerHorizontal="true"
android:layout_marginTop="36dp"
android:background="@android:color/holo_orange_dark"
android:text="@string/button_result"
android:textAllCaps="false"
android:textColor="@android:color/white"
android:textSize="18sp"
android:textStyle="bold" />
<TextView
android:id="@+id/textEasyNumber2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="@+id/textEasyEqual"
android:layout_alignEnd="@+id/textEasyLevel"
android:layout_alignRight="@+id/textEasyLevel"
android:layout_marginEnd="12dp"
android:layout_marginRight="12dp"
android:inputType="number"
android:text="@string/number_2"
android:textAlignment="center"
android:textColor="@android:color/black"
android:textSize="50sp" />
<TextView
android:id="@+id/textEasyLevel"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="@+id/textEasyScore"
android:layout_alignBottom="@+id/textEasyScore"
android:layout_toEndOf="@+id/answerButton"
android:layout_toRightOf="@+id/answerButton"
android:text="@string/level_0"
android:textAlignment="center"
android:textColor="@android:color/black"
android:textSize="24sp" />
</RelativeLayout>
JAVA-Datei:
@Override
public void onClick(View v) {
switch(v.getId()){
case R.id.answerButton:
int easyNum1 = Integer.parseInt(textEasyNumber1.getText().toString());
int easyNum2 = Integer.parseInt(textEasyNumber2.getText().toString());
int easyResult = Integer.parseInt(editTextEasyResult.getText().toString());
if(easyNum1 * easyNum2 == easyResult){
currentScore++;
currentLevel++;
textEasyScore.setText("Score: " + currentScore);
textEasyLevel.setText("Level: " + currentLevel);
Toast.makeText(getApplicationContext(), "Good job!", Toast.LENGTH_LONG).show();
editTextEasyResult.setText("");
}else{
currentScore = 0;
currentLevel = 0;
Toast.makeText(getApplicationContext(),"Wrong! :(", Toast.LENGTH_LONG).show();
}
//Updates Scores & Level
textEasyScore.setText("Score: " + currentScore);
textEasyLevel.setText("Level: " + currentLevel);
break;
}//switch ends here
//stores the Score into the High Score page, when new High Score is reached it will auto-update
SharedPreferences sharedPrefsHighScore = getSharedPreferences("Prefs_HighScore",MODE_PRIVATE);
SharedPreferences.Editor editorScore = sharedPrefsHighScore.edit();
int storedHighScore = sharedPrefsHighScore.getInt("highScore",0);
if (currentScore>storedHighScore) {
editorScore.putInt("highScore", currentScore);
editorScore.commit();
//if a new High Score is achieved, the toastmessage "NEW HIGH SCORE!" will be shown (with modifications)
Toast highScoreToast = Toast.makeText(getApplicationContext(), "NEW HIGH SCORE!", Toast.LENGTH_LONG);
TextView toastMessage = (TextView) highScoreToast.getView().findViewById(android.R.id.message);
toastMessage.setTextColor(Color.WHITE);
toastMessage.setTextSize(25);
highScoreToast.show();
}//if-statement ends here
randomNumbersForEquation();
}//onClick ends here
//adds a random number for our Equation (for textEasyNumber1 and textEasyNumber2)
void randomNumbersForEquation(){
int addingOneTocurrentLevel = currentLevel + 1;
int numberRange = addingOneTocurrentLevel * 3;
Random randInt = new Random();
int Number1 = randInt.nextInt(numberRange);
Number1++;//don't want a zero value
int Number2 = randInt.nextInt(numberRange);
Number2++;//don't want a zero value
textEasyNumber1.setText("" + Number1);
textEasyNumber2.setText("" + Number2);
}//setQuestion ends here
das Crash-Protokoll schreiben ... – rafsanahmad007
@ rafsanahmad007: Hier ist das Crash-Protokoll: –
@ rafsanahmad007: Hier ist der Crash-Log: [link] http://sv.tinypic.com/r/29bylw2/9 –