调用适当的函数

时间:2016-02-04 23:34:29

标签: java android

我是Android开发的新手,并且很难尝试在我的setOnClickListener上调用相应的函数,我有3个函数,每个都有一组editText,用户可以输入一些double值,这个值然后返回通过该功能,当用户点击计算时显示正确的信息,但是,我试图检查editText或函数中是否有值,以便我可以显示正确的信息,但我可以&# 39;弄清楚。

一次运行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;
   }
}

我刚刚对第一个进行检查以保持简短。

非常感谢任何帮助。

2 个答案:

答案 0 :(得分:0)

向我们提供您的代码,以便我们提供帮助,我需要查看您的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" />

希望它会有所帮助。

答案 1 :(得分:0)

 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个或全部区域并不重要,它永远不会找到“无效的双重”。我必须为所有领域做到这一点。

感谢。