Code chức năng File Mainactyvity.java package com.example.maytinhll 12; import androidx.annotation.NonNull; import androidx.appcompat.app.AlertDialog; import androidx.appcompat.app.AppCo
Trang 1
oe So SK OSS OO
BO GIAO DUC VA DAO TAO
Trường Đại Học Duy Tân
ĐỎ ÁN CÁ NHÂN
LẬP TRÌNH ỨNG DỤNG CHO CÁC THIẾT BỊ DI ĐỘNG
ĐÈ TÀI : Ứng Dụng Quản Lý Ghi Chú
|
Ậ
Lớp : CR 424 AM Giang Vién : Pham An Binh
Sinh viên thực hiện : Bùi Văn Bình
Mã Sinh Viên : 27211240478
——————=S®— *-=>« >*†ˆ®cC—— ©=———————
Trang 2Contents
1 Thigt ké giao GiGi cece cccccccsssssssssessecseesecssesscsscsscsscascessesecseessescaueacsuescaneseeseseesesees 4
1.1 Giao diện chính của ứng dụng - «+ ng nh ng ng re 4 1.2 Giao diện Lich st phép tính 5 sọ ng rưx 4
1.3 Giao diện bấm giờ 5-52 Sc St SE HT 21211211211211 111111111 trree 5
2 Giao diện thực tế của app ¿©2552 SxtSt2x SE 2 E211 rrrrre 5
3 Toam bO Coden 7 3.1 Code chức nang cece ccececseceseceseeessessesesesessesseeseseessesasaeseseaeeeeeeeeeees 7 Eile MainaCfyVify.]24VA HH HH HH TT 7 3.2 Code Giao đien «SH HT TH HT HT HT 15 File menu _ đ€1m10.XIHÌ - - <5 <5 <5 1 E1 TH HH ng tre 15 File actfvity _ baim10.XIHỈ -Ú 7-5 55 5 <0 TH TH ng re 15 I3) 5i.) 59 1 17 File phep tin 09 0n “17D 17
Trang 31 Thiết kế giao diện
1.1 Giao diện chính của ung dung
Máy tính
phép tinh
|]
lịch sữ
1.2 Giao diện Lịch sữ phép tính
lich str
phép tinh lich st
Trang 4
1.3 Giao dién bam giờ
bam gid’
0:00:000
2 Giao diện thực tế của app
PHÉP TÍNH HISTORY
MÁY TÍNH
Trang 5
Máy Tính
9X6=54 9X6=54 —
0:00:757 0:01:124 0:01:492 0:01:842 0:02:177 0:02:527 0:02:876 0:03:265 0:03:775
Trang 6
3 Toàn bộ Code
3.1 Code chức năng
File Mainactyvity.java
package com.example.maytinhll 12;
import androidx.annotation.NonNull;
import androidx.appcompat.app.AlertDialog;
import androidx.appcompat.app.AppCompatActivity; import androidx.appcompat.widget.Toolbar;
import android.content.DialogIinterface;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android view.MenulInflater;
import android view.Menultem;
import android view View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget Button;
import android.widget ListView;
import android.widget.TabHost;
import android.widget.TextView;
import android.widget.Toast;
import java.util.Arraylist;
public class MainActivity extends AppCompatActivity { Integer sol =0;
Integer so2 =0;
Character dau = null;
Button btnthoat;
ListView lvHistory;
ArrayList<String>dskq;
ArrayAdapter<String>adapterkqne;
@Override
protected void onCreate(Bundle savedInstanceState) { super.onCreate (savedInstanceState) ;
setContentView (R.layout.activity main); addControls();
addEvents ();
ControlButton();
Toolbar toolbar = findViewByld(R.id toolbar); setSupportActionBar (toolbar);
setTitle ("May tinh");
}
Trang 7btnthoat.setOnClickListener (new View.OnClickListener() {
@Override
public void onClick(View v) {
AlertDialog.Builder builder = new
AlertDialog.Builder(MainActivity.this,
android.R.style Theme DeviceDefault Light Dialog);
builder.setTitle("Bạn có muốn thoát khỏi ứng dung ?"); builder.setMessage ("Hay lua chọn bên dưới để xác nhận") ; builder.seticon(android.R.drawable.ic dialog alert); builder setPositiveButton("Cé6", new
DialogTnterface.OnClickTistener() {
@Override public void onClick(Dialoginterface dialog, int which) { onBackPressed();
}
})z
builder.setNegativeButton("Khéng", new
DialogTnterface.OnClickTistener() {
@Override public void onClick(Dialoginterface dialog, int which) { }
})z
builder show ();
1);
}
private void addEvents() {
final TextView txtvkq = (TextView) findViewById(R.id.kq);
Button Sod =(Button) findViewByld(R.id.so0);So0.setOnClickListener (new View.OnClickListener() {@Override public void onClick(View v)
{ txtvkq.setText (txtvkq.getText ().toString()+"0");if(dau == null) {sel = Integer.parselInt (txtvkq.getText().toString());} else {so2 =
Integer.parselInt (txtvkq.getText().toString());} 3h);
Button Sol =(Button) findViewByld(R.id.sol);Sol.setOnClickListener (new View.OnClickListener() {@Override public void onClick(View v)
{ txtvkq.setText (txtvkq.getText ().toString()+"1");if(dau == null) {sol = Integer.parselInt (txtvkq.getText().toString());} else {so2 =
Integer.parselInt (txtvkq.getText().toString());} }1)z
Button So2 =(Button) findViewByld(R.id.so2);So2.setOnClickListener (new View.OnClickListener() {@Override public void onClick(View v)
{ txtvkq.setText (txtvkq.getText ().toString()+"2");if(dau == null) {sel = Integer.parselInt (txtvkq.getText().toString());} else {so2 =
Integer.parselInt (txtvkq.getText().toString());} }1)z
Button So3 =(Button) findViewByld(R.id.so3);So3.setOnClickListener (new View.OnClickListener() {@Override public void onClick(View v)
{ txtvkq.setText (txtvkq.getText ().toString()+"3");if(dau == null) {sel = Integer.parselInt (txtvkq.getText().toString());} else {so2 =
Integer.parselInt (txtvkq.getText().toString());} 3h);
Button So4 =(Button) findViewByld(R.id.so4);So4.setOnClickListener (new View.OnClickListener() {@Override public void onClick(View v)
{ txtvkq.setText (txtvkq.getText ().toString()+"4");if(dau == null) {sel = Integer.parselInt (txtvkq.getText().toString());} else {so2 =
Integer.parselInt (txtvkq.getText().toString());} 3h);
Trang 8{ txtvkq.setText (txtvkq.getText ().toString()+"5");if(dau == null) {sel = Integer.parselInt (txtvkq.getText().toString());} else {so2 =
Integer.parselInt (txtvkq.getText().toString());} 3h);
Button So6 =(Button) findViewByld(R.id.so6);So6.setOnClickListener (new View.OnClickListener() {@Override public void onClick(View v)
{ txtvkq.setText (txtvkq.getText ().toString()+"6");if(dau == null) {sel = Integer.parselInt (txtvkq.getText().toString());} else {so2 =
Integer.parselInt (txtvkq.getText().toString());} }1)z
Button So7 =(Button) findViewByld(R.id.so7);So7.setOnClickListener (new View.OnClickListener() {@Override public void onClick(View v)
{ txtvkq.setText (txtvkq.getText ().toString()+"7");if (dau == null) {sel = Integer.parselInt (txtvkq.getText().toString());} else {so2 =
Integer.parselInt (txtvkq.getText().toString());} }1)z
Button So8 =(Button) findViewByld(R.id.so8);So8.setOnClickListener (new View.OnClickListener() {@Override public void onClick(View v)
{ txtvkq.setText (txtvkq.getText ().toString()+"8");if(dau == null) {sel = Integer.parselInt (txtvkq.getText().toString());} else {so2 =
Integer.parselInt (txtvkq.getText().toString());} }1)z
Button So9 =(Button) findViewByld(R.id.so9);So9.setOnClickListener (new View.OnClickListener() {@Override public void onClick(View v)
{ txtvkq.setText (txtvkq.getText ().toString()+"9");if(dau == null) {sel = Integer.parselInt (txtvkq.getText().toString());} else {so2 =
Integer.parselInt (txtvkq.getText().toString());} }1)z
Button Bo = (Button) findViewById(R.id.bo);
Bo.setOnClickListener (new View.OnClickListener({) {
@Override
public void onClick(View v) {
so2 =0;
txtvkq.setText ("™");
1);
Button Cong =(Button) findViewByld(R.id.cong);
Cong.setOnClickListener (new View.OnClickListener() {
@Override
public void onClick(View v) {
đau = '+';
txtvkq.setText("");
1);
Button Tru =(Button) findViewByld(R.id.tru);
Tru setOnClickListener (new View.OnClickListener{) {
@Override
public void onClick(View v) {
dau = '-';
txtvkq.setText ("™");
1);
Button Nhan =(Button) findViewByld(R.id.nhan);
Nhan.setOnClickListener (new View.OnClickListener() {
@Override
Trang 9dau = 'X!;
txtvkq.setText("");
1);
Button Chia =(Button) findViewByld(R.id.chia);
Chia.setOnClickTistener (new View.OnClickTistener() {
@Override
public void onClick(View v) {
dau = '/';
txtvkq.setText ("™");
1);
Button Bang =(Button) findViewByld(R.id.bang);
Bang setOnClickListener (new View.OnClickListener() {
@Override
public void onClick(View v) {
if (dau!=null)
{
Integer kqne =0;
switch (dau) {
case '+';:({
kqne = sol + so2;
break;
case '-':rf{
kqne = sol - so2;
break;
} case 'X':{
kqne = sol * so2;
break;
} case '/':{
kqne = sol / so2;
break;
} } txtvkq.setText (kqne.toString());
dskq.add(Integer.toString(sol) +" "+ dau+" "+
Integer.toString(so2) +" =" + String.valueOf(kqne));
sol = kqne;
dau = null;
adapterkqne.notifyDataSetChanged({);
1);
Trang 10@Override
public void onClick(View v) {
sol =0;
so2 =0;
dau =null;
txtvkq.setText ("™");
private void addControls() {
TabHost tabHost= (TabHost) findViewByld(R.id.tabHost);
tabHost.setup();
TabHost.TabSpec tabl=tabHost.newTabSpec("t1");
tabl.setindicator("Phép Tinh");
tabl.setContent (R.id.tabl);
tabHost.addTab(tabl);
TabHost.TabSpec tab2=tabHost.newTabSpec("t2");
tab2.setindicator ("History");
tab2.setContent (R.id.tab2);
tabHost.addTab (tab2);
lvHistory=(ListView) findViewByld(R.id.lvHistory);
dskq=new ArrayList<>();
adapterkqne= new ArrayAdapter<String> (
MainActivity.this,
android.R.layout.simple list item 1,
dskq);
lvHistory.setAdapter (adapterkqne);
}
public boolean onCreateOptionsMenu (Menu menu) {
// Inflate menu tu tép tin XML
MenuTnflater inflater = getMenulInflater();
inflater.inflate(R.menu.menu_demo, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(@NonNull Menultem item) {
int id = item getItemId();
if (id == R.id.menu_calculator) {
Toast.makeText (this, "day là phần máy tinh ",
Toast.LENGTH SHORT).show();
return true;
} else if (id == R.id.menu_timer) {
⁄/ Bắt đầu hoạt động bamgio
Toast.makeText (this, "đã chuyển đến phần bắm giờ "
r
10
Trang 11try {
Intent timerIntent = new Intent(this, bamgio.class);
startActivity(timerIntent);
return true;
} catch (Exception e) { - - -
Log.e("MenuSelection", "Tôi khi bắt dau hoat déng Timer", e); return false;
}
} else {
return super.onOptionsitemSelected (item);
}
File bamgiojava
package com.example.maytinhll 12;
import
import
import
import
import
import
import
import
import
import
import
import
import
import
import
public
androidx.annotation.NonNull;
androidx.appcompat.app.AppCompatActivity;
androidx.appcompat.widget.Toolbar;
android content.Intent;
android.os.Bundle;
android.os.Handler;
android.os.SystemClock;
android.util.Log;
android view.Menu;
android view.MenulInflater;
android view.Menultem;
android view View;
android.widget.ArrayAdapter;
android.widget Button;
android.widget.ListView;
android.widget.TextView;
android.widget.Toast;
java.util.ArrayList;
class bamgio extends AppCompatActivity {
Button btnStop, btnStart, btnPause, btnCount;
TextView txtTimer;
ListView lvCount;
long lStartTime, lPauseTime, lSystemTime = OL;
boolean isRun;
Handler handler = new Handler();
ArrayList<String> timeList = new ArrayList<>({);
ArrayAdapter<String> adapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
Super.onCreate (savedTnstanceState);
setContentView (R.layout.activity bamgio);
11
Trang 12btnStep = findViewById(R.id.btnStop);
btnPause = findViewByld(R.id.btnPause);
txtTimer = findViewByld(R.id.txtTimer) ;
btnCount = findViewByld(R.id.btnCount);
lvCount = findViewByld(R.id.ivCount);
clickStart
clickStop (
clickPause
elickCount
();
)z
();
()z ,
// Khoi tao adapter cho ListView
adapter = new ArrayAdapter<>(this,
android.R.layout.simple list iEem 1, Cimelist);
lvCount.setAdapter (adapter);
Toolbar toolbar = findViewByld(R.id.toolbar2);
setSupportActionBar (toolbar);
setTitle ("Bam giờ");
}
void clickStart() {
btnStart.setOnClickListener (new View.OnClickListener() {
@Override
public void onClick(View v) {
1£ (isRun)
return;
isRun = true;
lStartTime = SystemClock.uptimeMillis() - 1lPauseTime; // Cập nhật LStartTime
handler.postDelayed(runnable, 0);
}
1);
}
void clickStop() {
btnStop.setOnClickListener (new View.OnClickListener() {
@Override
public void onClick(View v) {
isRun = false;
lStartTime = 0;
l1PauseTime = 0;
lSystemTime = 0;
handler removeCallbacks (runnable);
txtTimer.setText("0:00:000");
// Lam moi ListView
timeList.clear();
adapter.notifyDataSetChanged();
void clickPause() {
btnPause.setOnClickListener (new View.OnClickListener() {
@Override
12
Trang 131£ (!isRun)
return;
isRun false;
lPauseTime = SystemClock.uptimeMillis() - 1StartTime; // C&ap nhật 1PauseTime
handler removeCallbacks (runnable);
}
1);
}
void clickCount() {
btnCount.setOnClickListener (new View.OnClickListener()
@Override
public void onClick(View v) {
1£ (isRun) {
{
String currentTime = txtTimer.getText().toString(); timeList.add(currentTime) ;
adapter.notifyDataSetChanged();
1);
}
Runnable runnable = new Runnable() {
@Override
public void run({) {
if (isRun) {
ilSystemTime = SystemClock.uptimeMillis() - 1lStartTime; long lUpdateTime lPauseTime + 1SystemTime;
long secs = (long) (lUpdateTime / 1000);
long mins = secs / 60;
secs secs % 60;
long milliseconds = (long) (lUpdateTime % 1000);
txtTimer.setText("" + mins + ":" + String format("%$02d", secs) + "i" + String format ("303d", milliseconds) ) ))z
handler.postDelayed (this, 0);
}
}
};
public boolean onCreateOptionsMenu (Menu menu) {
// Inflate menu tu tép tin XML
MenuTnflater inflater = getMenulInflater();
inflater.inflate(R.menu.menu_demo, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(@NonNull Menultem item) {
int id = item getItemId();
if (id == R.id.menu_timer) {
Toast.makeText (this, "day 1A phan bấm giờ",
Toast LENGTH SHORT) show();
return true;
} else if (id == R.id.menu_calculator) {
⁄/ Bắt đầu hoạt động bamgio
13
Trang 14Toast.makeText (this, "da chuyén dén phần máy tinh ",
Toast LENGTH SHORT) show();
try {
Intent timerIntent = new Intent(this, MainActivity.class); startActivity(timerIntent);
return true;
} catch (Exception e) {
Log.e("MenuSelection", "Lỗi khi bắt đầu hoạt động Timer", e);
return false;
}
} else {
return
} super onOptionsItemSelected (item) ;
3.2 Code Giao dien
4 <?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android: layout width="match parent"
android:layout height="match parent"
tools:context=".MainActivity"
android: orientation="vertical">
<androidx.appcompat.widget.Toolbar
android:
android:
android:
android:
:minHeight="?attr/actionBarSize"
android
id="@+id/toolbar"
layout_width="match_parent"
layout_height="wrap content"
background="?attr/colorPrimary"™
android:theme="?attr/actionBarTheme" />
<TabHost
android: layout _width="match_ parent"
android: layout _height="match_ parent"
android:id="@+id/tabHost"™>
<LinearLayout
android: layout _width="match_ parent"
android: layout _height="match_ parent"
android: orientation="vertical">
<TabWidget
android: id="@android:id/tabs"
android: layout_width="match_ parent"
android:layout_height="wrap content" />
<FrameLayout
android:id="@android:id/tabcontent"
android: layout_width="match_ parent"
android:layout_height="match parent">
<LinearLayout
android: id="@+tid/tabl1"
android: layout _width="match_ parent"
14