[英]Calculate in android studio Java
I have created several EditTexts that a user can enter numbers and I would like to get and show the output in the last Edittext called math_test2.Can you guys help me with the syntax?我创建了几个用户可以输入数字的 EditText,我想在最后一个名为 math_test2 的 Edittext 中获取并显示输出。你们能帮我语法吗? Iam new to android studio and I dont know the syntax to make this work.
我是 android studio 的新手,我不知道完成这项工作的语法。 I think I need to store the number input of x and y and another variable z and then set z = output.
我想我需要存储 x 和 y 的数字输入以及另一个变量 z 然后设置 z = 输出。 Then get the method from the regular java file I created.
然后从我创建的常规java文件中获取方法。 I know I am suppose to created an instance, but I am not sure I know how to do that correctly.I am trying to get better at using android studio.
我知道我想创建一个实例,但我不确定我知道如何正确地做到这一点。我正在努力更好地使用 android studio。 I made the MainActivity dependent of the JavaMathExample1 file located in the New module I created.
我使 MainActivity 依赖于位于我创建的新模块中的 JavaMathExample1 文件。 Thank you!
谢谢!
**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));
}
}
In order to calculate x and y which are entering, I have made changes in all three classes which you have posted above.为了计算输入的 x 和 y,我对您在上面发布的所有三个类进行了更改。
xml layout: 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>
MainActivity:主要活动:
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: 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;
}
}
If you still have issue, you can find full code here: https://github.com/parmeshtoyou/StackOverflow/tree/sof_23_oct_21 Happy Coding!!!如果您仍然有问题,您可以在这里找到完整的代码: https : //github.com/parmeshtoyou/StackOverflow/tree/sof_23_oct_21快乐编码!!!
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.