0

我在获取程序的 editText 中的值时遇到问题。我在 textView 中设置了值,以便我可以查看代码是否得到它。但不幸的是,它显示如下:

android.widget.EditText@410e5a58 -> @ 符号后的数字在模拟器中每次运行都会改变

为什么会发生这种情况?这是我的代码:

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);

        }
    });
}
}
4

4 回答 4

6

使用netSalary而不是net_salary

td = String.valueOf(netSalary);

当您传递编辑文本而不是双变量时

于 2012-12-06T13:34:44.380 回答
1

您正在传递EditText( net_salary)的对象,td因此td将具有net_salary对象的字符串表示形式。

您已将值存储在netSalaryvaribale 中,因此请改为传递:

td = String.valueOf(netSalary);

并供您参考:

android.widget.EditText@410e5a58是您的EditText对象的字符串表示形式(即您的对象的调用toString方法Edittext将返回此字符串)。

之后的值@是对象的内存地址。每次它都会发生变化,因为每次您的对象都是在不同的内存位置创建的。

于 2012-12-06T13:34:09.747 回答
1

试试这个,

tax_due.setText( net_salary.getText().toString());
于 2012-12-06T13:35:03.287 回答
1

如果问题在这里:

td = String.valueOf(net_salary);

那是因为您正在尝试处理net_salary,其中设置了字符串值的小部件,就好像它只是一个字符串一样。尝试:

td = String.valueOf(net_salary.getText().toString());
于 2012-12-06T13:35:51.827 回答