安卓簡單加法計算器

界面上有兩個輸入框,默認值爲0,一個文本輸出框,一個按鈕;輸入數字後點擊Button計算;
xml中輸入框有個屬性android:inputType=”number”記得注意下。
代碼如下:

private EditText input1;
private EditText input2;
private TextView textView;
int num = 0;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    input1 = (EditText)findViewById(R.id.shurukuan);
    input2 = (EditText)findViewById(R.id.shurukuan2);
    textView = (TextView)findViewById(R.id.textView2);

    Button Btn1 = (Button)findViewById(R.id.button3);
    Btn1.setOnClickListener(new Button.OnClickListener(){
        public void onClick(View v) {
            num= Integer.parseInt(input1.getText().toString()) + Integer.parseInt(input2.getText().toString());
            textView.setText(Integer.toString(num));

        }
    });
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章