簡體   English   中英

如何在 TextView 元素中設置 integer 的值?

[英]How to set value of integer inside of TextView element?

我想達到什么目的?

  • 每次生成int xint y的新值時,我都希望有一個TextView元素更新。
  • 我希望Correct answer文本和False answer文本不會顯示在日志中,但也作為textView元素顯示。


我有什么?

  1. 整個代碼計數x * y (答案是z
  2. 代碼生成xyz
  3. 比較zanswer的代碼(用戶的答案)

我需要什么?

  • textView元素顯示“ x * y ”(其中xy應為textView內的整數)並在每次xy值更新后更新
  • 某些元素可能顯示CorrectIncorrect ,因為另一個textView元素顯示在Button元素下而不是日志條目下

代碼

MainActivity.java

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.EditText;

import java.util.Random;

public class MainActivity extends AppCompatActivity {


    private EditText textBox;
    private int z , answer;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        textBox=findViewById(R.id.editText);
        setActual();
    }

    private void setActual(){
        Random random = new Random();

        int[] firstNum = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};

        int x = firstNum[random.nextInt(firstNum.length)];/*Here program should put a random number out of firstNum array*/
        int[] secondNum = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
        int y = secondNum[random.nextInt(secondNum.length)];/*Here program should put a random number out of secondNum array*/
        z = x*y;
    }

    public void onCheck(View view){

        this.answer = Integer.parseInt(textBox.getText().toString());

        if(answer == z){
            Log.d("TAG", "Correct."); // right answer
        }
        else{
            Log.d("TAG", "Incorrect."); // wrong answer
        }


    }


}

activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">

    <EditText
        android:id="@+id/editText"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_margin="20dp"
        android:hint="Enter you guess"
        android:inputType="numberDecimal"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintVertical_bias="0.333"
        tools:layout_editor_absoluteX="0dp" />

    <Button
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="Check"
        android:layout_margin="20dp"
        android:onClick="onCheck"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintTop_toTopOf="parent" />


</androidx.constraintlayout.widget.ConstraintLayout>

解決方案:在您的view中再添加兩個TextView並相應地更新它們。

您不必使用數字數組來獲取隨機數。只需使用帶有bound作為參數的隨機數。 (雖然這是可選的,但基於數組的方法也很好)。

活動:

/* package & imports.. */

public class MainActivity extends AppCompatActivity {

    private EditText textBox;
    private int z, answer;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        textBox = findViewById(R.id.editText);
        setActual();
    }

    private void setActual() {
        Random random = new Random();
        int x = random.nextInt(10) + 1; //you get a random number between 0 and 9 then add 1 to get 1 - 10, no need to use array.. 
        int y = random.nextInt(10) + 1;
        z = x * y;
        String hintStr = x + " * " + y;
        ((TextView) findViewById(R.id.hintTextView)).setText(hintStr);
    }

    public void onCheck(View view) {

        this.answer = Integer.parseInt(textBox.getText().toString());
        String res = "Incorrect";
        if (answer == z) {
            res = "Correct";
            Log.d("TAG", "Correct."); // right answer

            setActual(); // let's regenerate values 

        } else {
            Log.d("TAG", "Incorrect."); // wrong answer
        }
        ((TextView) findViewById(R.id.result_text_view)).setText(res);
    }
}

你的觀點:剛剛添加了 2 個文本視圖,第一個是你所說的將x * y都表示為整數,另一個是顯示結果。

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">

    <TextView
        android:id="@+id/hintTextView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="x * y"
        app:layout_constraintBottom_toTopOf="@+id/editText"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

    <EditText
        android:id="@+id/editText"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_margin="20dp"
        android:hint="Enter you guess"
        android:inputType="numberDecimal"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintVertical_bias="0.333"
        tools:layout_editor_absoluteX="0dp" />

    <Button
        android:id="@+id/button"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_margin="20dp"
        android:onClick="onCheck"
        android:text="Check"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        tools:layout_editor_absoluteX="20dp" />

    <TextView
        android:id="@+id/result_text_view"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Result"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/button"
        app:layout_constraintVertical_bias="0.316" />


</androidx.constraintlayout.widget.ConstraintLayout>

onCreate方法中,使用其 ID 獲取對TextView的引用,就像您對EditText所做的一樣。 然后,在您的xyz變量以及任何其他變量的值發生變化后,將文本設置為您的 TextView ,如下所示:

textView.setText("" + x + " * " + y + " = " + z);

answerIsCorrectText.setText("" + (x*y=z)? "Correct:": "Incorrect;(");

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM