将edittext和textview的值一起添加并在文本视图中显示总计

时间:2012-12-30 23:38:13

标签: java android

我正在创建一个简单的Android应用程序,其中包含editText字段和textView字段,这些字段中的值仅为数字。

我创建了一个按钮,在选择时,我希望将编辑文本和文本视图的值添加到一起,然后我想将editText更改为值'0'并且总计要显示在textView。

例如,如果editText的值为10,而按下按钮时textView的值为20,则textView中显示总数为30,而editText的值显示为0.

到目前为止,这是我的代码,当我在应用程序崩溃时按下按钮,所以我不确定我是否正确编码。我是java的新手。任何帮助将不胜感激。

public class durood extends Activity{

TextView txtCount;
TextView totalCount;
EditText enteramount;
Button btnCount;
Button addtotal1;
Button addtotal2;
Button cleartotal;
static int count=0;
double x=0;
double y=0;
double z=0;

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// The activity is being created.
setContentView(R.layout.durood);

enteramount = (EditText)findViewById(R.id.enteramount);
totalCount = (TextView)findViewById(R.id.totalCount);
addtotal1=(Button)findViewById(R.id.addtototal1);
addtotal2=(Button)findViewById(R.id.addtototal2);


//add to total 1
addtotal1.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {

x=Double.parseDouble(enteramount.getText().toString());
y=Double.parseDouble(totalCount.getText().toString());
z=x+y;
totalCount.setText(Double.toString(z));
}


});

logcat的

12-30 23:52:11.098: W/Trace(615): Unexpected value from nativeGetEnabledTags: 0
12-30 23:52:11.129: E/ActivityThread(615): Service com.android.exchange.ExchangeService has leaked ServiceConnection com.android.emailcommon.service.ServiceProxy$ProxyConnection@40d332e8 that was originally bound here
12-30 23:52:11.129: E/ActivityThread(615): android.app.ServiceConnectionLeaked: Service com.android.exchange.ExchangeService has leaked ServiceConnection com.android.emailcommon.service.ServiceProxy$ProxyConnection@40d332e8 that was originally bound here
12-30 23:52:11.129: E/ActivityThread(615):  at android.app.LoadedApk$ServiceDispatcher.<init>(LoadedApk.java:969)
12-30 23:52:11.129: E/ActivityThread(615):  at android.app.LoadedApk.getServiceDispatcher(LoadedApk.java:863)
12-30 23:52:11.129: E/ActivityThread(615):  at android.app.ContextImpl.bindService(ContextImpl.java:1418)
12-30 23:52:11.129: E/ActivityThread(615):  at android.app.ContextImpl.bindService(ContextImpl.java:1407)
12-30 23:52:11.129: E/ActivityThread(615):  at android.content.ContextWrapper.bindService(ContextWrapper.java:473)
12-30 23:52:11.129: E/ActivityThread(615):  at com.android.emailcommon.service.ServiceProxy.setTask(ServiceProxy.java:157)
12-30 23:52:11.129: E/ActivityThread(615):  at com.android.emailcommon.service.ServiceProxy.setTask(ServiceProxy.java:145)
12-30 23:52:11.129: E/ActivityThread(615):  at com.android.emailcommon.service.AccountServiceProxy.getDeviceId(AccountServiceProxy.java:116)
12-30 23:52:11.129: E/ActivityThread(615):  at com.android.exchange.ExchangeService.getDeviceId(ExchangeService.java:1249)
12-30 23:52:11.129: E/ActivityThread(615):  at com.android.exchange.ExchangeService$7.run(ExchangeService.java:1856)
12-30 23:52:11.129: E/ActivityThread(615):  at com.android.emailcommon.utility.Utility$2.doInBackground(Utility.java:551)
12-30 23:52:11.129: E/ActivityThread(615):  at com.android.emailcommon.utility.Utility$2.doInBackground(Utility.java:549)
12-30 23:52:11.129: E/ActivityThread(615):  at android.os.AsyncTask$2.call(AsyncTask.java:287)
12-30 23:52:11.129: E/ActivityThread(615):  at java.util.concurrent.FutureTask.run(FutureTask.java:234)
12-30 23:52:11.129: E/ActivityThread(615):  at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1080)
12-30 23:52:11.129: E/ActivityThread(615):  at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:573)
12-30 23:52:11.129: E/ActivityThread(615):  at java.lang.Thread.run(Thread.java:856)
12-30 23:52:11.238: E/StrictMode(615): null
12-30 23:52:11.238: E/StrictMode(615): android.app.ServiceConnectionLeaked: Service com.android.exchange.ExchangeService has leaked ServiceConnection com.android.emailcommon.service.ServiceProxy$ProxyConnection@40d332e8 that was originally bound here
12-30 23:52:11.238: E/StrictMode(615):  at android.app.LoadedApk$ServiceDispatcher.<init>(LoadedApk.java:969)
12-30 23:52:11.238: E/StrictMode(615):  at android.app.LoadedApk.getServiceDispatcher(LoadedApk.java:863)
12-30 23:52:11.238: E/StrictMode(615):  at android.app.ContextImpl.bindService(ContextImpl.java:1418)
12-30 23:52:11.238: E/StrictMode(615):  at android.app.ContextImpl.bindService(ContextImpl.java:1407)
12-30 23:52:11.238: E/StrictMode(615):  at android.content.ContextWrapper.bindService(ContextWrapper.java:473)
12-30 23:52:11.238: E/StrictMode(615):  at com.android.emailcommon.service.ServiceProxy.setTask(ServiceProxy.java:157)
12-30 23:52:11.238: E/StrictMode(615):  at com.android.emailcommon.service.ServiceProxy.setTask(ServiceProxy.java:145)
12-30 23:52:11.238: E/StrictMode(615):  at com.android.emailcommon.service.AccountServiceProxy.getDeviceId(AccountServiceProxy.java:116)
12-30 23:52:11.238: E/StrictMode(615):  at com.android.exchange.ExchangeService.getDeviceId(ExchangeService.java:1249)
12-30 23:52:11.238: E/StrictMode(615):  at com.android.exchange.ExchangeService$7.run(ExchangeService.java:1856)
12-30 23:52:11.238: E/StrictMode(615):  at com.android.emailcommon.utility.Utility$2.doInBackground(Utility.java:551)
12-30 23:52:11.238: E/StrictMode(615):  at com.android.emailcommon.utility.Utility$2.doInBackground(Utility.java:549)
12-30 23:52:11.238: E/StrictMode(615):  at android.os.AsyncTask$2.call(AsyncTask.java:287)
12-30 23:52:11.238: E/StrictMode(615):  at java.util.concurrent.FutureTask.run(FutureTask.java:234)
12-30 23:52:11.238: E/StrictMode(615):  at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1080)
12-30 23:52:11.238: E/StrictMode(615):  at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:573)
12-30 23:52:11.238: E/StrictMode(615):  at java.lang.Thread.run(Thread.java:856)
12-30 23:52:11.272: W/ActivityManager(290): Unbind failed: could not find connection for android.os.BinderProxy@40fb7e78
12-30 23:52:11.328: E/ActivityThread(615): Service com.android.exchange.ExchangeService has leaked ServiceConnection com.android.emailcommon.service.ServiceProxy$ProxyConnection@40d32ef0 that was originally bound here
12-30 23:52:11.328: E/ActivityThread(615): android.app.ServiceConnectionLeaked: Service com.android.exchange.ExchangeService has leaked ServiceConnection com.android.emailcommon.service.ServiceProxy$ProxyConnection@40d32ef0 that was originally bound here
12-30 23:52:11.328: E/ActivityThread(615):  at android.app.LoadedApk$ServiceDispatcher.<init>(LoadedApk.java:969)
12-30 23:52:11.328: E/ActivityThread(615):  at android.app.LoadedApk.getServiceDispatcher(LoadedApk.java:863)
12-30 23:52:11.328: E/ActivityThread(615):  at android.app.ContextImpl.bindService(ContextImpl.java:1418)
12-30 23:52:11.328: E/ActivityThread(615):  at android.app.ContextImpl.bindService(ContextImpl.java:1407)
12-30 23:52:11.328: E/ActivityThread(615):  at android.content.ContextWrapper.bindService(ContextWrapper.java:473)
12-30 23:52:11.328: E/ActivityThread(615):  at com.android.emailcommon.service.ServiceProxy.setTask(ServiceProxy.java:157)
12-30 23:52:11.328: E/ActivityThread(615):  at com.android.emailcommon.service.ServiceProxy.setTask(ServiceProxy.java:145)
12-30 23:52:11.328: E/ActivityThread(615):  at com.android.emailcommon.service.ServiceProxy.test(ServiceProxy.java:191)
12-30 23:52:11.328: E/ActivityThread(615):  at com.android.exchange.ExchangeService$7.run(ExchangeService.java:1850)
12-30 23:52:11.328: E/ActivityThread(615):  at com.android.emailcommon.utility.Utility$2.doInBackground(Utility.java:551)
12-30 23:52:11.328: E/ActivityThread(615):  at com.android.emailcommon.utility.Utility$2.doInBackground(Utility.java:549)
12-30 23:52:11.328: E/ActivityThread(615):  at android.os.AsyncTask$2.call(AsyncTask.java:287)
12-30 23:52:11.328: E/ActivityThread(615):  at java.util.concurrent.FutureTask.run(FutureTask.java:234)
12-30 23:52:11.328: E/ActivityThread(615):  at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1080)
12-30 23:52:11.328: E/ActivityThread(615):  at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:573)
12-30 23:52:11.328: E/ActivityThread(615):  at java.lang.Thread.run(Thread.java:856)
12-30 23:52:11.398: E/StrictMode(615): null
12-30 23:52:11.398: E/StrictMode(615): android.app.ServiceConnectionLeaked: Service com.android.exchange.ExchangeService has leaked ServiceConnection com.android.emailcommon.service.ServiceProxy$ProxyConnection@40d32ef0 that was originally bound here
12-30 23:52:11.398: E/StrictMode(615):  at android.app.LoadedApk$ServiceDispatcher.<init>(LoadedApk.java:969)
12-30 23:52:11.398: E/StrictMode(615):  at android.app.LoadedApk.getServiceDispatcher(LoadedApk.java:863)
12-30 23:52:11.398: E/StrictMode(615):  at android.app.ContextImpl.bindService(ContextImpl.java:1418)
12-30 23:52:11.398: E/StrictMode(615):  at android.app.ContextImpl.bindService(ContextImpl.java:1407)
12-30 23:52:11.398: E/StrictMode(615):  at android.content.ContextWrapper.bindService(ContextWrapper.java:473)
12-30 23:52:11.398: E/StrictMode(615):  at com.android.emailcommon.service.ServiceProxy.setTask(ServiceProxy.java:157)
12-30 23:52:11.398: E/StrictMode(615):  at com.android.emailcommon.service.ServiceProxy.setTask(ServiceProxy.java:145)
12-30 23:52:11.398: E/StrictMode(615):  at com.android.emailcommon.service.ServiceProxy.test(ServiceProxy.java:191)
12-30 23:52:11.398: E/StrictMode(615):  at com.android.exchange.ExchangeService$7.run(ExchangeService.java:1850)
12-30 23:52:11.398: E/StrictMode(615):  at com.android.emailcommon.utility.Utility$2.doInBackground(Utility.java:551)
12-30 23:52:11.398: E/StrictMode(615):  at com.android.emailcommon.utility.Utility$2.doInBackground(Utility.java:549)
12-30 23:52:11.398: E/StrictMode(615):  at android.os.AsyncTask$2.call(AsyncTask.java:287)
12-30 23:52:11.398: E/StrictMode(615):  at java.util.concurrent.FutureTask.run(FutureTask.java:234)
12-30 23:52:11.398: E/StrictMode(615):  at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1080)
12-30 23:52:11.398: E/StrictMode(615):  at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:573)
12-30 23:52:11.398: E/StrictMode(615):  at java.lang.Thread.run(Thread.java:856)
12-30 23:52:11.448: W/ActivityManager(290): Unbind failed: could not find connection for android.os.BinderProxy@40f92598

