java - 单击按钮时开始的 Intent 是什么以及如何在基本数学应用程序中计算分数?

标签 java android

我正在尝试创建一个非常基本的应用程序,用户应该为一个简单的数学问题提供解决方案。例如,数学问题是 10+0=,用户应该输入这两个数字的和。我被困在用户输入答案并单击“确定”按钮的位置。我知道我应该在这里开始某种 Intent ,但我不确定是什么样的 Intent ?我希望应用程序能够判断所提供的解决方案是数学问题的正确还是错误答案,如果正确,则将当前分数加 1(在底部的 TextView 中)并显示一个新的随机数学问题就像已解决(或未解决)的问题一样。

MathActivity.java

public class MathActivity extends Activity {

private MediaPlayer mp;

@Override
protected void onCreate (Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_math);

    //Media player with toggle button
    final ToggleButton togglesound = (ToggleButton) findViewById(R.id.togglesound);
    mp = MediaPlayer.create(this, R.raw.sound);
    togglesound.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            if (togglesound.isChecked()) {
                mp.start();
            } else {
                mp.pause();
            }   
        }
    });

    //Two random integers adding up to the sum of 10
    int min = 1;
    int max = 10;
    Random randomint = new Random();
    int randomint1 = randomint.nextInt(max - min +1) + min;
    int randomint2 = 10 - randomint1;
    //Displays random problem in textview
    TextView displayrandomproblem = (TextView) findViewById(R.id.tvrandomproblem);
    displayrandomproblem.setText(randomint1 + " + " + randomint2 + " = ");

    //Fetches user answer and converts it to an integer
        EditText answer = (EditText)findViewById(R.id.editanswer); {
        if (answer.getText().toString().length() > 0){
            int answerInt = Integer.parseInt(answer.getText().toString());
        }
    //Displaying a toast if no answer is provided
        else
        {
            Toast.makeText(getApplicationContext(), "Please enter your solution to the math problem!",
                   Toast.LENGTH_SHORT).show();
        }
            }
    //When the OK button is clicked
        findViewById(R.id.btnok).setOnClickListener(new OnClickListener() {
            public void onClick(View v) {
                //Submit user's answer
                Intent intent = new Intent();
        }});

}
    //Releasing media player on pause
    @Override
    public void onPause() {
        super.onPause();
        if (mp !=null) {
            mp.release();
            mp = null;
        }
}

}

activity_math.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/bg" >
 <TextView
    android:id="@+id/tvheadline"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentTop="true"
    android:layout_centerHorizontal="true"
    android:text="SPELA"
    android:textColor="@color/gul"
    android:textStyle="bold"
    android:typeface="monospace"
    android:textAppearance="?android:attr/textAppearanceLarge" />
<ToggleButton
    android:id="@+id/togglesound"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentBottom="true"
    android:layout_alignParentRight="true"
    android:textColor="@color/gul"
    android:textOn="Sound is on"
    android:textOff="Sound is off"
    android:text="ToggleButton" />
<TextView
    android:id="@+id/tvrandomproblem"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignBottom="@+id/editanswer"
    android:layout_alignLeft="@+id/tvheadline"
    android:text="x + x ="
    android:textColor="@color/gul" />
<EditText
    android:id="@+id/editanswer"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_below="@+id/tvheadline"
    android:layout_marginTop="58dp"
    android:layout_toRightOf="@+id/tvrandomproblem"
    android:ems="2"
    android:inputType="number"
    android:textColor="@color/gul" >
    <requestFocus />
</EditText>
<Button
    android:id="@+id/btnok"
    style="?android:attr/buttonStyleSmall"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_below="@+id/tvrandomproblem"
    android:layout_centerHorizontal="true"
    android:layout_marginTop="33dp"
    android:text="OK"
    android:textColor="@color/gul" />
<TextView
    android:id="@+id/tvcurrentscore"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignRight="@+id/tvrandomproblem"
    android:layout_below="@+id/btnok"
    android:layout_marginTop="34dp"
    android:text="Current score:"
    android:textColor="@color/gul"
    android:textAppearance="?android:attr/textAppearanceSmall" />

最佳答案

我认为,你不需要使用Intent。它们仅用于应用程序之间的互连或 Activity 之间的数据传输。

据我了解,您的应用程序只有一个 Activity ,因此您只需要永久增加底部的 textedit 的值

EditText 分数 = (EditText)findViewById(R.id.tvcurrentscore); Score.setText(String.valueOf(Integer.valueOf(score.getText) + 1))

关于java - 单击按钮时开始的 Intent 是什么以及如何在基本数学应用程序中计算分数?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23493512/

相关文章:

java - 如何在 Spring Boot 中发送请求范围 bean 作为响应

java - GCP 为 Java 应用程序调用 OOMKiller,但它不会消耗最大允许内存

java - OSX Yosemite - JVM 共享库不包含 JNI_CreateJavaVM 符号

android - Kotlin: 'length' 类型的表达式 'Int' 不能作为函数调用。找不到函数 'invoke()'

android - 使用 Gradle 更改 apk 名称

android - 如何使用 "Taking Action for Users"的辅助功能服务?

java - 将对象从服务发送到应用程序 - 处理程序

java - spring中css和js文件的放置和使用

java - 设置磁盘上的最大缓存大小 Android Universal Image Loader

android - 带有 StickyListHeaders 的 SwipeActionAdapter