今天做了个简易计算器,先看看效果,嘿嘿。。。。。。。
加法:
减法:
乘法:
除法:
布局代码:
<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<TableRow>
<EditText
android:id="@+id/num1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
/>
<TextView
android:id="@+id/fuhao"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
/>
<EditText
android:id="@+id/num2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
/>
<TextView android:text="="
android:layout_width="wrap_content"
android:layout_height="wrap_content"
/>
<TextView
android:id="@+id/result"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
/>
</TableRow>
<TableRow>
<Button
android:id="@+id/button1"
android:layout_width="50dip"
android:layout_height="30dip"
android:text="+"
/>
<Button
android:id="@+id/button2"
android:layout_width="50dip"
android:layout_height="30dip"
android:text="-"
/>
<Button
android:id="@+id/button3"
android:layout_width="50dip"
android:layout_height="30dip"
android:text="*"
/>
<Button
android:id="@+id/button4"
android:layout_width="50dip"
android:layout_height="30dip"
android:text="/"
/>
</TableRow>
</TableLayout>
java类
package wn.stu.calculator;
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
public class CalculatorActivity extends Activity {
private EditText num1;
private EditText num2;
private TextView fuhao;
private TextView result;
private Button button1;
private Button button2;
private Button button3;
private Button button4;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_calculator);
num1=(EditText) this.findViewById(R.id.num1);
num2=(EditText) this.findViewById(R.id.num2);
fuhao=(TextView) this.findViewById(R.id.fuhao);
result=(TextView) this.findViewById(R.id.result);
button1=(Button) this.findViewById(R.id.button1);
button2=(Button) this.findViewById(R.id.button2);
button3=(Button) this.findViewById(R.id.button3);
button4=(Button) this.findViewById(R.id.button4);
button1.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
float number1=Integer.parseInt(num1.getEditableText().toString());
float number2=Integer.parseInt(num2.getEditableText().toString());
fuhao.setText(" +");
float r=number1+number2;
System.out.println(r);
result.setText(""+r);
}
});
button2.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
float number1=Integer.parseInt(num1.getEditableText().toString());
float number2=Integer.parseInt(num2.getEditableText().toString());
fuhao.setText(" -");
float r=number1-number2;
result.setText(""+r);
System.out.println(r);
}
});
button3.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
float number1=Integer.parseInt(num1.getEditableText().toString());
float number2=Integer.parseInt(num2.getEditableText().toString());
fuhao.setText(" *");
float r=number1*number2;
result.setText(""+r);
System.out.println(r);
}
});
button4.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
float number1=Integer.parseInt(num1.getEditableText().toString());
float number2=Integer.parseInt(num2.getEditableText().toString());
fuhao.setText(" /");
float r=number1/number2;
result.setText(""+r);
System.out.println(r);
}
});
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.activity_calculator, menu);
return true;
}
}
得到的r的类型是float,而setText();要string类型的," "+r相当于String.valueOf(int r);
开始我在布局中button设置的大小是80dip,50dip,但是显示不出来第五个组件,改小之后就可以了。