Android计算器简单版(2)

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.RadioButton;
import android.widget.RadioGroup;
import android.widget.RadioGroup.OnCheckedChangeListener;
import android.widget.TextView;

public class MainActivity extends Activity {
    /** Called when the activity is first created. */
 
 private EditText number1 = null;
 private EditText number2 = null;
 private Button resultButton = null;
 private TextView resultTextView = null;
 private RadioGroup mRadioGroup = null;
 private RadioButton jiaRadioButton,jianRadioButton,chengRadioButton,chuRadioButton;
 private String yunsuan;
 
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        number1 = (EditText)findViewById(R.id.number1);
        number2 = (EditText)findViewById(R.id.number2);
        resultButton = (Button)findViewById(R.id.resultButton);
        mRadioGroup = (RadioGroup)findViewById(R.id.RadioGroup_choice);
        jiaRadioButton = (RadioButton)findViewById(R.id.add_RadioButton);
        jianRadioButton = (RadioButton)findViewById(R.id.jian_RadioButton);
        chengRadioButton = (RadioButton)findViewById(R.id.cheng_RadioButton);
        chuRadioButton = (RadioButton)findViewById(R.id.chu_RadioButton);
        resultTextView = (TextView)findViewById(R.id.resultTextView);
       
        mRadioGroup.setOnCheckedChangeListener(new OnCheckedChangeListener(){

public void onCheckedChanged(RadioGroup arg0, int arg1) {
    // TODO Auto-generated method stub
    if(arg1 == jiaRadioButton.getId()){
     yunsuan = "+";
    }
    else if(arg1 == jianRadioButton.getId()){
     yunsuan = "-";
    }
    else if(arg1 == chengRadioButton.getId()){
     yunsuan = "*";
    }
    else if(arg1 == chuRadioButton.getId()){
     yunsuan = "/";
    }
   }
         
        });
       
        resultButton.setOnClickListener(new OnClickListener(){

public void onClick(View arg0) {
    
    // TODO Auto-generated method stub
    String num1 = number1.getText().toString();
    String num2 = number2.getText().toString();
    final double dnum1 = Double.parseDouble(num1);
    final double dnum2 = Double.parseDouble(num2);
    
    double resultNum = 0.0;
    
    if(yunsuan.equals("+")){
     resultNum = dnum1 + dnum2;
    }
    else if(yunsuan.equals("-")){
     resultNum = dnum1 - dnum2;
    }
    else if(yunsuan.equals("*")){
     resultNum = dnum1 * dnum2;
    }
    else if(yunsuan.equals("/")){
     resultNum = dnum1 / dnum2;
    } 
    String resultString = String.valueOf(resultNum);
    resultTextView.setText(resultString);
   } 
        });
    }
}

linux

Android的常用控件总结

Android线程使用注意问题

相关资讯       Android教程 

   

本文评论   查看全部评论 (0)


评论声明

尊重网上道德,遵守中华人民共和国的各项有关法律法规

承担一切因您的行为而直接或间接导致的民事或刑事法律责任

本站管理人员有权保留或删除其管辖留言中的任意内容

本站有权在网站内转载或引用您的评论

参与本评论即表明您已经阅读并接受上述条款

 

 

 

最新资讯

内容版权声明:除非注明,否则皆为本站原创文章。

转载注明出处:https://www.heiqu.com/wwjwzp.html