Android Can';t混合使用TextView和EditText进行计算

Android Can';t混合使用TextView和EditText进行计算,android,Android,我正在做一个非常简单的数学游戏。在这个数学游戏中,我想让玩家解简单的方程。看起来是这样的:9*u u;=45,玩家然后填写正确的数字来解方程,然后按下正确的按钮。如果玩家获得正确的分数 空白区域是EditText,其他区域是TextView。因为我混合使用了TextView和EditText,所以我需要进行某种转换,以便程序能够读取和计算。我一直在疯狂地阅读,尝试了各种不同的方法,但都没有成功。我该怎么做才能避开这件事 我的数据是这样的: 活动\u play.xml <?xml vers

我正在做一个非常简单的数学游戏。在这个数学游戏中,我想让玩家解简单的方程。看起来是这样的:9*u u;=45,玩家然后填写正确的数字来解方程,然后按下正确的按钮。如果玩家获得正确的分数

空白区域是EditText,其他区域是TextView。因为我混合使用了TextView和EditText,所以我需要进行某种转换,以便程序能够读取和计算。我一直在疯狂地阅读,尝试了各种不同的方法,但都没有成功。我该怎么做才能避开这件事

我的数据是这样的:

活动\u 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>

TextView和EditText都有字符串类型的值。您必须将这些值解析为整数,然后计算并显示结果

这是单击回答按钮的操作-

 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;
       }
  }
希望这有帮助。

你能做到

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

你怎么说不能把它们混在一起?你甚至没有显示你的计算代码。我没有显示,因为我曾经尝试过的方法根本不起作用,因为它总是抱怨TextView使用Int,而EditText是字符串。所以我抹去了一切,希望有人能告诉我如何计算。有什么建议吗?然后发布您的代码,我们不能简单地假设,如果您提取了这些元素的正确值并正确地解析了它们,那么这两种方法在技术上都应该有效。TextView值怎么可能是Int?请解释一下。它显示的是一个整数,但实际值是一个字符串。只是我是一个完全困惑的初学者!使用安卓系统只工作了大约1个月(只有部分时间)。所以我还有很多东西要学。我为术语的混乱道歉。这帮了我很大的忙!太感谢你了!我的问题是,我认为我需要转换其中的一个,因此我总是出错。就像你在上面做的那样,我只需要把它们解析成整数,因为它们都是字符串。我总是把EditText误认为int,因为那里只能写数字。在之前的帖子中读到一些关于它的信息。不客气。我明白是什么把你弄糊涂了。将EditText输入类型设置为数字。为方便用户,这仅影响软输入法(键盘)。但是你得到的值总是一个字符串。你可以在这里读到:老实说,Android开发者的网站很糟糕。我认为他们需要重写所有内容,或者为初学者制作一个易于理解的版本。当他们解释如何将不同的代码与其他代码结合使用,以及他们实际做了什么时,我几乎一无所获。再次感谢你们!这让我好几天都很痛苦!每天花6小时阅读和编码,阅读和编码。我受够了,决定自己开账户。现在我觉得自己像个白痴,因为我没有早点做。帮助在几分钟内就来了,活动非常活跃!你能帮我解决另一个问题吗?如何存储高分?我正在做一个简单的数学应用程序,我想在另一个活动中存储高分。使用上面的代码,我如何在其他活动中存储我的分数并将其显示在高分文本视图中?这也是我的想法。起初我想给你发一条私人信息询问此事,但显然这是不可能的。那我就另提一个问题。非常感谢你的帮助。非常感谢。然而,上面的帖子正是我想要使用的。
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