实战——简易计算器
新建一个继承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类中得到各个组件的实例化对象,并设置4个Button按钮的单机事件,在事件处理中先得到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); } }
|
运行结果: