EditText浮动不返回值

时间:2011-12-02 14:54:36

标签: android

我正在尝试将a,b和c值作为来自编辑文本字符串的浮点数,但是当创建新的二进制类时,它们似乎不会更新二次类。

Android代码。在第一部分,“savenum”,我希望我的a,b和c浮动更新。当我稍后用方法最后的Quadratic quad = new Quadratic(a,b,c)调用它们时,它们似乎没有更新。应显示在edittext框中输入的值的对话框中显示的值为0.0。 :

package com.bensherman.quadroid;

import android.app.Activity;
import android.app.AlertDialog;
import android.app.AlertDialog.Builder;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;

public class QuadraticdroidActivity extends Activity {
    /** Called when the activity is first created. */

    private EditText entA, entB, entC;
    float a, b, c;

    @Override 
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        SharedPreferences QuadPref = this.getSharedPreferences("QUADROID", MODE_PRIVATE);
        final SharedPreferences.Editor QuadEdit = QuadPref.edit();

        Button savenum = (Button) findViewById(R.id.btnSave);
        savenum.setOnClickListener(new OnClickListener() {
            public void onClick(View v) {

                       entA = (EditText) findViewById(R.id.entA);  
                       float a = Float.parseFloat(entA.getText().toString()); 
                       QuadEdit.putFloat("a", a).commit();

                       entB = (EditText) findViewById(R.id.entB);  
                       float b = Float.parseFloat(entB.getText().toString()); 
                       QuadEdit.putFloat("b", b).commit();

                       entC = (EditText) findViewById(R.id.entC);  
                       float c = Float.parseFloat(entC.getText().toString()); 
                       QuadEdit.putFloat("c", c).commit();
                    }
                });  

        final Quadratic quad = new Quadratic(a, b, c);

        final Builder showX = new AlertDialog.Builder(this);
        Button getX =  (Button) findViewById(R.id.getXb);
        showX.setTitle("X Value");
        getX.setOnClickListener(new OnClickListener() {
            public void onClick(View v) {
                showX.setMessage("X: " + Math.round(quad.getXvalue()));
                showX.show();
            }
        });

        final Builder showY = new AlertDialog.Builder(this);
        Button getY =  (Button) findViewById(R.id.getYb);
        showY.setTitle("Y Value");
        getY.setOnClickListener(new OnClickListener() {
            public void onClick(View v) {
                showY.setMessage("Y: " + Math.round(quad.getYvalue()));
                showY.show();
            }
        });

        final Builder showA = new AlertDialog.Builder(this);
        Button getA = (Button) findViewById(R.id.getAb);
        showA.setTitle("A Value");
        getA.setOnClickListener(new OnClickListener() {
            public void onClick(View v) {
                showA.setMessage("A: " + quad.getA());
                showA.show();
            }
        });

        final Builder showB = new AlertDialog.Builder(this);
        Button getB = (Button) findViewById(R.id.getBb);
        showB.setTitle("B Value");
        getB.setOnClickListener(new OnClickListener() {
            public void onClick(View v) {
                showB.setMessage("B: " + quad.getB());
                showB.show();
            }
        });

        final Builder showC = new AlertDialog.Builder(this);
        Button getC = (Button) findViewById(R.id.getCb);
        showC.setTitle("C Value");
        getC.setOnClickListener(new OnClickListener() {
            public void onClick(View v) {
                showC.setMessage("C: " + quad.getC());
                showC.show();
            }
        });

        final Builder showSF = new AlertDialog.Builder(this);
        Button getSF = (Button) findViewById(R.id.getSFb);
        showSF.setTitle("Standard Form");
        getSF.setOnClickListener(new OnClickListener() {
            public void onClick(View v) {
                showSF.setMessage("Stand. Form: " + quad.getStandardForm());
                showSF.show();
            }
        });
    } 
}  

XML有3个edittext字段是numberSigned以允许否定。然后有几个按钮,单击时,显示Quadratic类中的方法。 :

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/RelativeLayout1"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >

    <EditText
        android:id="@+id/entA"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_alignParentTop="true"
        android:hint="@string/ahint"
        android:inputType="numberSigned" >

        <requestFocus />
    </EditText>

    <EditText
        android:id="@+id/entB"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_below="@+id/entA"
        android:hint="@string/bhint"
        android:inputType="numberSigned" />

    <EditText
        android:id="@+id/entC"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_below="@+id/entB"
        android:hint="@string/chint"
        android:inputType="numberSigned" />

    <Button
        android:id="@+id/btnSave"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_below="@+id/entC"
        android:text="@string/savebutton" />

    <Button
        android:id="@+id/getXb"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_below="@+id/btnSave"
        android:text="@string/getX" />

    <Button
        android:id="@+id/getAb"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentRight="true"
        android:layout_below="@+id/entC"
        android:text="@string/getA" />

    <Button
        android:id="@+id/getCb"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentRight="true"
        android:layout_below="@+id/getXb"
        android:text="@string/getC" />

    <Button
        android:id="@+id/getBb"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentRight="true"
        android:layout_below="@+id/getAb"
        android:text="@string/getB" />

    <Button
        android:id="@+id/getYb"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignBaseline="@+id/getCb"
        android:layout_alignBottom="@+id/getCb"
        android:layout_alignParentLeft="true"
        android:text="@string/getY" />

    <Button
        android:id="@+id/getSFb"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_below="@+id/getYb"
        android:text="@string/getSF" />

</RelativeLayout>

二次类。在这个课程中,正在制作二次方。有几种方法调用不同的事情要做或告诉二次方。在android活动中,为每个函数调用此类。:

package com.bensherman.quadroid;


public class Quadratic {

    private double a, b, c, y, x, s;

    public Quadratic()  
    {
        a = 1;
        b = 2;
        c = -1;
    }

    public Quadratic (double myA, double myB, double myC)
    {
        a = myA;
        b = myB;
        c = myC;
    }
    public double getA()
    {
        return a;
    }

    public double getB()
    {
        return b;
    }

    public double getC()
    {
        return c;
    }

    public double getSolution1()
    {
        return (-(b) + Math.sqrt((Math.pow(b, 2))-4*a*c))/(2*a);
    }

    public double getSolution2()
    {
        return (-(b) - Math.sqrt((Math.pow(b, 2))-4*a*c))/(2*a);
    }

    public double getXvalue()
    {
        return (-b)/(2*a);
    }

    public double getYvalue()
    {
        return (a*(Math.pow(x,2))) + (b*x) + c;
    }

    public String getStandardForm()
    {
        if (c < 0){
            return a + "x^2 + " + b + "x " + c;
        }
        else
        {
            return a + "x^2 + " + b + "x +" + c;
        }
    }
}

1 个答案:

答案 0 :(得分:1)

当您在onCreate中调用final Quadratic quad = new Quadratic(a, b, c);时,ab和c尚未初始化,因此它们将始终为0.按钮处理程序中的代码在按下按钮之前不会执行,因此即使它出现在源中的final Quadratic quad = new Quadratic(a, b, c);行,尚未执行。

除此之外,即使它被执行了,a,b和c仍然是未初始化的,因为你要声明具有相同名称的局部变量并分配给那些而不是使用你已经定义的实例变量。 / p>

您可以做的是将quad从局部变量移动到实例变量。然后,当您从编辑文本更新a,b,c时,可以在按钮处理程序中重新初始化它:

public class QuadraticdroidActivity extends Activity {

    private Quadratic quad;
    private EditText entA, entB, entC;
    float a, b, c;

    public void onCreate(Bundle savedInstanceState) {
       super.onCreate(savedInstanceState);
       setContentView(R.layout.main);

       SharedPreferences QuadPref = this.getSharedPreferences("QUADROID", MODE_PRIVATE);

       final SharedPreferences.Editor QuadEdit = QuadPref.edit();
       a = QuadEdit.getFloat("a",0f);
       b = QuadEdit.getFloat("b",0f);
       c = QuadEdit.getFloat("c",0f);
//initialize here since the initialization in the button handler won't happen until someone clicks the button
           quad = new Quadtratic (a,b,c);
       Button savenum = (Button) findViewById(R.id.btnSave);
       savenum.setOnClickListener(new OnClickListener() {
            public void onClick(View v) {

                   entA = (EditText) findViewById(R.id.entA);  
                   //got rid of the "float" here so now we're referring to the instance     variable instead of a local
                   a = Float.parseFloat(entA.getText().toString()); 
                   QuadEdit.putFloat("a", a).commit();

                   entB = (EditText) findViewById(R.id.entB);  
                   b = Float.parseFloat(entB.getText().toString()); 
                   QuadEdit.putFloat("b", b).commit();

                   entC = (EditText) findViewById(R.id.entC);  
                   c = Float.parseFloat(entC.getText().toString()); 
                   QuadEdit.putFloat("c", c).commit();

                   quad = new Quadratic(a,b,c);
                }
            });