Działanie na obiektach

Działanie na obiektach

Program: wypisujący dane wprowadzone przez użytkownika w postaci obiektów.

Jak w opisie.

Kompilator: Eclipse

Film:

Kod programu:

Główna klasa – backend:

package com.example.project3_wlasne;

import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

public class MainActivity extends Activity {

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		
		Button bZapisz=(Button)findViewById(R.id.buttonZapisz);
		final EditText eImie=(EditText)findViewById(R.id.editTextImie);
		final EditText eNazwisko=(EditText)findViewById(R.id.editTextNazwisko);
		
		bZapisz.setOnClickListener(new View.OnClickListener() {		
			@Override
			public void onClick(View v) {
				Pracownik pracownik = new Pracownik();
				pracownik.imie=eImie.getText().toString();
				pracownik.nazwisko=eNazwisko.getText().toString();
				Toast.makeText(getApplicationContext(),"Zalogowano jako "+pracownik.imie+" "
                                +pracownik.nazwisko, Toast.LENGTH_LONG).show();
			}
		});	
	}
	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		getMenuInflater().inflate(R.menu.main, menu);
		return true;
	}
}

Klasa – pracownicy:

package com.example.project3_wlasne;

public class Pracownik {
	
	String imie;
	String nazwisko;

}

Layout:

<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"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context=".MainActivity" >

    <EditText
        android:id="@+id/editTextNazwisko"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="66dp"
        android:ems="10"
        android:hint="@string/nazwisko" >

        <requestFocus />
    </EditText>

    <TextView
        android:id="@+id/textFieldNazwisko"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/editTextNazwisko"
        android:layout_centerHorizontal="true"
        />

    <EditText
        android:id="@+id/editTextImie"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true"
        android:ems="10"
        android:hint="@string/imie" />

    <TextView
        android:id="@+id/textFieldImie"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/editTextImie"
        android:layout_centerHorizontal="true"
        />

    <Button
        android:id="@+id/buttonZapisz"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@+id/textFieldNazwisko"
        android:layout_below="@+id/textFieldNazwisko"
        android:text="@string/zapisz" />

</RelativeLayout>