Sunteți pe pagina 1din 10

Ministerul Educaiei al Republicii Moldova Universitatea Tehnica a Moldovei Facultatea Calculatoare Informatica si Microelectronica

Lucrare de laborator Nr.1 La Programarea Aplicaiilor Incorporate i Independente De Platform.


Tema: Mediul de dezvoltare Android.

A efectuat: A verificat:

Taca Nicolae gr. SI-111 Antohii Ion

Chiinau 2013

Program 1. Am creat o aplicatie cu doua butoane Ok i Cancel. Odat ce le apsm se afieaza la ecran toxtul corespunztor ce butoon am selectat. Main.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" tools:context=".MainActivity" > <TextView android:id="@+id/mytext" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginTop="36dp" android:text="@string/myText" /> <Button android:id="@+id/myBtn" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_below="@+id/mytext" android:layout_centerHorizontal="true" android:layout_marginTop="102dp" android:text="@string/myBtn" /> </RelativeLayout>

MainActivity.java
package com.example.poo91_oneclickbutton; import import import import import import android.app.Activity; android.os.Bundle; android.view.View; android.view.View.OnClickListener; android.widget.Button; android.widget.TextView;

public class MainActivity extends Activity { TextView tvOut; Button btnOk; Button btnCancel; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout. main); //gasim elementele view tvOut=(TextView) findViewById(R.id.tvOut); btnOk=(Button) findViewById(R.id.btnOk); btnCancel=(Button) findViewById(R.id. btnCancel); tvOut.setText("initializare"); //buton de apasare OnClickListener oclBtnOk = new OnClickListener(){ @Override public void onClick(View v) { //schimbam textul in TextView (tvOut) tvOut.setText("este apasat butonul OK"); } };

btnOk.setOnClickListener(oclBtnOk); OnClickListener oclBtnCancel = new OnClickListener(){ @Override public void onClick(View v) { //schimbam textul in TextView (tvOut) tvOut.setText("este apasat butonul Cancel");} }; btnCancel.setOnClickListener(oclBtnCancel); } }

Program 2. Am creat o aplicaie ContextMeniu unde se initializeaza toua texte la apsarea carora ape un meniu si alegem marimea textului sau culoare tehtului. Main.xml
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent"> <TextView android:layout_height="wrap_content" android:textSize="26sp" android:layout_width="wrap_content" android:id="@+id/tvColor" android:layout_marginBottom="50dp" android:layout_marginTop="50dp" android:text="@string/TheColor"> </TextView> <TextView android:layout_width="fill_parent" android:layout_height="wrap_content" android:textSize="22sp" android:id="@+id/tvSize" android:text="@string/TextSize"> </TextView> </LinearLayout>

MainActiviry.java
package com.example.p0151_contextmenu; import android.app.Activity; import android.graphics.Color; import android.os.Bundle;

import import import import import

android.view.ContextMenu; android.view.ContextMenu.ContextMenuInfo; android.view.MenuItem; android.view.View; android.widget.TextView;

public class MainActivity extends Activity { final int MENU_COLOR_RED = 1; final int MENU_COLOR_GREEN = 2; final int MENU_COLOR_BLUE = 3; final int MENU_SIZE_22 = 4; final int MENU_SIZE_26 = 5; final int MENU_SIZE_30 = 6; TextView tvColor, tvSize; /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); tvColor = (TextView) findViewById(R.id. tvColor); tvSize = (TextView) findViewById(R.id. tvSize); // pentru tvColor si tvSize registerForContextMenu(tvColor); registerForContextMenu(tvSize); } @Override public void onCreateContextMenu(ContextMenu menu, View v, ContextMenuInfo menuInfo) { // TODO Auto-generated method stub switch (v.getId()) { case R.id.tvColor: menu.add(0, MENU_COLOR_RED, 0, "Red"); menu.add(0, MENU_COLOR_GREEN, 0, "Green"); menu.add(0, MENU_COLOR_BLUE, 0, "Blue"); break; case R.id.tvSize: menu.add(0, MENU_SIZE_22, 0, "22"); menu.add(0, MENU_SIZE_26, 0, "26"); menu.add(0, MENU_SIZE_30, 0, "30"); break; } } @Override public boolean onContextItemSelected(MenuItem item) { // TODO Auto-generated method stub switch (item.getItemId()) { case MENU_COLOR_RED: tvColor.setTextColor(Color.RED); tvColor.setText("Text color = red"); break; case MENU_COLOR_GREEN: tvColor.setTextColor(Color.GREEN); tvColor.setText("Text color = green"); break; case MENU_COLOR_BLUE: tvColor.setTextColor(Color.BLUE);

tvColor.setText("Text color = blue"); break; case MENU_SIZE_22: tvSize.setTextSize(22); tvSize.setText("Text size = 22"); break; case MENU_SIZE_26: tvSize.setTextSize(26); tvSize.setText("Text size = 26"); break; case MENU_SIZE_30: tvSize.setTextSize(30); tvSize.setText("Text size = 30"); break; } return super.onContextItemSelected(item); } }

Program 3. La atingerea ecranului se iniializeaza un contor ce numara atingerele si le afieaza intr-un textview pe ecran,program este utilizat pentru a face flotari. Main.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:id="@+id/asd" android:layout_width="match_parent" android:layout_height="match_parent" tools:context=".MainActivity" > <TextView android:id="@+id/tvContor" android:layout_width="fill_parent" android:layout_marginTop="102dp" android:gravity="center" android:text="" android:textSize="40sp" /> <Button android:id="@+id/Reset" android:layout_width="wrap_content"

android:layout_height="wrap_content" android:layout_marginTop="44dp" android:text="@string/Reset" /> </RelativeLayout>

MainActivity.java
import import import import import import android.app.Activity; android.os.Bundle; android.view.Menu; android.view.View; android.widget.Button; android.widget.TextView;

public class MainActivity extends Activity { int j=0;TextView textOut; Button Res; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); View s=(View) findViewById(R.id.asd); textOut=(TextView) findViewById(R.id. tvContor); Res=(Button) findViewById(R.id. Reset); s.setOnClickListener(new View.OnClickListener() {

@Override public void onClick(View arg0) { j++; String s = Integer.toString(j); textOut.setText(s);

} }); Res.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View arg0) { j=0; textOut.setText("");

@Override public boolean onCreateOptionsMenu(Menu menu) { // Inflate the menu; this adds items to the action bar if it is present. getMenuInflater().inflate(R.menu.main, menu); return true; }

Program 4. Calculator Main.xml


<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent"> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:id="@+id/linearLayout1" android:layout_marginLeft="10pt" android:layout_marginRight="10pt" android:layout_marginTop="3pt"> <EditText android:layout_weight="1" android:layout_height="wrap_content" android:layout_marginRight="5pt" android:id="@+id/etNum1" android:layout_width="match_parent" android:inputType="numberDecimal"> </EditText> <EditText android:layout_height="wrap_content" android:layout_weight="1" android:layout_marginLeft="5pt" android:id="@+id/etNum2" android:layout_width="match_parent" android:inputType="numberDecimal"> </EditText> </LinearLayout> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:id="@+id/linearLayout2" android:layout_marginTop="3pt" android:layout_marginLeft="5pt" android:layout_marginRight="5pt"> <Button android:layout_height="wrap_content" android:layout_width="match_parent" android:layout_weight="1" android:text="@string/+" android:textSize="8pt" android:id="@+id/btnAdd"> </Button> <Button android:layout_height="wrap_content" android:layout_width="match_parent" android:layout_weight="1" android:text="@string/-" android:textSize="8pt" android:id="@+id/btnSub"> </Button> <Button android:layout_height="wrap_content" android:layout_width="match_parent" android:layout_weight="1" android:text="@string/*" android:textSize="8pt" android:id="@+id/btnMult">

</Button> <Button android:layout_height="wrap_content" android:layout_width="match_parent" android:layout_weight="1" android:text="@string//" android:textSize="8pt" android:id="@+id/btnDiv"> </Button> </LinearLayout> <TextView android:layout_height="wrap_content" android:layout_width="match_parent" android:textSize="12pt" android:layout_marginTop="3pt" android:id="@+id/tvResult" android:gravity="center_horizontal"> </TextView> </LinearLayout>

MainActivity.java
import import import import import import import import import import android.app.Activity; android.os.Bundle; android.text.TextUtils; android.view.Menu; android.view.MenuItem; android.view.View; android.view.View.OnClickListener; android.widget.Button; android.widget.EditText; android.widget.TextView;

public class MainActivity extends Activity implements OnClickListener { final int MENU_RESET_ID = 1; final int MENU_QUIT_ID = 2; EditText etNum1; EditText etNum2; Button Button Button Button btnAdd; btnSub; btnMult; btnDiv;

TextView tvResult; String oper = ""; /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout. main); // gasim elementele etNum1 = (EditText) findViewById(R.id. etNum1); etNum2 = (EditText) findViewById(R.id.etNum2); btnAdd = (Button) findViewById(R.id. btnAdd); btnSub = (Button) findViewById(R.id. btnSub); btnMult = (Button) findViewById(R.id.btnMult); btnDiv = (Button) findViewById(R.id. btnDiv);

tvResult = (TextView) findViewById(R.id.tvResult);

btnAdd.setOnClickListener(this); btnSub.setOnClickListener(this); btnMult.setOnClickListener(this); btnDiv.setOnClickListener(this); } @Override public void onClick(View v) { // TODO Auto-generated method stub float num1 = 0; float num2 = 0; float result = 0;

if (TextUtils.isEmpty(etNum1.getText().toString()) || TextUtils.isEmpty(etNum2.getText().toString())) { return; } // edittext le facem te tip string num1 = Float.parseFloat(etNum1.getText().toString()); num2 = Float.parseFloat(etNum2.getText().toString()); // setam butoanele switch (v.getId()) { case R.id.btnAdd: oper = "+"; result = num1 + num2; break; case R.id.btnSub: oper = "-"; result = num1 - num2; break; case R.id.btnMult: oper = "*"; result = num1 * num2; break; case R.id.btnDiv: oper = "/"; result = num1 / num2; break; default: break; }

tvResult.setText(num1 + " " + oper + " " + num2 + " = " + result); } @Override public boolean onCreateOptionsMenu(Menu menu) { // TODO Auto-generated method stub menu.add(0, MENU_RESET_ID, 0, "Reset"); menu.add(0, MENU_QUIT_ID, 0, "Quit"); return super.onCreateOptionsMenu(menu); }

@Override public boolean onOptionsItemSelected(MenuItem item) { // TODO Auto-generated method stub

switch (item.getItemId()) { case MENU_RESET_ID: etNum1.setText(""); etNum2.setText(""); tvResult.setText(""); break; case MENU_QUIT_ID: finish(); break; } return super.onOptionsItemSelected(item); } }

Concluzie: n lucrarea dat de laborator am facut cunotina cu mediul de dezvoltare Android . Am distins elementele de baz precum i lucrul i interaciunea intre ele.