2017-03-05 1 views
0

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 
+0

das Crash-Protokoll schreiben ... – rafsanahmad007

+0

@ rafsanahmad007: Hier ist das Crash-Protokoll: –

+0

@ rafsanahmad007: Hier ist der Crash-Log: [link] http://sv.tinypic.com/r/29bylw2/9 –

Antwort

1

versuchen, diese zusammen

  android:imeOptions="actionDone" 
      android:inputType="number" 
      android:maxLines="1" 
+0

Ich benutze Zahlen als 'inputType', um zu verhindern, dass Text geschrieben wird. Funktioniert es auch mit Zahlen? EDIT: Ich habe gerade festgestellt, dass ich den 'inputType' nicht eingefügt habe, nachdem ich einen TextView & EditText getauscht habe. Versuchen wir es auch. –

+0

Funktioniert jetzt gut. Ich habe auch die Einstellungen oben und kein weiteres Problem. –

1

Nun, wenn Sie auf "Enter" -Taste drücken, wird der Text nicht gelöscht bekommen, ist es eher schaltet hoch. Nachdem Sie die Eingabetaste gedrückt haben, drücken Sie die Pfeiltaste nach oben und Sie werden Ihren Text finden. Das Problem könnte mit der Logik sein, die Sie für die Schaltfläche "Korrigieren" geschrieben haben. Bitte teilen Sie den Code, der beim Drücken der Taste "Korrigieren"

AKTUALISIERT --- Verwenden Sie trim() -Methode, um nachstehende Leerzeichen zu entfernen. Da Sie den Wert in einem int parsen, könnte der zusätzliche Raum, das Problem so etwas wie dies verursacht:

Integer.parseInt(editTextEasyResult.getText().toString().trim()); 

Ein weiterer Vorschlag ist, da Sie eine Mathematik im Zusammenhang App entwickeln, können Sie Ihre EditText beschränken können Zahlen akzeptieren nur. Tun Sie dies, indem Sie das folgende Attribut für Ihre bearbeiten Textdatei zu erwähnen:

android:inputType="number" 

Dadurch wird eine numerische Tastatur für den Benutzer darstellen, ohne die Enter-Taste.

+0

Die onClick-Codes für den "Korrekt-Button" wurden aktualisiert. –

+0

Das Problem wurde gefunden. Ich weiß jedoch nicht, wie ich es lösen soll. Das Problem tritt auf, wenn Sie die Eingabetaste drücken. Genau wie Sie angegeben haben, wird die Nummer hochgeschaltet und ist nicht mehr zu sehen. Dies schafft auch einen Raum. Wenn der Space nicht entfernt/gelöscht wird, bevor der "Korrekt-Button" gedrückt wird, funktioniert die App nicht mehr. –

+0

Das 'trim()' verhindert, dass die App abstürzt/nicht mehr funktioniert. Wie verhindere ich, dass es nach oben geht, wenn ich die Enter-Taste drücke? –

Verwandte Themen