【问题标题】:Calculate in android studio Java在 android studio Java 中计算
【发布时间】:2021-10-23 04:50:01
【问题描述】:

我创建了几个用户可以输入数字的 EditText,我想在最后一个名为 math_test2 的 Edittext 中获取并显示输出。你们能帮我语法吗?我是 android studio 的新手,我不知道实现这项工作的语法。我想我需要存储 x 和 y 的数字输入以及另一个变量 z,然后设置 z = output。然后从我创建的常规 java 文件中获取方法。我知道我想创建一个实例,但我不确定我知道如何正确地做到这一点。我正在努力更好地使用 android studio。我使 MainActivity 依赖于位于我创建的新模块中的 JavaMathExample1 文件。谢谢!

**activity_main.xml file:**

<?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:layout_width="166dp"
        android:layout_height="70dp"
        android:gravity="center"
        android:text="Math Test Demo"
        android:textAlignment="center"
        android:textSize="20sp"
        android:textStyle="bold"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintHorizontal_bias="0.044"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintVertical_bias="0.167" />


    <EditText
        android:id="@+id/calculateX"
        android:layout_width="251dp"
        android:layout_height="62dp"
        android:hint="Enter x value:"
        android:padding="15dp"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.071"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintVertical_bias="0.316" />


    <EditText
        android:id="@+id/calculateY"
        android:layout_width="265dp"
        android:layout_height="58dp"
        android:hint="Enter y value:"
        android:padding="15dp"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.078"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintVertical_bias="0.456" />


    <EditText
        android:id="@+id/math_test2"
        android:layout_width="324dp"
        android:layout_height="68dp"
        android:hint="Calculated Output for x and y: "
        android:padding="15dp"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.126"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintVertical_bias="0.598" />

</androidx.constraintlayout.widget.ConstraintLayout>


**MainActivity.java file:**

package com.example.testdemo;



import com.example.javaprogram.JavaMathExample1;

public class MainActivity extends AppCompatActivity {

    EditText calculateX,calculateY,output;
    JavaMathExample1 javaMathExample1;

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

        calculateX = (EditText) findViewById(R.id.calculateX);
        calculateY = (EditText) findViewById(R.id.calculateY);
        output = (EditText) findViewById(R.id.math_test2);


        String x = calculateX.getText().toString();// Enter a number for x
        String y  = calculateY.getText().toString();// Enter a number for y
        String result = output.getText().toString();// Get output from method created in JavaMathExample1

        calculateY.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {

            }
        });

        calculateY.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {

            }
        });

        output.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
            
            JavaMathExample1 myObj = new JavaMathExample1();
            myObj.fullThrottle(x,y);

            }
        }); 
    }
}



**Regular Java file from "New Module android library created"**

package com.example.javaprogram

public class JavaMathExample1 {

    public void fullThrottle(double x, double y) {

        // return the maximum of two numbers
        System.out.println("Maximum number of x and y is: " + Math.max(x, y));
    }
}

【问题讨论】:

    标签: java android computer-science


    【解决方案1】:

    为了计算输入的 x 和 y,我对您在上面发布的所有三个类进行了更改。

    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">
    
    <TextView
      android:layout_width="166dp"
      android:layout_height="70dp"
      android:gravity="center"
      android:text="Math Test Demo"
      android:textAlignment="center"
      android:textSize="20sp"
      android:textStyle="bold"
      app:layout_constraintBottom_toBottomOf="parent"
      app:layout_constraintHorizontal_bias="0.044"
      app:layout_constraintLeft_toLeftOf="parent"
      app:layout_constraintRight_toRightOf="parent"
      app:layout_constraintTop_toTopOf="parent"
      app:layout_constraintVertical_bias="0.167" />
    
    <EditText
      android:id="@+id/calculateX"
      android:layout_width="251dp"
      android:layout_height="62dp"
      android:hint="Enter x value:"
      android:padding="15dp"
      app:layout_constraintBottom_toBottomOf="parent"
      app:layout_constraintEnd_toEndOf="parent"
      app:layout_constraintHorizontal_bias="0.071"
      app:layout_constraintStart_toStartOf="parent"
      app:layout_constraintTop_toTopOf="parent"
      app:layout_constraintVertical_bias="0.316" />
    
    
    <EditText
      android:id="@+id/calculateY"
      android:layout_width="265dp"
      android:layout_height="58dp"
      android:hint="Enter y value:"
      android:padding="15dp"
      app:layout_constraintBottom_toBottomOf="parent"
      app:layout_constraintEnd_toEndOf="parent"
      app:layout_constraintHorizontal_bias="0.078"
      app:layout_constraintStart_toStartOf="parent"
      app:layout_constraintTop_toTopOf="parent"
      app:layout_constraintVertical_bias="0.456" />
    
    <EditText
      android:id="@+id/math_test2"
      android:layout_width="324dp"
      android:layout_height="68dp"
      android:hint="Calculated Output for x and y: "
      android:padding="15dp"
      app:layout_constraintBottom_toBottomOf="parent"
      app:layout_constraintEnd_toEndOf="parent"
      app:layout_constraintHorizontal_bias="0.126"
      app:layout_constraintStart_toStartOf="parent"
      app:layout_constraintTop_toTopOf="parent"
      app:layout_constraintVertical_bias="0.598" />
    
    <Button
      android:id="@+id/calculate"
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:text="Calculate"
      app:layout_constraintTop_toBottomOf="@id/math_test2"
      app:layout_constraintStart_toStartOf="parent"
      app:layout_constraintEnd_toEndOf="parent"/>
    
     </androidx.constraintlayout.widget.ConstraintLayout>
    

    主活动:

    public class MainActivity extends AppCompatActivity {
    
    EditText calculateX, calculateY, output;
    
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    
        calculateX = findViewById(R.id.calculateX);
        calculateY = findViewById(R.id.calculateY);
        output = findViewById(R.id.math_test2);
    
        findViewById(R.id.calculate).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                JavaMathExample1 myObj = new JavaMathExample1();
                String inputX = calculateX.getText().toString();
                String inputY = calculateY.getText().toString();
    
                if (inputX.isEmpty() || inputY.isEmpty()) {
                    Toast.makeText(MainActivity.this, "Enter X or Y value", Toast.LENGTH_LONG).show();
                } else {
                    double x = Double.parseDouble(inputX);  // Enter a number for x
                    double y = Double.parseDouble(inputY); // Enter a number for y
                    double max = myObj.fullThrottle(x, y); //This will give you result
                    output.setText(String.format("%s", max));
                }
            }
        });
    }
    }
    

    JavaMathExample1:

    public class JavaMathExample1 {
    public double fullThrottle(double x, double y) {
        double max = Math.max(x, y);
        // return the maximum of two numbers
        System.out.println("Maximum number of x and y is: " + max);
        return max;
    }
    }
    

    如果仍有问题,您可以在此处找到完整代码: https://github.com/parmeshtoyou/StackOverflow/tree/sof_23_oct_21

    结果将如下所示:

    【讨论】:

    • 我尚未对此进行测试,但我只想非常感谢您的回复和帮助。我正在尝试学习 android studio,并且在某些方面遇到了困难,所以我来这里寻求帮助和指导。我非常感谢您抽出宝贵时间来帮助我理解。感激不尽。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-11-28
    • 2020-06-23
    • 2021-10-13
    • 1970-01-01
    • 2012-03-28
    • 2021-10-25
    相关资源
    最近更新 更多