当前位置:   article > 正文

android四则运算,Android开发之简单的四则运算器的…

安卓1)选择运算方法界面,4种运算方法,选择类型后(只能选择1种类型),进入计算界面

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)+"");

}

}

}

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/知新_RL/article/detail/271975
推荐阅读
相关标签
  

闽ICP备14008679号