我目前正在制作一个非常简单的数学游戏。在这个数学游戏中,我希望玩家解决简单的方程式。它看起来像这样:9 * __ = 45,玩家然后填写正确的数字来解决方程,然后按下正确按钮。如果正确的分数被添加到播放器。
空白区域是EditText,其他是TextViews。因为我混合使用TextView& EditText我需要以某种方式为程序进行转换以便能够读取和计算。我一直在疯狂阅读,尝试各种不同的方法但没有成功。我该怎么办呢?
这就是我的数据:
activity_play.xml
<?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: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.PlayActivity">
<TextView
android:id="@+id/textPlayMultiply"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignTop="@+id/textPlayNumber1"
android:layout_centerHorizontal="true"
android:text="@string/multiply"
android:textAlignment="textStart"
android:layout_gravity = "start"
android:textColor="@android:color/black"
android:textSize="40sp" />
<TextView
android:id="@+id/textPlayNumber1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignEnd="@+id/textPlayScore"
android:layout_alignParentTop="true"
android:layout_alignRight="@+id/textPlayScore"
android:layout_marginTop="48dp"
android:text="@string/number_1"
android:textAlignment="center"
android:textColor="@android:color/black"
android:textSize="50sp" />
<TextView
android:id="@+id/textPlayEqual"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="16dp"
android:text="@string/equal"
android:textAlignment="center"
android:textColor="@android:color/black"
android:textSize="50sp"
android:layout_below="@+id/editTextPlayNumber2"
android:layout_alignLeft="@+id/textPlayMultiply"
android:layout_alignStart="@+id/textPlayMultiply" />
<TextView
android:id="@+id/textPlayResult"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="29dp"
android:text="@string/result_number3"
android:textAlignment="center"
android:textColor="@android:color/black"
android:textSize="50sp"
android:layout_below="@+id/textPlayEqual"
android:layout_alignLeft="@+id/textPlayEqual"
android:layout_alignStart="@+id/textPlayEqual" />
<TextView
android:id="@+id/textPlayScore"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_marginBottom="31dp"
android:layout_toStartOf="@+id/answerButton"
android:text="@string/score_0"
android:textAlignment="textStart"
android:layout_gravity="start"
android:textColor="@android:color/black"
android:textSize="24sp"
android:layout_toLeftOf="@+id/answerButton" />
<Button
android:id="@+id/answerButton"
android:layout_width="120dp"
android:layout_height="wrap_content"
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"
android:layout_below="@+id/textPlayResult"
android:layout_centerHorizontal="true" />
<EditText
android:id="@+id/editTextPlayNumber2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="@+id/textPlayMultiply"
android:layout_alignBottom="@+id/textPlayMultiply"
android:layout_marginLeft="16dp"
android:layout_marginStart="16dp"
android:layout_toEndOf="@+id/answerButton"
android:layout_toRightOf="@+id/answerButton"
android:hint=" "
android:inputType="number"
android:textAlignment="center"
android:textColor="@android:color/black"
android:textSize="50sp" />
<TextView
android:id="@+id/textPlayLevel"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="@+id/textPlayScore"
android:layout_alignBottom="@+id/textPlayScore"
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>
PlayActivity.java
package com.example.android.laboration2;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
public class PlayActivity extends AppCompatActivity implements View.OnClickListener {
TextView textPlayNumber1;
EditText editTextPlayNumber2;
TextView textPlayResult;
TextView textPlayScore;
TextView textPlayLevel;
Button answerButton;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_play);
textPlayNumber1 = (TextView) findViewById(R.id.textPlayNumber1);
editTextPlayNumber2 = (EditText) findViewById(R.id.editTextPlayNumber2);
textPlayResult = (TextView) findViewById(R.id.textPlayResult);
textPlayScore = (TextView) findViewById(R.id.textPlayScore);
textPlayLevel = (TextView) findViewById(R.id.textPlayLevel);
answerButton = (Button) findViewById(R.id.answerButton);
answerButton.setOnClickListener(this);
}//onCreate ends here
@Override
public void onClick(View v) {
}//onClick ends here
}//PlayActivity ends here
答案 0 :(得分:2)
你可以做到
int playNumberValue = Integer.getInteger(textPlayNumber1.getText().toString());
int userInputValue = Integer.getInteger(editTextPlayNumber2.getText().toString());
int result = Integer.getInteger(textPlayResult.getText().toString());
if(result == userInputValue+playNumberValue)
//win game
答案 1 :(得分:1)
您的TextView和EditText都具有String类型值。您必须将这些值解析为Integer,然后计算并显示结果。
以下是onClick answer按钮的操作 -
int score = 0;
@Override
public void onClick(View v) {
switch(v.getId()){
case R.id.answerButton:
int playNum1 = Integer.parseInt(textPlayNumber1.getText().toString());
int playNum2 = Integer.parseInt(editTextPlayNumber2.getText().toString());
int playResult = Integer.parseInt(textPlayResult.getText().toString());
if(playNum1*playNum2 == playResult){
score++;
textPlayScore.setText(""+score);
}
break;
}
}
希望这有帮助。