package com.example.test1;
import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
public class MainActivity extends Activity implements OnClickListener{
//定义控件
private EditText num1;
private EditText num2;
private EditText result ;
private Button btn1;
private Button btn2;
private Button btn3;
private Button btn4;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//加载xml界面
setContentView(R.layout.activity_main);
//绑定控件
num1 = (EditText)this.findViewById(R.id.editText1);
num2 = (EditText)this.findViewById(R.id.editText2);
result = (EditText)this.findViewById(R.id.editText3);
btn1 = (Button)this.findViewById(R.id.button1);
btn2 = (Button)this.findViewById(R.id.button2);
btn3 = (Button)this.findViewById(R.id.button3);
btn4 = (Button)this.findViewById(R.id.button4);
//设置按钮的监听事件
num1.setOnClickListener(this);
num2.setOnClickListener(this);
result.setOnClickListener(this);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
String Num1 = num1.getText().toString();
String Num2 = num1.getText().toString();
String Result = result.getText().toString();
switch(arg0.getId())
{
case R.id.button1:
Result=Num1 + Num2;
break;
case R.id.button2:
Result=Num1 - Num2;
break;
case R.id.button3:
Result=Num1 * Num2;
break;
case R.id.button4:
Result=Num1 / Num2;
break;
}
}
}
java中的写法和C中的写法,是有一定的区别的