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

Android代码。在第一部分中,“ savenum”是我期望a,b和c浮点数被更新的地方。稍后当我使用final 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;
        }
    }
}

最佳答案

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

除此之外,即使您执行了a,b和c操作,它们仍然会被初始化,因为您声明的局部变量具有相同的名称并分配给它们,而不是使用已经定义的实例变量。

您可以做的是将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);
                }
            });

关于android - EditText float 不返回值,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/8358139/

10-10 17:46