Java 调用适当的函数

Java 调用适当的函数,java,android,Java,Android,我是android开发新手,很难在我的setOnClickListener上调用适当的函数,我有3个函数,每个函数都有一组editText,用户可以输入一些双值,这些值然后由函数返回,当用户单击calculate时,会显示正确的信息,我试图检查editText或函数中是否有值,以便显示正确的信息,但我无法找到它 一次运行1个功能 public class MainWindowActivity extends AppCompatActivity { private Button calculat

我是android开发新手,很难在我的setOnClickListener上调用适当的函数,我有3个函数,每个函数都有一组editText,用户可以输入一些双值,这些值然后由函数返回,当用户单击calculate时,会显示正确的信息,我试图检查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;
   }
}
我只是在第一节课上做了检查,以保持简短


任何帮助都将不胜感激。

请提供您的代码,以便我们能够提供帮助,我需要查看您的计算器a{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" />


希望它会有帮助。

给我们你的代码,这样我们就可以提供帮助,我需要查看你的计算器ea{1,2,3}

 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());
        // Toast.makeText(MainWindowActivity.this, "inside the 'ELSE' statement", Toast.LENGTH_LONG).show();
    }
好的,伙计,我想我找到了解决办法,而且很容易。当您编写这样的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" />

希望这会有帮助

 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());
        // Toast.makeText(MainWindowActivity.this, "inside the 'ELSE' statement", Toast.LENGTH_LONG).show();
    }
上面的代码就是问题所在,如果条件是这样的话,我必须再做一次检查并更改值

  if (squareYards1Text.getText().toString().trim().length() == 0 && length1Text.getText().toString().trim().length() == 0) {
                length1 = 0.0;
                syard1 = 0.0;
                width1 = 0.0;
            }else 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());
            }
现在,不管是只使用一个区域,还是2个、3个或所有区域,都不会发现“无效双精度”。我必须在所有领域都这样做

谢谢

上面的代码就是问题所在,如果条件是这样的话,我必须再做一次检查并更改值

  if (squareYards1Text.getText().toString().trim().length() == 0 && length1Text.getText().toString().trim().length() == 0) {
                length1 = 0.0;
                syard1 = 0.0;
                width1 = 0.0;
            }else 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());
            }
现在,不管是只使用一个区域,还是2个、3个或所有区域,都不会发现“无效双精度”。我必须在所有领域都这样做


谢谢。

什么是Calculate1?对不起,这是一个函数,我刚刚更新了帖子,谢谢。
CalculateArea2()
计算EA3()将永远不会被调用。因为这种情况永远不会发生“if(CalculateArea1()==null)”,因为它不能为null。它返回double。实际上它返回一个double,而不是可以检查null的基本类型。Calculate1到底是什么?对不起,是函数,我刚刚更新了帖子。谢谢。
CalculateArea2()
计算EA3()将永远不会被调用。因为这种情况永远不会发生“if(CalculateArea1()==null)”,因为它不能为null。它返回double。实际上它返回一个double,而不是您可以检查null的基本类型。只需使用我的其余代码编辑帖子。谢谢。我很确定是这样设置的,我会仔细检查,但主要的问题是如果我调用上面的语句,它会工作,但是如果我为calculateArea3添加第二个条件,它会在calculateArea1上找到null,然后崩溃。我需要检查用户是否在区域1、区域2、区域3或所有区域,并相应地显示结果。谢谢。用我的代码编辑这篇文章就行了。谢谢。我很确定是这样设置的,我会仔细检查,但主要的问题是如果我调用上面的语句,它会工作,但是如果我为calculateArea3添加第二个条件,它会在calculateArea1上找到null,然后崩溃。我需要检查用户是否在区域1、区域2、区域3或所有区域,并相应地显示结果。谢谢