第一阶段实现最基本的四则运算计算,最原始的所以还没有美化
xml文件
<LinearLayout 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"
tools:context=".MainActivity"
android:orientation="vertical">
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
tools:context=".MainActivity"
android:orientation="horizontal">
<EditText
android:id="@+id/num1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="12px"
android:text="请输入第一个数字"/>
<TextView
android:id="@+id/tips"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text=""/>
<EditText
android:id="@+id/num2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="12px"
android:text="请输入第二个数字"/>
<TextView
android:id="@+id/txt"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="="/>
<TextView
android:id="@+id/result"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="12px"
android:text="计算结果"/>
</LinearLayout>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
tools:context=".MainActivity"
android:orientation="horizontal">
<Button
android:id="@+id/add"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="+"/>
<Button
android:id="@+id/sub"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="-"/>
<Button
android:id="@+id/mul"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="*"/>
<Button
android:id="@+id/div"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="/"/>
</LinearLayout>
</LinearLayout>
.java文件
public class MainActivity extends Activity {
private EditText num1=null;
private EditText num2=null;
private TextView result=null;
//表示的是符号
private TextView tips=null;
private Button butAdd=null;
private Button butSub=null;
private Button butMul=null;
private Button butDiv=null;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
this.num1=(EditText)super.findViewById(R.id.num1);
this.num2=(EditText)super.findViewById(R.id.num2);
this.result=(TextView)super.findViewById(R.id.result);
this.tips=(TextView)super.findViewById(R.id.tips);
this.butAdd=(Button)super.findViewById(R.id.add);
this.butSub=(Button)super.findViewById(R.id.sub);
this.butMul=(Button)super.findViewById(R.id.mul);
this.butDiv=(Button)super.findViewById(R.id.div);
//增加监听
this.butAdd.setOnClickListener(new AddListener());
this.butSub.setOnClickListener(new SubListener());
this.butMul.setOnClickListener(new MulListener());
this.butDiv.setOnClickListener(new DivListener());
this.num1.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
MainActivity.this.num1.setText("");
}
});
this.num2.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
MainActivity.this.num2.setText("");
}
});
}
//加法操作
public class AddListener implements OnClickListener{
int onenum=0;
int twonum=0;
public void onClick(View v) {
onenum=Integer.parseInt(MainActivity.this.num1.getText().toString());
twonum=Integer.parseInt(MainActivity.this.num2.getText().toString());
MainActivity.this.tips.setText("+");
MainActivity.this.result.setText((onenum+twonum)+"");
}
}
//减法操作
public class SubListener implements OnClickListener{
int onenum=0;
int twonum=0;
public void onClick(View v) {
onenum=Integer.parseInt(MainActivity.this.num1.getText().toString());
twonum=Integer.parseInt(MainActivity.this.num2.getText().toString());
MainActivity.this.tips.setText("-");
MainActivity.this.result.setText((onenum-twonum)+"");
}
}
//乘法操作
public class MulListener implements OnClickListener{
int onenum=0;
int twonum=0;
public void onClick(View v) {
onenum=Integer.parseInt(MainActivity.this.num1.getText().toString());
twonum=Integer.parseInt(MainActivity.this.num2.getText().toString());
MainActivity.this.tips.setText("*");
MainActivity.this.result.setText((onenum*twonum)+"");
}
}
//除法操作
public class DivListener implements OnClickListener{
int onenum=0;
int twonum=0;
public void onClick(View v) {
onenum=Integer.parseInt(MainActivity.this.num1.getText().toString());
twonum=Integer.parseInt(MainActivity.this.num2.getText().toString());
MainActivity.this.tips.setText("/");
MainActivity.this.result.setText((onenum/twonum)+"");
}
}
}