在EditText中显示结果

时间:2014-05-06 02:34:29

标签: java android android-studio

  

MainActivity

package com.elitiv.myapplication2.app;

import android.os.Bundle;
import android.support.v7.app.ActionBarActivity;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.RadioButton;
import android.widget.TextView;

public class MainActivity extends ActionBarActivity {


private EditText Result;
private Double NumberYear;
private Double NumberCapacity;
private String Operation;
private Double NumberResult;
private EditText editYear;
private EditText editCapacity;
private Button calc;
private RadioButton radioD;
private RadioButton radioB;


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

    TextView Year = (TextView)findViewById(R.id.Year);
    TextView Capacity = (TextView)findViewById(R.id.Capacity);
    editYear = (EditText)findViewById(R.id.editYear);
     editCapacity = (EditText)findViewById(R.id.editCapacity);
    final EditText Result = (EditText)findViewById(R.id.Result);
    Button   calc =(Button)findViewById(R.id.calc);
    final RadioButton radioD = (RadioButton)findViewById(R.id.radioD);
    final RadioButton radioB = (RadioButton)findViewById(R.id.radioB);



}



public void Operation(){

    Double result;
    //Benzin
    if(NumberCapacity >0 &&NumberCapacity <1000) {
        if (Operation.equals(2004)) {
            result = NumberCapacity * 0.52;
            Result.setText(String.valueOf(result));
        }
        if (Operation.equals("2005")) {
            result = NumberCapacity * 0.50;
            Result.setText(String.valueOf(result));
        }
        if (Operation.equals("2006")) {
            result = NumberCapacity * 0.47;
            Result.setText(String.valueOf(result));
        }
        if (Operation.equals("2007")) {
            result = NumberCapacity * 0.44;
        }
        if (Operation.equals("2009")) {
            result = NumberCapacity * 0.43;

        }
        if (Operation.equals("2011")) {
            result = NumberCapacity * 0.42;

        }

        if (Operation.equals("2013")) {
            result = NumberCapacity * 0.40;

        }
       }
    if(NumberCapacity >1001 &&NumberCapacity <1500){
        if (Operation.equals("2004")) {
            result = NumberCapacity * 0.67;

        }
        if (Operation.equals("2005")) {
            result = NumberCapacity * 0.63;
        }
        if (Operation.equals("2006")) {
            result = NumberCapacity * 0.60;
        }
        if (Operation.equals("2007")) {
            result = NumberCapacity * 0.56;
        }
        if (Operation.equals("2009")) {
            result = NumberCapacity * 0.55;
        }
        if (Operation.equals("2011")) {
            result = NumberCapacity * 0.54;
        }
        if (Operation.equals("2013")) {
            result = NumberCapacity * 0.52;
        }
    }
    if(NumberCapacity >1501 &&NumberCapacity <2000){
        if (Operation.equals("2004")) {
            result = NumberCapacity * 1.00;
        }
        if (Operation.equals("2005")) {
            result = NumberCapacity * 0.95;
        }
        if (Operation.equals("2006")) {
            result = NumberCapacity * 0.89;
        }
        if (Operation.equals("2007")) {
            result = NumberCapacity * 0.84;
        }
        if (Operation.equals("2009")) {
            result = NumberCapacity * 0.83;
        }
        if (Operation.equals("2011")) {
            result = NumberCapacity * 0.82;
        }
        if (Operation.equals("2013")) {
            result = NumberCapacity * 0.80;
        }
    }
    if(NumberCapacity >2001 &&NumberCapacity <3000){
        if (Operation.equals("2004")) {
            result = NumberCapacity * 1.59;
        }
        if (Operation.equals("2005")) {
            result = NumberCapacity * 1.52;
        }
        if (Operation.equals("2006")) {
            result = NumberCapacity * 1.44;
        }
        if (Operation.equals("2007")) {
            result = NumberCapacity * 1.36;
        }
        if (Operation.equals("2009")) {
            result = NumberCapacity * 1.35;
        }
        if (Operation.equals("2011")) {
            result = NumberCapacity * 1.34;
        }
        if (Operation.equals("2013")) {
            result = NumberCapacity * 1.32;
        }
    }
    if(NumberCapacity >3000 ){
        if (Operation.equals("2004")) {
            result = NumberCapacity * 3.57;
        }
        if (Operation.equals("2005")) {
            result = NumberCapacity * 3.56;
        }
        if (Operation.equals("2006")) {
            result = NumberCapacity * 3.55;
        }
        if (Operation.equals("2007")) {
            result = NumberCapacity * 3.54;
        }
        if (Operation.equals("2009")) {
            result = NumberCapacity * 3.53;
        }
        if (Operation.equals("2011")) {
            result = NumberCapacity * 3.52;
        }
        if (Operation.equals("2013")) {
            result = NumberCapacity * 3.50;
        }
    }

    //Diesel
    if(NumberCapacity <1500){
        if (Operation.equals("20041")) {
            result = NumberCapacity * 1.67;
        }
        if (Operation.equals("20051")) {
            result = NumberCapacity * 1.63;
        }
        if (Operation.equals("20061")) {
            result = NumberCapacity * 1.60;
        }
        if (Operation.equals("20071")) {
            result = NumberCapacity * 1.56;
        }
        if (Operation.equals("20091")) {
            result = NumberCapacity * 1.55;
        }
        if (Operation.equals("20111")) {
            result = NumberCapacity * 1.54;
        }
        if (Operation.equals("20131")) {
            result = NumberCapacity * 1.52;
        }
    }
    if(NumberCapacity >1501 &&NumberCapacity <2500){
        if (Operation.equals("20041")) {
            result = NumberCapacity * 1.59;
        }
        if (Operation.equals("20051")) {
            result = NumberCapacity * 1.52;
        }
        if (Operation.equals("20061")) {
            result = NumberCapacity * 1.44;
        }
        if (Operation.equals("20071")) {
            result = NumberCapacity * 1.36;
        }
        if (Operation.equals("20091")) {
            result = NumberCapacity * 1.35;
        }
        if (Operation.equals("20111")) {
            result = NumberCapacity * 1.34;
        }
        if (Operation.equals("20131")) {
            result = NumberCapacity * 1.32;
        }
    }
    if(NumberCapacity >2501 ){
        if (Operation.equals("20041")) {
            result = NumberCapacity * 3.57;
        }
        if (Operation.equals("20051")) {
            result = NumberCapacity * 3.56;
        }
        if (Operation.equals("20061")) {
            result = NumberCapacity * 3.55;
        }
        if (Operation.equals("20071")) {
            result = NumberCapacity * 3.54;
        }
        if (Operation.equals("20091")) {
            result = NumberCapacity * 3.53;
        }
        if (Operation.equals("20111")) {
            result = NumberCapacity * 3.52;
        }
        if (Operation.equals("20131")) {
            result = NumberCapacity * 3.50;

        }

    }
calc.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {
        if(radioB.isChecked()){
            switch (R.id.editYear){

                case 2004:
                    Operation=("2004");
                    break;
                case 2005:
                    Operation="2005";
                    break;
                case 2006:
                    Operation="2006";
                    break;
                case 2007:
                    Operation="2007";
                    break;
                case 2008:
                    Operation="2007";
                    break;
                case 2009:
                    Operation="2009";
                    break;
                case 2010:
                    Operation="2010";
                    break;
                case 2011:
                    Operation="2011";
                    break;
                case 2012:
                    Operation="2011";
                    break;
                case 2013:
                    Operation="2013";
                    break;
                case 2014:
                    Operation="2013";
                    break;
            }

        }
        else
            if(radioD.isChecked()){
                switch(R.id.editYear){

                    case 2004:
                        Operation="20041";
                        break;
                    case 2005:
                        Operation="20051";
                        break;
                    case 2006:
                        Operation="20061";
                        break;
                    case 2007:
                        Operation="20071";
                        break;
                    case 2008:
                        Operation="20071";
                        break;
                    case 2009:
                        Operation="20091";
                        break;
                    case 2010:
                        Operation="20101";
                        break;
                    case 2011:
                        Operation="20111";
                        break;
                    case 2012:
                        Operation="20111";
                        break;
                    case 2013:
                        Operation="20131";
                        break;
                    case 2014:
                        Operation="20131";
                        break;
                }

    }
}
}


);}
}
  

activity_main.xml中

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:paddingBottom="@dimen/activity_vertical_margin"
tools:context="com.elitiv.myapplication2.app.MainActivity">


<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:textAppearance="?android:attr/textAppearanceLarge"
    android:text="Anul Autovehicolului"
    android:id="@+id/Year"
    android:layout_marginTop="25dp"
    android:layout_alignParentTop="true"
    android:layout_alignParentLeft="true"
    android:layout_alignParentStart="true" />

    <EditText
    android:layout_width="200dp"
    android:layout_height="wrap_content"
    android:id="@+id/editYear"
    android:layout_alignTop="@+id/Year"
    android:layout_toRightOf="@+id/Year"
    android:layout_marginLeft="27dp" />

    <TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:textAppearance="?android:attr/textAppearanceLarge"
    android:text="capacitatea"
    android:id="@+id/Capacity"
    android:layout_alignParentBottom="true"
    android:layout_toLeftOf="@+id/editYear"
    android:layout_marginBottom="175dp" />

    <EditText
    android:layout_width="200dp"
    android:layout_height="wrap_content"
    android:id="@+id/editCapacity"
    android:layout_alignBottom="@+id/Capacity"
    android:layout_alignLeft="@+id/editYear"
    android:layout_alignStart="@+id/editYear" />
    <RadioGroup
    android:layout_width="300dp"
    android:layout_height="wrap_content"
    android:gravity="center_horizontal"
    android:layout_alignTop="@id/Year">
    <RadioButton
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="B"
    android:id="@+id/radioB"
    android:layout_marginTop="83dp"
    android:checked="true"
    android:layout_marginRight="77dp" />

    <RadioButton
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="D"
    android:id="@+id/radioD"
    android:checked="false"

     />

    </RadioGroup>
    <Button
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Calc"
    android:id="@+id/calc"
    android:layout_below="@+id/editCapacity"
    android:layout_centerHorizontal="true"
    android:layout_marginTop="25dp" />

    <EditText
    android:layout_width="200dp"
    android:layout_height="wrap_content"
    android:id="@+id/Result"
    android:layout_below="@+id/calc"
    android:layout_centerHorizontal="true"
    android:layout_marginTop="31dp" />

   </RelativeLayout>

