简单的计算停止了,我怎么能做得更好?

时间:2014-01-16 14:14:58

标签: java android button onclicklistener

简单计算,对于每个按钮,我必须定义一个值,我可以直接从按钮获取吗? 它是一个简单的应用程序,但当我想测试它时,我得到错误“app停止”。我正在为所有按钮使用一个监听器,我认为它会导致错误。

import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;

public class MainActivity extends Activity implements OnClickListener {

    Button buttOne, buttTwo, buttThree, buttFour, buttFive, buttSix, buttSeven, buttEight, buttNine, buttZero;
    Button buttSum, buttSubstr;
    Double result, buffer;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        buttOne = (Button) findViewById(R.id.one);
        buttTwo = (Button) findViewById(R.id.two);
        buttThree = (Button) findViewById(R.id.three);
        buttFour = (Button) findViewById(R.id.four);
        buttFive = (Button) findViewById(R.id.five);
        buttSix = (Button) findViewById(R.id.six);
        buttSeven = (Button) findViewById(R.id.seven);
        buttEight = (Button) findViewById(R.id.eight);
        buttNine = (Button) findViewById(R.id.nine);
        buttZero = (Button) findViewById(R.id.zero);

        buttSum = (Button) findViewById(R.id.sum);
        buttSubstr = (Button) findViewById(R.id.substr);

             buttOne.setOnClickListener(this);
     buttTwo.setOnClickListener(this);
     buttThree.setOnClickListener(this);
     buttFour.setOnClickListener(this);
      buttFive.setOnClickListener(this);
      buttSix.setOnClickListener(this);
      buttSeven.setOnClickListener(this);
      buttEight.setOnClickListener(this);
      buttNine.setOnClickListener(this);
      buttZero.setOnClickListener(this); 
      buttSum.setOnClickListener(this);
      buttSubstr.setOnClickListener(this);  

        result = 0.0;
    }

    /*
     * @Override public boolean onCreateOptionsMenu(Menu menu) { // Inflate the menu; this adds items to the action bar if it is present.
     * getMenuInflater().inflate(R.menu.main, menu); return true; }
     */

    @Override
    public void onClick(View v) {

        switch (v.getId()) {

        case R.id.one:
            buffer = 1.0;
            break;
        case R.id.two:
            buffer = 2.0;
            break;
        case R.id.three:
            buffer = 3.0;
            break;
        case R.id.four:
            buffer = 4.0;
            break;
        case R.id.five:
            buffer = 5.0;
            break;
        case R.id.six:
            buffer = 6.0;
            break;
        case R.id.seven:
            buffer = 7.0;
            break;
        case R.id.eight:
            buffer = 8.0;
            break;
        case R.id.nine:
            buffer = 9.0;
            break;
        case R.id.zero:
            buffer = 0.0;
            break;
        case R.id.substr:
            result = -buffer;
            break;
        case R.id.sum:
            result = +buffer;
        }
    }

}

布局:

    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@style/AppTheme"
android:gravity="center_horizontal"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".MainActivity" >

<Button
    android:id="@+id/seven"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="@string/seven" />

<Button
    android:id="@+id/eight"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignBaseline="@+id/seven"
    android:layout_alignBottom="@+id/seven"
    android:layout_toRightOf="@+id/seven"
    android:text="@string/eight" />

<Button
    android:id="@+id/nine"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignBaseline="@+id/eight"
    android:layout_alignBottom="@+id/eight"
    android:layout_toRightOf="@+id/eight"
    android:text="@string/nine" />

<Button
    android:id="@+id/four"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignLeft="@+id/seven"
    android:layout_below="@+id/seven"
    android:text="@string/four" />

<Button
    android:id="@+id/five"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignBaseline="@+id/four"
    android:layout_alignBottom="@+id/four"
    android:layout_toRightOf="@+id/four"
    android:text="@string/five" />

<Button
    android:id="@+id/six"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignLeft="@+id/nine"
    android:layout_below="@+id/nine"
    android:text="@string/six" />

<Button
    android:id="@+id/one"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignLeft="@+id/four"
    android:layout_below="@+id/four"
    android:text="@string/one" />

<Button
    android:id="@+id/two"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignLeft="@+id/five"
    android:layout_below="@+id/five"
    android:text="@string/two" />

<Button
    android:id="@+id/three"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignLeft="@+id/six"
    android:layout_below="@+id/six"
    android:text="@string/three" />

<Button
    android:id="@+id/zero"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_below="@+id/one"
    android:layout_toLeftOf="@+id/three"
    android:text="@string/zero" />

<Button
    android:id="@+id/division"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_toRightOf="@+id/nine"
    android:text="@string/division" />

<Button
    android:id="@+id/multiple"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignBaseline="@+id/six"
    android:layout_alignBottom="@+id/six"
    android:layout_toRightOf="@+id/six"
    android:text="@string/multiple" />

<Button
    android:id="@+id/substr"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_below="@+id/six"
    android:layout_toRightOf="@+id/six"
    android:text="@string/subst" />

<Button
    android:id="@+id/sum"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_below="@+id/three"
    android:layout_toRightOf="@+id/three"
    android:text="@string/sum" />

<Button
    android:id="@+id/result"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignBaseline="@+id/sum"
    android:layout_alignBottom="@+id/sum"
    android:layout_toLeftOf="@+id/sum"
    android:text="@string/result" />

<Button
    android:id="@+id/clear"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignLeft="@+id/one"
    android:layout_below="@+id/one"
    android:text="@string/clear" />

<EditText
    android:id="@+id/output"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_above="@+id/eight"
    android:layout_alignRight="@+id/division"
    android:ems="10"
    android:inputType="numberDecimal" >

    <requestFocus />
</EditText>

<EditText
    android:id="@+id/editText1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_above="@+id/eight"
    android:layout_alignRight="@+id/division"
    android:ems="10"
    android:inputType="numberDecimal" />

控制台:

    [2014-01-16 15:42:41 - ddmlib] An established connection was aborted by the software installed on the host computer java.io.IOException: An established connection was aborted by the software installed on the host computer
at sun.nio.ch.SocketDispatcher.write0(Native Method)
at sun.nio.ch.SocketDispatcher.write(Unknown Source)
at sun.nio.ch.IOUtil.writeFromNativeBuffer(Unknown Source)
at sun.nio.ch.IOUtil.write(Unknown Source)
at sun.nio.ch.SocketChannelImpl.write(Unknown Source)
at com.android.ddmlib.JdwpPacket.writeAndConsume(JdwpPacket.java:213)
at com.android.ddmlib.Client.sendAndConsume(Client.java:642)
at com.android.ddmlib.HandleHeap.sendREAQ(HandleHeap.java:348)
at com.android.ddmlib.Client.requestAllocationStatus(Client.java:488)
at com.android.ddmlib.DeviceMonitor.createClient(DeviceMonitor.java:835)
at com.android.ddmlib.DeviceMonitor.openClient(DeviceMonitor.java:803)
at com.android.ddmlib.DeviceMonitor.processIncomingJdwpData(DeviceMonitor.java:763)
at com.android.ddmlib.DeviceMonitor.deviceClientMonitorLoop(DeviceMonitor.java:652)
at com.android.ddmlib.DeviceMonitor.access$100(DeviceMonitor.java:44)
at com.android.ddmlib.DeviceMonitor$3.run(DeviceMonitor.java:580)

2 个答案:

答案 0 :(得分:0)

您需要添加onClickListeners。

buttOne.setOnClickListener(this);

等......:)

答案 1 :(得分:0)

也许this适合你。使用像OcuS'答案中提议的android:标签。