EditText,当设置为maxLines = 1并且输入被按下时,它将被擦除并崩溃
我有一个关于EditText的问题。做一个简单的数学游戏,玩家将得到这样的例子:9 x 9 = __。空格(= EditText,仅限于数字)是玩家必须从等式中填写正确答案的地方,然后按下[CORRECT] - 按钮来修正等式。EditText,当设置为maxLines = 1并且输入被按下时,它将被擦除并崩溃
问题: 我目前设置了android.maxLines = "1"
。现在每当玩家按下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
尝试这些在一起
android:imeOptions="actionDone"
android:inputType="number"
android:maxLines="1"
我使用Numbers作为'inputType'来防止文本被写入。它是否也适用于数字?编辑:刚才意识到我没有把'inputType'之后我做了一个TextView和EditText之间的交换。让我们尝试一下。 –
现在很好用。我也有上面的设置,没有进一步的问题。 –
那么,当你按下 “ENTER” 键,文字不会被删除,而是向上移动。点击回车键后,按向上箭头键,你会发现你的文字。问题可能在于您为“正确”按钮编写的逻辑。请分享按“正确”按钮时执行的代码
已更新---尝试使用trim()方法删除尾随空格。既然你解析值转换成int,则额外的空间可能是导致该问题的事情是这样的:
Integer.parseInt(editTextEasyResult.getText().toString().trim());
另一个建议是,因为你正在开发一个数学相关的应用程序,你可以限制你的EditText接受号只要。通过提以下属性为您编辑文本文件,这样做的:
android:inputType="number"
这样做会出现数字键盘给用户无需输入密钥。
更新了与“正确按钮”相关的onClick代码。 –
发现这个问题。但是,不知道如何解决它。当你按下回车键时,问题就会出现,就像你说的那样,号码会上移并且看不见。这也创造了一个空间。如果在按下“修正按钮”之前未删除/删除空间,则应用程序停止工作。 –
'trim()'防止应用程序崩溃/停止工作。当我按下Enter键时,如何完全阻止它上升? –
后崩溃日志... – rafsanahmad007
@ rafsanahmad007:这里的崩溃日志: –
@ rafsanahmad007:这里的崩溃日志:[链接] http://sv.tinypic.com/r/29bylw2/9 –