[英]EditText, when setting to maxLines = 1 and enter is pressed it erases and crashes
我有關於EditText的問題。 做一個簡單的數學游戲,玩家會得到一個這樣的例子:9 x 9 = __。 空格(= EditText,僅限於數字)是玩家必須從公式中填寫正確答案並按[CORRRECT]按鈕校正公式的地方。
問題:我目前已設置android.maxLines = "1"
。 現在,每當玩家按下Enter鍵時,任何書面文字都會被刪除。 如果重新輸入並按“更正”按鈕,則應用程序將停止運行。 但是,只要您不按Enter鍵,使用“正確”按鈕就可以正常運行該應用程序。 如何防止它崩潰/停止工作? 但是,也可以在按Enter鍵時阻止它擦除任何數字。
XML檔案:
<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文件:
@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
好吧,當您按“輸入”鍵時,文本不會被擦除,而是向上移動。 按下Enter鍵后,按向上箭頭鍵,您將找到您的文本。 問題可能出在您為“正確”按鈕編寫的邏輯上。 請分享按下“更正”按鈕后執行的代碼
更新---嘗試使用trim()方法刪除尾隨空格。 由於您要將值解析為int,因此多余的空間可能會引起問題,如下所示:
Integer.parseInt(editTextEasyResult.getText().toString().trim());
另一個建議是,由於您正在開發與數學相關的應用程序,因此可以將EditText限制為僅接受數字。 為此,請為您的編輯文本文件提及以下屬性:
android:inputType="number"
這樣做會將數字鍵盤呈現給用戶,而無需使用Enter鍵。
一起嘗試
android:imeOptions="actionDone"
android:inputType="number"
android:maxLines="1"
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.