今天要做的安卓应用是一个简单的计算器加减乘除
comput:
首先要做的是对其页面布局进行操作,除了1234567890这几个按钮还需要(+) (-) (*) (/)(clear)(.)(back)按钮,对于这次计算器的并排问题如何处理除了设置按钮的大小之外建议
按如下方法
<TableRow
android:id="@+id/tableRow_et"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<EditText
android:id="@+id/et"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_span="4"
android:focusable="false"
android:singleLine="true"
android:inputType="text"
android:gravity="right">
</EditText>
</TableRow>
<TableRow
android:id="@+id/tableRow1"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<Button
android:id="@+id/bt_7"
android:layout_width="40px"
android:layout_height="40px"
android:text="7" />
<Button
android:id="@+id/bt_8"
android:layout_width="40px"
android:layout_height="40px"
android:text="8" />
<Button
android:id="@+id/bt_9"
android:layout_width="40px"
android:layout_height="40px"
android:text="9" />
<Button
android:id="@+id/bt_back"
android:layout_width="40px"
android:layout_height="40px"
android:text="back" />
</TableRow>
在使用TableRow之后会显得非常工整。
android:layout_width="fill_parent"
android:layout_height="fill_parent"会使对应的组件填充整个table。
下面是源代码的说明:
会用的到的几个包
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
首先声明内些按钮和
private EditText et_play; // 显示
private String str_oper = "+"; // 运算符
private StringBuffer str_display = new StringBuffer();; // 显示
private String str_result; // 结果显示
private double num1;
private double num2;
private boolean flag = true; // 小数点个数开关控制;
private boolean b_sub, b_mul, b_div; // 运算符开关控制
之后对其进行实例化
bt_0 = (Button) findViewById(R.id.bt_0);
bt_1 = (Button) findViewById(R.id.bt_1);
bt_2 = (Button) findViewById(R.id.bt_2);
bt_3 = (Button) findViewById(R.id.bt_3);
bt_4 = (Button) findViewById(R.id.bt_4);
bt_5 = (Button) findViewById(R.id.bt_5);
bt_6 = (Button) findViewById(R.id.bt_6);
bt_7 = (Button) findViewById(R.id.bt_7);
bt_8 = (Button) findViewById(R.id.bt_8);
bt_9 = (Button) findViewById(R.id.bt_9);
bt_add = (Button) findViewById(R.id.bt_add);
bt_sub = (Button) findViewById(R.id.bt_sub);
bt_multiply = (Button) findViewById(R.id.bt_multiply);
bt_divide = (Button) findViewById(R.id.bt_divide);
bt_back = (Button) findViewById(R.id.bt_back);
bt_equal = (Button) findViewById(R.id.bt_equal);
bt_point = (Button) findViewById(R.id.bt_point);
bt_clear = (Button) findViewById(R.id.bt_clear);
et_play = (EditText) findViewById(R.id.et);
做一个添加监听的例子
bt_0.setOnClickListener(new OnClickListener()
{
@Override
public void onClick(View v)
{
str_display.append("0");
et_play.setText(str_display.toString());
}
});
当点击的时候会在显示框以字符串显示对应的数字。对于匿名内部类的方法不会的可以百度,我也很多是百度的
以上是对编写简单计算器的叙述,谢谢。