大家好,我的应用程序必须计算2个文本字段,第一个文本转换为文本ex:2004指定的值,主要问题是显示文本字段3中的结果,我想询问代码这是正确的吗?

2 个答案:

答案 0 :(得分:0)

首先,我建议您浏览JAVA的指导指南,并检查命名约定 这将有助于您正确维护代码。

现在回答你的问题

您的setOnClickListener只是将值Operation分配给您定义的String

您还需要调用该函数来为您的字段设置计算值。

答案 1 :(得分:0)

开关(R.id.editYear) - &gt;这是错的,R.id.edityear是一个很长的,永远不会改变,(检查你R.java和搜索edityear var,也许她的价值从来没有在开关案例上被贬低)我认为你在寻找这些价值,然后你应该在onClickListener上执行此操作:

public void onClick(View v) {
    if(radioB.isChecked()){

        int edityear_value=Integer.parseInt(editYear.getText().toString());//Convert to int
            switch(edityear_value){//All your cases..

另外,在operation()中(也检查从未调用过operation(),如果函数永远不被调用,你如何得到结果?),并非所有操作都将结果设置为Result,那么,你可以分配在函数结束之前计算的结果只有一次:

Result.setText(String.valueOf(result));

而且,onclick监听器应该在onCreate上设置,而不是在她的同一个函数中...最后,结果将是这样的:

package com.elitiv.myapplication2.app;

import android.os.Bundle;
import android.support.v7.app.ActionBarActivity;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.RadioButton;
import android.widget.TextView;

public class MainActivity extends ActionBarActivity {


private EditText Result;
private Double NumberYear;
private Double NumberCapacity;
private String Operation;
private Double NumberResult;
private EditText editYear;
private EditText editCapacity;
private Button calc;
private RadioButton radioD;
private RadioButton radioB;


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

    TextView Year = (TextView)findViewById(R.id.Year);
    TextView Capacity = (TextView)findViewById(R.id.Capacity);
    editYear = (EditText)findViewById(R.id.editYear);
     editCapacity = (EditText)findViewById(R.id.editCapacity);
    final EditText Result = (EditText)findViewById(R.id.Result);
    Button   calc =(Button)findViewById(R.id.calc);
    final RadioButton radioD = (RadioButton)findViewById(R.id.radioD);
    final RadioButton radioB = (RadioButton)findViewById(R.id.radioB);
calc.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
int edityear_value=Integer.parseInt(editYear.getText().toString());//Convert to intger
    if(radioB.isChecked()){
        switch (edityear_value){

            case 2004:
                Operation=("2004");
                break;
            case 2005:
                Operation="2005";
                break;
            case 2006:
                Operation="2006";
                break;
            case 2007:
                Operation="2007";
                break;
            case 2008:
                Operation="2007";
                break;
            case 2009:
                Operation="2009";
                break;
            case 2010:
                Operation="2010";
                break;
            case 2011:
                Operation="2011";
                break;
            case 2012:
                Operation="2011";
                break;
            case 2013:
                Operation="2013";
                break;
            case 2014:
                Operation="2013";
                break;
        }

    }
    else
        if(radioD.isChecked()){
            switch(edityear_value){

                case 2004:
                    Operation="20041";
                    break;
                case 2005:
                    Operation="20051";
                    break;
                case 2006:
                    Operation="20061";
                    break;
                case 2007:
                    Operation="20071";
                    break;
                case 2008:
                    Operation="20071";
                    break;
                case 2009:
                    Operation="20091";
                    break;
                case 2010:
                    Operation="20101";
                    break;
                case 2011:
                    Operation="20111";
                    break;
                case 2012:
                    Operation="20111";
                    break;
                case 2013:
                    Operation="20131";
                    break;
                case 2014:
                    Operation="20131";
                    break;
            }

}
Operation();//when click tht button, set the values and call function to calculate over these values
}
}


);


}



