Mình sẽ hướng dẫn các bạn tự làm máy tính cá nhân với Android
Thông tin của Project:

Project name: MayTinh2
Build Target: Android 2.3.3
Application name: MayTinh2
Package name: com.dac.MayTinh2
Create Activity: MayTinh2

Trong file main.xml:

"1.0" encoding="utf-8"?>
<linearlayout></linearlayout>
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<textview></textview>
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/hello"
/>
<textview android:text="<em">"So 1" android:id="@+id/textView1" android:layout_width="wrap_content" android:layout_height="wrap_content"></textview>
<edittext android:hint="<em">"Nhap vao so 1" android:layout_width="match_parent" android:layout_height="wrap_content" android:id="@+id/edit1" android:inputType="numberDecimal"></edittext>
<requestfocus></requestfocus>
<textview android:text="<em">"So 2" android:id="@+id/textView2" android:layout_width="wrap_content" android:layout_height="wrap_content"></textview>
<edittext android:hint="<em">"Nhap vao so 2" android:layout_width="match_parent" android:layout_height="wrap_content" android:id="@+id/edit2" android:inputType="numberDecimal"></edittext>
<linearlayout android:id="<em">"@+id/linearLayout1" android:layout_height="wrap_content" android:weightSum="1" android:layout_width="match_parent"></linearlayout>
<button android:layout_height="<em">"wrap_content" android:id="@+id/btnCong" android:text="+" android:layout_width="wrap_content" android:layout_weight="0.18"></button>
<button android:layout_height="<em">"wrap_content" android:id="@+id/btnTru" android:text="-" android:layout_width="wrap_content" android:layout_weight="0.16"></button>
<button android:layout_width="<em">"wrap_content" android:layout_height="wrap_content" android:id="@+id/btnNhan" android:text="*" android:layout_weight="0.06"></button>
<button android:layout_width="<em">"wrap_content" android:layout_height="wrap_content" android:id="@+id/btnChia" android:text="/" android:layout_weight="0.06"></button>
<linearlayout android:id="<em">"@+id/linearLayout2" android:layout_width="match_parent" android:layout_height="wrap_content"></linearlayout>
<button android:text="<em">"SQRT" android:id="@+id/btnSqrt" android:layout_width="wrap_content" android:layout_height="wrap_content"></button>
<button android:text="<em">"!" android:id="@+id/btnGiaithua" android:layout_width="wrap_content" android:layout_height="wrap_content"></button>
<button android:text="<em">"Mod" android:id="@+id/btnMod" android:layout_width="wrap_content" android:layout_height="wrap_content"></button>
<button android:text="<em">"Nhi Phan" android:id="@+id/btnNhiPhan2" android:layout_width="wrap_content" android:layout_height="wrap_content"></button>
<textview android:id="<em">"@+id/textView3" android:text="Ket qua la`: " android:layout_width="wrap_content" android:layout_height="wrap_content"></textview>
<edittext android:hint="<em">"KQ se hien o day" android:layout_width="match_parent" android:layout_height="wrap_content" android:id="@+id/editKQ" android:editable="false"></edittext>



Trong file MayTinh2.java :
package com.dac.MayTinh2;

import android.app.Activity;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.View.OnKeyListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

public class MayTinh2 extends Activity {

MayTinh2 m = this;
OnKeyListener o = new OnKeyListener() {

@Override
public boolean onKey(View v, int keyCode, KeyEvent event) {
EditText e1 = (EditText) findViewById(R.id.edit1);
EditText e2 = (EditText) findViewById(R.id.edit2);
if ((event.getAction() == KeyEvent.ACTION_DOWN)
&& (keyCode == KeyEvent.KEYCODE_ENTER)) {
Toast.makeText(m,"So 1 la`: " + e1.getText() + "/n So 2 la`: "+ e2.getText(), Toast.LENGTH_SHORT).show();
return true;
}
// TODO Auto-generated method stub
return false;
}
};

OnClickListener c = new OnClickListener() {

@Override
public void onClick(View v) {

Button btnCong = (Button) findViewById(R.id.btnCong);
Button btnTru = (Button) findViewById(R.id.btnTru);
Button btnNhan = (Button) findViewById(R.id.btnNhan);
Button btnChia = (Button) findViewById(R.id.btnChia);
Button btnSqrt = (Button) findViewById(R.id.btnSqrt);
Button btnGiaThua = (Button) findViewById(R.id.btnGiaithua);
Button btnMod = (Button) findViewById(R.id.btnMod);
Button btnNhiPhan2 = (Button) findViewById(R.id.btnNhiPhan2);
EditText e1 = (EditText) findViewById(R.id.edit1);
EditText e2 = (EditText) findViewById(R.id.edit2);
EditText eKQ = (EditText) findViewById(R.id.editKQ);

Double so1 = (double) 0;
Double so2 = (double) 0;
Double kq;
try {
so1 = Double.parseDouble(e1.getText().toString());
} catch (Exception e) {
so1 = (double) 0;
}
try {
so2 = Double.parseDouble(e2.getText().toString());
} catch (Exception e) {
so2 = (double) 0;
}
if (v.getId() == btnCong.getId()) {
kq = so1 + so2;
eKQ.setText(kq.toString());
}
if (v.getId() == btnTru.getId()) {
kq = so1 - so2;
eKQ.setText(kq.toString());
}
if (v.getId() == btnNhan.getId()) {
kq = so1 * so2;
eKQ.setText(kq.toString());
}
if (v.getId() == btnChia.getId()) {
kq = so1 / so2;
eKQ.setText(kq.toString());
}
if (v.getId() == btnSqrt.getId()) {
kq = Math.sqrt(so1);
eKQ.setText(kq.toString());
}
if (v.getId() == btnGiaThua.getId()) {
kq = (double) 1;
for (int i = 1; i
kq *= i;
}
eKQ.setText(kq.toString());
}
if (v.getId() == btnMod.getId()) {
kq = so1 % so2;
eKQ.setText(kq.toString());
}
if (v.getId() == btnNhiPhan2.getId()) {
String kqStr = "";
int so1int = so1.intValue();
while (so1int != 0) {
kqStr += String.valueOf(so1int % 2);
so1int = so1int / 2;
}
StringBuffer bf = new StringBuffer(kqStr);
eKQ.setText(bf.reverse());
}
}

};

/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main2);
EditText e1 = (EditText) findViewById(R.id.edit1);
EditText e2 = (EditText) findViewById(R.id.edit2);
Button btnCong = (Button) findViewById(R.id.btnCong);
Button btnTru = (Button) findViewById(R.id.btnTru);
Button btnNhan = (Button) findViewById(R.id.btnNhan);
Button btnChia = (Button) findViewById(R.id.btnChia);
Button btnSqrt = (Button) findViewById(R.id.btnSqrt);
Button btnGiaThua = (Button) findViewById(R.id.btnGiaithua);
Button btnMod = (Button) findViewById(R.id.btnMod);
Button btnNhiPhan2 = (Button) findViewById(R.id.btnNhiPhan2);
btnChia.setOnClickListener(c);
btnTru.setOnClickListener(c);
btnNhan.setOnClickListener(c);
btnCong.setOnClickListener(c);
btnSqrt.setOnClickListener(c);
btnMod.setOnClickListener(c);
btnNhiPhan2.setOnClickListener(c);
btnGiaThua.setOnClickListener(c);
e1.setOnKeyListener(o);
e2.setOnKeyListener(o);
}
}

Chủ đề tương tự: