gpt4 book ai didi

android - 无法混合使用 TextView 和 EditText 进行计算

转载 作者:行者123 更新时间:2023-11-29 17:01:33 25 4
gpt4 key购买 nike

我目前正在制作一个非常简单的数学游戏。在这个数学游戏中,我希望玩家能够解决简单的方程式。看起来像这样:9 * __ = 45,然后玩家填写正确的数字来解方程,然后按下正确按钮。如果正确的分数被添加到播放器。

空白处是一个EditText,其他是TextView。因为我混合使用了 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

最佳答案

你可以做到

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

关于android - 无法混合使用 TextView 和 EditText 进行计算,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42600259/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com