public void Operation(){

Double result;
//Benzin
if(NumberCapacity >0 &&NumberCapacity <1000) {
    if (Operation.equals(2004)) {
        result = NumberCapacity * 0.52;

    }
    if (Operation.equals("2005")) {
        result = NumberCapacity * 0.50;

    }
    if (Operation.equals("2006")) {
        result = NumberCapacity * 0.47;

    }
    if (Operation.equals("2007")) {
        result = NumberCapacity * 0.44;
    }
    if (Operation.equals("2009")) {
        result = NumberCapacity * 0.43;

    }
    if (Operation.equals("2011")) {
        result = NumberCapacity * 0.42;

    }

    if (Operation.equals("2013")) {
        result = NumberCapacity * 0.40;

    }
   }
if(NumberCapacity >1001 &&NumberCapacity <1500){
    if (Operation.equals("2004")) {
        result = NumberCapacity * 0.67;

    }
    if (Operation.equals("2005")) {
        result = NumberCapacity * 0.63;
    }
    if (Operation.equals("2006")) {
        result = NumberCapacity * 0.60;
    }
    if (Operation.equals("2007")) {
        result = NumberCapacity * 0.56;
    }
    if (Operation.equals("2009")) {
        result = NumberCapacity * 0.55;
    }
    if (Operation.equals("2011")) {
        result = NumberCapacity * 0.54;
    }
    if (Operation.equals("2013")) {
        result = NumberCapacity * 0.52;
    }
}
if(NumberCapacity >1501 &&NumberCapacity <2000){
    if (Operation.equals("2004")) {
        result = NumberCapacity * 1.00;
    }
    if (Operation.equals("2005")) {
        result = NumberCapacity * 0.95;
    }
    if (Operation.equals("2006")) {
        result = NumberCapacity * 0.89;
    }
    if (Operation.equals("2007")) {
        result = NumberCapacity * 0.84;
    }
    if (Operation.equals("2009")) {
        result = NumberCapacity * 0.83;
    }
    if (Operation.equals("2011")) {
        result = NumberCapacity * 0.82;
    }
    if (Operation.equals("2013")) {
        result = NumberCapacity * 0.80;
    }
}
if(NumberCapacity >2001 &&NumberCapacity <3000){
    if (Operation.equals("2004")) {
        result = NumberCapacity * 1.59;
    }
    if (Operation.equals("2005")) {
        result = NumberCapacity * 1.52;
    }
    if (Operation.equals("2006")) {
        result = NumberCapacity * 1.44;
    }
    if (Operation.equals("2007")) {
        result = NumberCapacity * 1.36;
    }
    if (Operation.equals("2009")) {
        result = NumberCapacity * 1.35;
    }
    if (Operation.equals("2011")) {
        result = NumberCapacity * 1.34;
    }
    if (Operation.equals("2013")) {
        result = NumberCapacity * 1.32;
    }
}
if(NumberCapacity >3000 ){
    if (Operation.equals("2004")) {
        result = NumberCapacity * 3.57;
    }
    if (Operation.equals("2005")) {
        result = NumberCapacity * 3.56;
    }
    if (Operation.equals("2006")) {
        result = NumberCapacity * 3.55;
    }
    if (Operation.equals("2007")) {
        result = NumberCapacity * 3.54;
    }
    if (Operation.equals("2009")) {
        result = NumberCapacity * 3.53;
    }
    if (Operation.equals("2011")) {
        result = NumberCapacity * 3.52;
    }
    if (Operation.equals("2013")) {
        result = NumberCapacity * 3.50;
    }
}

//Diesel
if(NumberCapacity <1500){
    if (Operation.equals("20041")) {
        result = NumberCapacity * 1.67;
    }
    if (Operation.equals("20051")) {
        result = NumberCapacity * 1.63;
    }
    if (Operation.equals("20061")) {
        result = NumberCapacity * 1.60;
    }
    if (Operation.equals("20071")) {
        result = NumberCapacity * 1.56;
    }
    if (Operation.equals("20091")) {
        result = NumberCapacity * 1.55;
    }
    if (Operation.equals("20111")) {
        result = NumberCapacity * 1.54;
    }
    if (Operation.equals("20131")) {
        result = NumberCapacity * 1.52;
    }
}
if(NumberCapacity >1501 &&NumberCapacity <2500){
    if (Operation.equals("20041")) {
        result = NumberCapacity * 1.59;
    }
    if (Operation.equals("20051")) {
        result = NumberCapacity * 1.52;
    }
    if (Operation.equals("20061")) {
        result = NumberCapacity * 1.44;
    }
    if (Operation.equals("20071")) {
        result = NumberCapacity * 1.36;
    }
    if (Operation.equals("20091")) {
        result = NumberCapacity * 1.35;
    }
    if (Operation.equals("20111")) {
        result = NumberCapacity * 1.34;
    }
    if (Operation.equals("20131")) {
        result = NumberCapacity * 1.32;
    }
}
if(NumberCapacity >2501 ){
    if (Operation.equals("20041")) {
        result = NumberCapacity * 3.57;
    }
    if (Operation.equals("20051")) {
        result = NumberCapacity * 3.56;
    }
    if (Operation.equals("20061")) {
        result = NumberCapacity * 3.55;
    }
    if (Operation.equals("20071")) {
        result = NumberCapacity * 3.54;
    }
    if (Operation.equals("20091")) {
        result = NumberCapacity * 3.53;
    }
    if (Operation.equals("20111")) {
        result = NumberCapacity * 3.52;
    }
    if (Operation.equals("20131")) {
        result = NumberCapacity * 3.50;

    }

}
        Result.setText(String.valueOf(result));//Set the value after calculate it
}
}
祝你好运!