[英]Getting the value in EditText Android
I am having a problem getting the value in the editText of my program. 我在获取程序的editText中的值时遇到问题。 I set the value in a textView so that I can see if the code gets it.
我在textView中设置了值,以便可以查看代码是否得到它。 But unfortunately it displays something like this:
但不幸的是,它显示如下内容:
android.widget.EditText@410e5a58 -> the number after @ sign changes every run in the emulator android.widget.EditText@410e5a58-> @符号后的数字会更改模拟器中的每次运行
Why this happens? 为什么会这样? This is my code:
这是我的代码:
package com.example.ITax;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
/**
* Created with IntelliJ IDEA.
* User: Karla Mae Jaro
* Date: 12/3/12
* Time: 3:58 PM
* To change this template use File | Settings | File Templates.
*/
public class AnnualComputation extends MyActivity
{
String civil_status2;
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.annual_computation);
Bundle extras = getIntent().getExtras();
if(extras != null)
{
civil_status2 = extras.getString("user_status");
}
final Button btn_compute = (Button) findViewById(R.id.btn_compute_from_annual);
final Button btn_back = (Button) findViewById(R.id.btn_back_from_annual_computation);
btn_back.setOnClickListener(new Button.OnClickListener()
{
@Override
public void onClick(View v)
{
Intent i = new Intent(getApplicationContext(), OpenChoices.class);
startActivity(i);
}
});
btn_compute.setOnClickListener(new Button.OnClickListener()
{
@Override
public void onClick(View v)
{
final EditText net_salary = (EditText) findViewById(R.id.et_net_annual);
final EditText tax_due = (EditText) findViewById(R.id.et_taxdue);
final EditText tax_exe = (EditText) findViewById(R.id.et_taxexemption);
final TextView txt3 = (TextView) findViewById(R.id.textView3);
final TextView txt4 = (TextView) findViewById(R.id.textView4);
final TextView txt5 = (TextView) findViewById(R.id.textView5);
final TextView txt6 = (TextView) findViewById(R.id.textView6);
final TextView txt7 = (TextView) findViewById(R.id.textView7);
final TextView txt8 = (TextView) findViewById(R.id.textView8);
double netSalary, taxDue, rate = 0, exemption = 0, additional = 0, lowerlimit = 0, total;
String ns, td, r, e, a, t;
ns = net_salary.getText().toString();
netSalary = Double.parseDouble(ns);
/* Getting the tax exemption */
if ("SME".equals(civil_status2))
{
exemption = 50000;
}
else if ("SM1".equals(civil_status2))
{
exemption = 25000;
}
else if ("SM2".equals(civil_status2))
{
exemption = 50000;
}
else if ("SM3".equals(civil_status2))
{
exemption = 75000;
}
else if ("SM4".equals(civil_status2))
{
exemption = 100000;
}
/* Getting the rate, additional, lowerlimit */
if(netSalary <= 10000)
{
rate = 0.05;
}
else if((netSalary > 10000) && (netSalary <=30000))
{
rate = 0.1;
additional = 5000;
lowerlimit = 10000;
}
else if ((netSalary > 30000) && (netSalary <= 70000))
{
rate = 0.15;
additional = 2500;
lowerlimit = 30000;
}
else if((netSalary > 70000) && (netSalary <= 14000))
{
rate = 0.20;
additional = 8500;
lowerlimit = 70000;
}
else if ((netSalary > 140000) && (netSalary <= 250000))
{
rate = 0.25;
additional = 22500;
lowerlimit = 140000;
}
else if((netSalary > 250000) && (netSalary <= 500000))
{
rate = 0.30;
additional = 50000;
lowerlimit = 250000;
}
else if (netSalary > 500000)
{
rate = 0.32;
additional = 125000;
lowerlimit = 500000;
}
taxDue = netSalary - exemption;
total = taxDue - lowerlimit;
total = total * rate;
total = total + additional;
/* Converting exemption from Double to String */
td = String.valueOf(net_salary);
e = String.valueOf(exemption);
a = String.valueOf(additional);
r = String.valueOf(rate);
t = String.valueOf(total);
/* Placing the value to the editText (textbox) */
tax_due.setText(td);
tax_exe.setText(e);
txt3.setText(civil_status2);
txt4.setText(td);
txt5.setText(e);
txt6.setText(t);
txt7.setText(r);
txt8.setText(a);
}
});
}
}
use netSalary
instead of net_salary
使用
netSalary
而不是net_salary
td = String.valueOf(netSalary);
As you are passing the edit text instead of the double variable 在传递编辑文本而不是double变量时
You are passing an object of EditText
( net_salary
) in td
so td
will have the the string representation of your net_salary
object. 您在
td
中传递了EditText
( net_salary
)对象,因此td
将具有net_salary
对象的字符串表示形式。
You have stored your value in netSalary
varibale so pass that instead : 您已将您的价值存储在
netSalary
,请通过传递它:
td = String.valueOf(netSalary);
And for your information : 并且供您参考:
android.widget.EditText@410e5a58
is the string representation of your EditText
object (ie calling toString
method of your Edittext
object will return this string) . android.widget.EditText@410e5a58
是您的EditText
对象的字符串表示形式(即,调用您的Edittext
对象的toString
方法将返回此字符串)。
And value after @
is the memory address of your object. @
之后的值是对象的内存地址。 And every time it changes because every time your object is created at different memory locations. 每次更改时都是因为每次在不同的内存位置创建对象。
尝试这个,
tax_due.setText( net_salary.getText().toString());
If the problem is here: 如果问题在这里:
td = String.valueOf(net_salary);
Then it is because you are trying to treat net_salary
, a widget with a string value set in it, as if it were just a string. 这是因为您试图将
net_salary
(其中设置了字符串值的小部件) net_salary
一个字符串来对待。 Try: 尝试:
td = String.valueOf(net_salary.getText().toString());
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.