在 android 工作室中计算 Java
Calculate in android studio Java
我已经创建了几个用户可以输入数字的 EditText,我想在最后一个名为 math_test2.Can 的 Edittext 中获取并显示输出,你们帮我看看语法?我是 android studio 的新手,我不知道完成这项工作的语法。我想我需要存储 x 和 y 的数字输入以及另一个变量 z,然后设置 z = 输出。然后从我创建的常规 java 文件中获取方法。我知道我应该创建一个实例,但我不确定我是否知道该怎么做 correctly.I 我正在努力更好地使用 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));
}
}
为了计算输入的 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/Whosebug/tree/sof_23_oct_21
结果将如下所示:
我已经创建了几个用户可以输入数字的 EditText,我想在最后一个名为 math_test2.Can 的 Edittext 中获取并显示输出,你们帮我看看语法?我是 android studio 的新手,我不知道完成这项工作的语法。我想我需要存储 x 和 y 的数字输入以及另一个变量 z,然后设置 z = 输出。然后从我创建的常规 java 文件中获取方法。我知道我应该创建一个实例,但我不确定我是否知道该怎么做 correctly.I 我正在努力更好地使用 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));
}
}
为了计算输入的 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/Whosebug/tree/sof_23_oct_21
结果将如下所示: