java - 调用适当的函数

标签 java android

我是 Android 开发新手,很难尝试在 setOnClickListener 上调用适当的函数,我有 3 个函数,每个函数都有一组 editText,用户可以输入一些 double 值,然后函数返回该值,当用户单击计算时显示正确的信息,但是,我试图检查 editText 或函数中是否有值,以便我可以显示正确的信息,但我无法弄清楚。

一次可以运行 1 个函数

public class MainWindowActivity extends AppCompatActivity {

private Button calculateButton; 
private EditText length1Text;
private EditText length2Text;
private EditText length3Text;
private EditText width1Text;
private EditText width2Text;
private EditText width3Text;
private EditText thick1Text;
private EditText thick2Text;
private EditText thick3Text;
private EditText squareYards1Text;
private EditText squareYards2Text;
private EditText squareYards3Text;
private EditText result1TotalText;
private EditText result2TotalText;
private EditText result3TotalText;
private EditText grandTotalText;

private Double length1 = 0.0;
private Double length2 = 0.0;
private Double length3 = 0.0;
private Double width1 = 0.0;
private Double width2 = 0.0;
private Double width3 = 0.0;
private Double thick1 = 0.0;
private Double thick2 = 0.0;
private Double thick3 = 0.0;
private Double syard1 = 0.0;
private Double syard2 = 0.0;
private Double syard3 = 0.0;


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

    calculateButton = (Button) findViewById(R.id.calculateButtonPressed);
    calculateButton.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {


            if(CalculateArea1() == null){
                CalculateArea2();
                CalculateArea3();
                Total = CalculateArea2() + CalculateArea3();

            }

        grandTotalText.setText(String.format("%.1f",Total));
        }
    });

   public Double CalculateArea1(){

    length1Text = (EditText)  findViewById(R.id.length1Text);
    width1Text = (EditText)  findViewById(R.id.width1Text);
    thick1Text = (EditText) findViewById(R.id.thickness1Text);
    squareYards1Text = (EditText) findViewById(R.id.squareYards1Text);
    result1TotalText = (EditText) findViewById(R.id.result1TotalText);
    grandTotalText = (EditText) findViewById(R.id.grandTotalText);

    if (squareYards1Text.getText().toString().trim().length() == 0) {

        length1 = Double.valueOf(length1Text.getText().toString());
        width1 = Double.valueOf(width1Text.getText().toString());
        thick1 = Double.valueOf(thick1Text.getText().toString());
    }else{
        syard1 = Double.valueOf(squareYards1Text.getText().toString());
        thick1 = Double.valueOf(thick1Text.getText().toString());
    }

    //area 1

    double yard = length1 * width1 / 9;
    double t = yard / thick1;

    double y1 = 12.20 / thick1;
    double sy1 = syard1 / y1;

    if (thick1 == 1) {
        t = yard / 12.20;
    }else if(thick1 == 1.25){
        t = yard / 9.76;
    }else if (thick1 == 1.5){
        t = yard / 8.13;
    }else if (thick1 == 1.75){
        t = yard / 6.97;
    }else if (thick1 == 2){
        t = yard / 6.1;
    }

  Double result;
    if (length1Text.getText().toString().trim().length() == 0) {
        result1TotalText.setText(String.format("%.1f", sy1));
        grandTotalText.setText(String.format("%.1f", sy1));
        result = sy1;
    } else {
        result1TotalText.setText(String.format("%.1f", t));
        squareYards1Text.setText(String.format("%.1f", yard));
        grandTotalText.setText(String.format("%.1f", t));
        result = t;

    }
    return result;
}


  public Double CalculateArea2(){

    length2Text = (EditText)  findViewById(R.id.length2Text);
    width2Text = (EditText)  findViewById(R.id.width2Text);
    thick2Text = (EditText) findViewById(R.id.thickness2Text);
    squareYards2Text = (EditText) findViewById(R.id.squareYards2Text);
    result2TotalText = (EditText) findViewById(R.id.result2TotalText);
    grandTotalText = (EditText) findViewById(R.id.grandTotalText);

    if (squareYards2Text.getText().toString().trim().length() == 0) {

        length2 = Double.valueOf(length2Text.getText().toString());
        width2 = Double.valueOf(width2Text.getText().toString());
        thick2 = Double.valueOf(thick2Text.getText().toString());
    }else{
        syard2 = Double.valueOf(squareYards2Text.getText().toString());
        thick2 = Double.valueOf(thick2Text.getText().toString());
    }

    //area 2

    double yard2 = length2 * width2 / 9;
    double t2 = yard2 / thick2;

    double y2 = 12.20 / thick2;
    double sy2 = syard2 / y2;

    if (thick2 == 1) {
        t2 = yard2 / 12.20;
    }else if(thick2 == 1.25){
        t2 = yard2 / 9.76;
    }else if (thick2 == 1.5){
        t2 = yard2 / 8.13;
    }else if (thick2 == 1.75){
        t2 = yard2 / 6.97;
    }else if (thick2 == 2){
        t2 = yard2 / 6.1;
    }

  Double result;
    if (length2Text.getText().toString().trim().length() == 0) {
        result2TotalText.setText(String.format("%.1f", sy2));
        grandTotalText.setText(String.format("%.1f", sy2));
        result = sy2;
    } else {
        result2TotalText.setText(String.format("%.1f", t2));
        squareYards2Text.setText(String.format("%.1f", yard2));
        grandTotalText.setText(String.format("%.1f", t2));
        result = t2;

    }
    return result;
}


 public Double CalculateArea3(){

    length3Text = (EditText)  findViewById(R.id.length3Text);
    width3Text = (EditText)  findViewById(R.id.width3Text);
    thick3Text = (EditText) findViewById(R.id.thickness3Text);
    squareYards3Text = (EditText) findViewById(R.id.squareYards3Text);
    result3TotalText = (EditText) findViewById(R.id.result3TotalText);
    grandTotalText = (EditText) findViewById(R.id.grandTotalText);

    if (squareYards3Text.getText().toString().trim().length() == 0) {

        length3 = Double.valueOf(length3Text.getText().toString());
        width3 = Double.valueOf(width3Text.getText().toString());
        thick3 = Double.valueOf(thick3Text.getText().toString());
    }else{
        syard3 = Double.valueOf(squareYards3Text.getText().toString());
        thick3 = Double.valueOf(thick3Text.getText().toString());
    }

    //area 3

    double yard3 = length3 * width3 / 9;
    double t3 = yard3 / thick3;

    double y3 = 12.20 / thick3;
    double sy3 = syard3 / y3;

    if (thick3 == 1) {
        t3 = yard3 / 12.20;
    }else if(thick3 == 1.25){
        t3 = yard3 / 9.76;
    }else if (thick3 == 1.5){
        t3 = yard3 / 8.13;
    }else if (thick3 == 1.75){
        t3 = yard3 / 6.97;
    }else if (thick3 == 2){
        t3 = yard3 / 6.1;
    }

  Double result;
    if (length3Text.getText().toString().trim().length() == 0) {
        result3TotalText.setText(String.format("%.1f", sy3));
        grandTotalText.setText(String.format("%.1f", sy3));
        result = sy3;
    } else {
        result3TotalText.setText(String.format("%.1f", t3));
        squareYards3Text.setText(String.format("%.1f", yard3));
        grandTotalText.setText(String.format("%.1f", t3));
        result = t3;

    }
    return result;
   }
}

为了保持简短,我只是对第一个进行了检查。

任何帮助将不胜感激。

最佳答案

请向我们提供您的代码,以便我们提供帮助,我需要查看您的CalculateArea{1,2,3}。

好的,伙计,我想我找到了解决方案,而且非常简单。当您编写这样的 OnClick() 函数时,您还需要在 XML 文件上分配 OnClick 函数,以便按钮知道它有一个“函数”来执行某些操作。

这是一个例子;

<Button android:id="@+id/mybutton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Click me!"
android:onClick="OnClick" />

希望对您有所帮助。

关于java - 调用适当的函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35213751/

相关文章:

java - 只在 JAR 中保留某些类的 Ant 任务 - 具有依赖性检查?

java - 如何以编程方式获取 Apache 项目上托管的项目的详细信息(我们有 API)吗?

android - (Android Studio)快速完成外部类的建议变量名?

android - 尝试膨胀自定义 View 时出现 stackoverflow 错误

Android gridview 适配器为零位置调用多个 getview()

java - JDBC 连接池监控 GlassFish

java - 如何在多宿主机器上用 Java 发送和接收 UDP 数据包?

java - 新的 libgdx 安装程序构建一直失败

安卓错误 : Failure [INSTALL_CANCELED_BY_USER]

android - 如何使用 Mapsforge 在运行时正确添加可绘制对象