功能在android应用程序中没有显示正确的解决方案,有人能告诉我我做错了什么吗?

时间:2013-12-04 12:21:20

标签: android android-edittext

enter image description here

程序应该从用户那里获取长度,宽度和深度测量结果,然后显示水箱可容纳多少加仑的水。然而,当我做“12,12,14”这样的测量时,它给了我一些可笑的大数而不是大约15加仑的东西。这是主要活动:

package com.example.aquariumconverter;

import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;

public class MainActivity extends Activity {

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

    final EditText gallonText = (EditText) findViewById(R.id.editText1);

    String strlen = String.valueOf(R.id.editText2);
    String strwid = String.valueOf(R.id.editText3);
    String strdep = String.valueOf(R.id.editText4);
    double length = Double.parseDouble(strlen);
    double width = Double.parseDouble(strwid);
    double depth = Double.parseDouble(strdep);

    RectangularTank recTank = new RectangularTank(length,width,depth);
    final String strConvertedToGallons = recTank.convertToGallons(length,width,depth);

    Button button = (Button) findViewById(R.id.button1);
    button.setOnClickListener(new View.OnClickListener() {
        public void onClick(View v) {
            gallonText.setText(strConvertedToGallons);
        }
    });
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.main, menu);
    return true;
}

}

这是rectangulartank类:

package com.example.aquariumconverter;

import android.view.View;
import android.widget.Button;


public class RectangularTank{

public RectangularTank(double length, double width, double depth) {
    super();
    init(length, width, depth);
}

 private void init(double length, double width, double depth)
 {
     convertToGallons(length, width, depth);

 }

public String convertToGallons(double length, double width, double depth)
{
    double feetLength = length / 12.0;
    double feetWidth = width / 12.0;
    double feetDepth = depth / 12.0;
    double gallonAmount = feetLength * feetWidth * feetDepth * 7.47;
    String strGallonAmount = String.valueOf(gallonAmount);
    return strGallonAmount;
}



}

以下是一些建议后的主要活动的更新版本:

package com.example.aquariumconverter;

import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;

public class MainActivity extends Activity {

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

    final EditText gallonText = (EditText) findViewById(R.id.editText1);
    EditText editText2Text = (EditText) findViewById(R.id.editText2);    
    EditText editText3Text = (EditText) findViewById(R.id.editText3);    
    EditText editText4Text = (EditText) findViewById(R.id.editText4);

    double length, width, depth;
    try {
        length = Double.valueOf(editText2Text.getText().toString());
        width = Double.valueOf(editText3Text.getText().toString());
        depth = Double.valueOf(editText4Text.getText().toString());
    }  catch (NumberFormatException e){
        length=0;
        width=0;
        depth=0;
    }

    RectangularTank recTank = new RectangularTank(length,width,depth);
    final String strConvertedToGallons = recTank.convertToGallons(length,width,depth);

    Button button = (Button) findViewById(R.id.button1);
    button.setOnClickListener(new View.OnClickListener() {
        public void onClick(View v) {
            gallonText.setText(strConvertedToGallons);
        }
    });
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.main, menu);
    return true;
}

}

这是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:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".MainActivity" >

<TextView
    android:id="@+id/textView3"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignLeft="@+id/textView2"
    android:layout_below="@+id/textView2"
    android:layout_marginTop="20dp"
    android:text="Width"
    android:textAppearance="?android:attr/textAppearanceSmall" />

<TextView
    android:id="@+id/textView4"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignLeft="@+id/textView3"
    android:layout_below="@+id/textView3"
    android:layout_marginTop="22dp"
    android:text="Depth"
    android:textAppearance="?android:attr/textAppearanceSmall" />

<TextView
    android:id="@+id/textView1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentTop="true"
    android:layout_centerHorizontal="true"
    android:layout_marginTop="23dp"
    android:text="Rectangular Tank"
    android:textAppearance="?android:attr/textAppearanceLarge" />

<TextView
    android:id="@+id/textView2"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_below="@+id/textView1"
    android:layout_marginTop="54dp"
    android:layout_toLeftOf="@+id/textView1"
    android:text="Length"
    android:textAppearance="?android:attr/textAppearanceSmall" />

<EditText
    android:id="@+id/editText2"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_above="@+id/textView3"
    android:layout_alignLeft="@+id/textView1"
    android:ems="10"
    android:inputType="number" >

    <requestFocus />
</EditText>

<EditText
    android:id="@+id/editText3"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_above="@+id/textView4"
    android:layout_alignLeft="@+id/editText2"
    android:ems="10"
    android:inputType="number" />

<EditText
    android:id="@+id/editText4"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignBottom="@+id/textView4"
    android:layout_alignLeft="@+id/editText3"
    android:ems="10"
    android:inputType="number" />

<Button
    android:id="@+id/button1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_below="@+id/editText4"
    android:layout_centerHorizontal="true"
    android:layout_marginTop="16dp"
    android:text="Convert" />

<EditText
    android:id="@+id/editText1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_below="@+id/button1"
    android:layout_centerHorizontal="true"
    android:layout_marginTop="20dp"
    android:ems="10"
    android:inputType="textMultiLine" />

</RelativeLayout>

1 个答案:

答案 0 :(得分:1)

试试这个..

String strlen = String.valueOf(R.id.editText2);

这是 正确EditText

获取文字的方式

您需要初始化EditText,然后您需要通过此EditText

editText2Text.getText().toString().trim()获取文字
EditText editText2Text = (EditText) findViewById(R.id.editText2);    
EditText editText3Text = (EditText) findViewById(R.id.editText3);    
EditText editText4Text = (EditText) findViewById(R.id.editText4);

String strlen = editText2Text.getText().toString().trim();
String strwid = editText3Text.getText().toString().trim();
String strdep = editText4Text.getText().toString().trim();

编辑1:

    try{
          length = Double.parseDouble(editText2Text.getText().toString());
          width = Double.parseDouble(editText3Text.getText().toString());
          depth = Double.parseDouble(editText4Text.getText().toString());
    }catch (NumberFormatException e){
        length=0;
        width=0;
        depth=0;
    }

编辑2:

我发现了问题..你没有点击按钮从edittext获取文本。尝试按下按钮进行操作。它会工作..

Button button = (Button) findViewById(R.id.button1);
    button.setOnClickListener(new View.OnClickListener() {
        public void onClick(View v) {
try{
              length = Double.parseDouble(editText2Text.getText().toString());
              width = Double.parseDouble(editText3Text.getText().toString());
              depth = Double.parseDouble(editText4Text.getText().toString());
        }catch (NumberFormatException e){
            length=0;
            width=0;
            depth=0;
        }

     RectangularTank recTank = new RectangularTank(length,width,depth);
    final String strConvertedToGallons = recTank.convertToGallons(length,width,depth);

            gallonText.setText(strConvertedToGallons);
        }
    });