XML:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="center"
android:orientation="vertical" >

<LinearLayout
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal" >

 <Button
 android:id="@+id/dmute"
 style="?android:attr/buttonStyleSmall"
 android:layout_width="wrap_content"
 android:layout_height="wrap_content"
 android:layout_marginLeft="3dp"
 android:minHeight="30dp"
 android:minWidth="48dp"
 android:text="Mute" />

 <Button
 android:id="@+id/dreset"
 style="?android:attr/buttonStyleSmall"
 android:layout_width="wrap_content"
 android:layout_height="wrap_content"
 android:layout_marginLeft="198dp"
 android:minHeight="30dp"
 android:minWidth="48dp"
 android:text="Reset" />
</LinearLayout>

 <LinearLayout
 android:layout_width="fill_parent"
 android:layout_height="wrap_content"
 android:orientation="horizontal"
 android:paddingTop="20dp" >

 <TextView
 android:id="@+id/dcount"
 android:layout_width="0dip"
 android:layout_height="wrap_content"
 android:layout_weight="90"
 android:gravity="center"
 android:singleLine="true"
 android:text="Numbers"
 android:textSize="25sp" />

 <Button
 android:id="@+id/addtototal1"
 style="?android:attr/buttonStyleSmall"
 android:layout_width="wrap_content"
 android:layout_height="wrap_content"
 android:layout_weight="5"
 android:text="+ to Total" />

  </LinearLayout>

  <LinearLayout
  android:layout_width="fill_parent"
  android:layout_height="wrap_content"
  android:orientation="horizontal"
  android:paddingTop="20dp" >

  <EditText
  android:id="@+id/enteramount"
  android:layout_width="0dip"
  android:layout_height="wrap_content"
  android:layout_gravity="center"
  android:layout_weight="90"
  android:gravity="center"
  android:hint="Enter Amount"
  android:inputType="number"
  android:singleLine="true"
  android:textSize="25sp" />

  <Button
  android:id="@+id/addtototal2"
  style="?android:attr/buttonStyleSmall"
  android:layout_width="wrap_content"
  android:layout_height="wrap_content"
  android:layout_weight="5"
  android:text="+ to Total" />

 </LinearLayout>

 <LinearLayout
 android:layout_width="fill_parent"
 android:layout_height="wrap_content"
 android:orientation="horizontal"
 android:paddingTop="20dp" >

 <TextView
 android:id="@+id/totalCount"
 android:layout_width="0dip"
 android:layout_height="wrap_content"
 android:layout_gravity="center"
 android:layout_weight="80"
 android:gravity="center"
 android:singleLine="true"
 android:textSize="25sp" />

  <Button
  android:id="@+id/cleartotal"
  style="?android:attr/buttonStyleSmall"
  android:layout_width="wrap_content"
  android:layout_height="wrap_content"
  android:text="Clear" />

 </LinearLayout>

  <Button
  android:id="@+id/dclick"
  android:layout_width="wrap_content"
  android:layout_height="wrap_content"
  android:layout_gravity="center"
  android:layout_marginTop="30dp"
  android:height="120dp"
  android:width="250dp" />

