gpt4 book ai didi

android - EditText float 不返回值

转载 作者:行者123 更新时间:2023-11-30 04:26:49 25 4
gpt4 key购买 nike

我试图从编辑文本字符串中获取 a、b 和 c 值作为 float ,但它们似乎不会在创建新类时更新二次类。

安卓代码。在第一部分中,“savenum”是我希望更新 a、b 和 c float 的地方。当我稍后使用方法 final Quadratic quad = new Quadratic (a, b, c) 调用它们时,但它们似乎没有更新。应显示在编辑文本框中输入的值的对话框中显示的值为 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>

二次类。在本类(class)中,正在制作二次方程式。有几个方法调用用于不同的事情来做或讲述二次。在 android Activity 中,为每个函数调用此类。:

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); 时,a b 和 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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com