实战——简易计算器

实战——简易计算器

新建一个继承Activity类的SimpleCalculatorActivity,并设置布局文件为:simplecalculator.xml

首先在布局文件中定义好布需要的组件。

界面:

<?xml version="1.0" encoding="utf-8"?>

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"

    android:layout_width="match_parent"

    android:layout_height="match_parent"

    android:orientation="vertical" >

 

    <LinearLayout

        android:layout_width="match_parent"

        android:layout_height="wrap_content"

        android:layout_marginTop="10dp"

        android:orientation="horizontal" >

 

        <EditText

            android:id="@+id/simplecalculator_edit01"

            android:layout_width="0dp"

            android:layout_height="wrap_content"

            android:layout_weight="1" />

 

        <TextView

            android:id="@+id/simplecalculator_tv01"

            android:layout_width="wrap_content"

            android:layout_height="wrap_content"

            android:text="@string/add" />

 

 

        <EditText

            android:id="@+id/simplecalculator_edit02"

            android:layout_width="0dp"

            android:layout_height="wrap_content"

            android:layout_weight="1" />

 

 

        <TextView

            android:id="@+id/simplecalculator_tv02"

            android:layout_width="wrap_content"

            android:layout_height="wrap_content"

            android:text="@string/equals" />

 

        <TextView

            android:id="@+id/simplecalculator_tv03"

            android:layout_width="wrap_content"

            android:layout_height="wrap_content"

            android:text="@string/result"

            android:visibility="invisible" />

    </LinearLayout>

 

    <LinearLayout

        android:layout_width="match_parent"

        android:layout_height="wrap_content"

        android:layout_marginTop="10dp"

        android:orientation="horizontal" >

 

        <Button

            android:id="@+id/simplecalculator_btn01"

            style="@android:style/Widget.Button.Inset"

            android:layout_width="0dp"

            android:layout_height="wrap_content"

            android:layout_weight="1"

            android:text="@string/add" />

 

        <Button

            android:id="@+id/simplecalculator_btn02"

            style="@android:style/Widget.Button.Inset"

            android:layout_width="0dp"

            android:layout_height="wrap_content"

            android:layout_weight="1"

            android:text="@string/subtract" />

 

        <Button

            android:id="@+id/simplecalculator_btn03"

            style="@android:style/Widget.Button.Inset"

            android:layout_width="0dp"

            android:layout_height="wrap_content"

            android:layout_weight="1"

            android:text="@string/multiply" />

 

        <Button

            android:id="@+id/simplecalculator_btn04"

            style="@android:style/Widget.Button.Inset"

            android:layout_width="0dp"

            android:layout_height="wrap_content"

            android:layout_weight="1"

            android:text="@string/divide" />

    </LinearLayout>

 

</LinearLayout>

 

最后在Activity类中得到各个组件的实例化对象,并设置4Button按钮的单机事件,在事件处理中先得到2个文本的字符,后根据点击的按钮进行相应的运算。

package lyx.feng.simpletextdemo;

.......

publicclass SimpleCalculatorActivity extends Activity implements

       OnClickListener {

    private Button add;

    private Button subtract;

    private Button multiply;

    private Button divide;

 

    private EditText one;

    private EditText two;

 

    private TextView result;

 

    @Override

    protectedvoid onCreate(Bundle savedInstanceState) {

       super.onCreate(savedInstanceState);

       super.setContentView(R.layout.simplecalculator);

 

       this.initViews();

 

       this.add.setOnClickListener(this);

       this.subtract.setOnClickListener(this);

       this.multiply.setOnClickListener(this);

       this.divide.setOnClickListener(this);

 

    }

 

    privatevoid initViews() {

       this.add = (Button) super.findViewById(R.id.simplecalculator_btn01);

       this.subtract = (Button) super

              .findViewById(R.id.simplecalculator_btn02);

       this.multiply = (Button) super

              .findViewById(R.id.simplecalculator_btn03);

       this.divide = (Button) super.findViewById(R.id.simplecalculator_btn04);

 

       this.one = (EditText) super.findViewById(R.id.simplecalculator_edit01);

       this.two = (EditText) super.findViewById(R.id.simplecalculator_edit02);

 

       this.result = (TextView) super.findViewById(R.id.simplecalculator_tv03);

    }

 

    @Override

    publicvoid onClick(View v) {

       String first = this.one.getText().toString().trim();

       String second = this.two.getText().toString().trim();

       if (TextUtils.isEmpty(first) || TextUtils.isEmpty(second)) {

           Toast.makeText(this, "没有输入数字", Toast.LENGTH_SHORT).show();

           return;

       }

       double temp = 0;

       switch (v.getId()) {

       case R.id.simplecalculator_btn01:

           temp = Double.parseDouble(first) + Double.parseDouble(second);

           // 加法操作

           break;

       case R.id.simplecalculator_btn02:

           // 减法操作

           temp = Double.parseDouble(first) - Double.parseDouble(second);

           break;

       case R.id.simplecalculator_btn03:

           // 乘法操作

           temp = Double.parseDouble(first) * Double.parseDouble(second);

           break;

       case R.id.simplecalculator_btn04:

           // 除法操作

           if (Double.parseDouble(second) == 0) {

              Toast.makeText(this, "除数不能为0", Toast.LENGTH_SHORT).show();

              return;

           }

           temp = Double.parseDouble(first) / Double.parseDouble(second);

           break;

       }

       this.result.setVisibility(View.VISIBLE);

       this.result.setText("" + temp);

    }

}

 

 

运行结果:

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值