</LinearLayout>

2 个答案:

答案 0 :(得分:2)

在转换为Double之前检查这种情况......

if(enteramount.getText().toString().length()>0)
{
   x=Double.parseDouble(enteramount.getText().toString());
}
if(totalCount.getText().toString().length()>0)
{
   y=Double.parseDouble(totalCount.getText().toString());
}

答案 1 :(得分:1)

您必须初始化按钮addtotal1:

    addtotal1 = (Button)findViewById(R.id.yourButtonId);

之前可以设置onClickListener。

修改

您的xml EditText只有inputType“number”,因此用户无法输入一些小数值。如果你想这样,你不需要双变量。而不是double,使用int,所以你的结果没有显示小数点:

       public class durood extends Activity{

         private TextView txtCount;
         private TextView totalCount;
         private EditText enteramount;
         private Button btnCount;
         private Button addtotal1;
         private Button addtotal2;
         private Button cleartotal;
         private static int count=0;
         private int x=0;
         private int y=0;
         private int z=0;

          @Override
           public void onCreate(Bundle savedInstanceState) {
           super.onCreate(savedInstanceState);
            // The activity is being created.
            setContentView(R.layout.durood);

            enteramount = (EditText)findViewById(R.id.enteramount);
            totalCount = (TextView)findViewById(R.id.totalCount);
            addtotal1=(Button)findViewById(R.id.addtototal1);
            addtotal2=(Button)findViewById(R.id.addtototal2);


            //add to total 1
            addtotal1.setOnClickListener(new View.OnClickListener() {
              public void onClick(View v) {

              x=Integer.parseInt(enteramount.getText().toString());
              y=Integer.parseInt(totalCount.getText().toString());
              z=x+y;
              totalCount.setText(Integer.toString(z));
              //to set value of enteramount to 0
              enteramount.setText("0"); //or enteramaount.setText("") if You want show nothing
            }


           });