04.06.2008 | 22:12:22
immagine
Ing. Emanuele Scapin

Android, es1: Elementare applicazione con due finestre e pochi oggetti (LinearLayout).
  • Main Activity con tre bottoni, il bottone copia per copiare il testo inserito nel primo EditText nel secondo, il secondo bottone per passare ad altra finestra (activity secondaria), il terzo bottone chiude l'applicazione

     

  • Definizione layout prima activity.

    <?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"
        android:orientation="vertical" android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:weightSum="1">
    
        <TextView
            android:id="@+id/textView"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="Stringa da copiare" />
    
        <EditText
            android:id="@+id/editText"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:ems="10"
            android:inputType="textPersonName" />
    
        <Button
            android:id="@+id/button2"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="Copia" />
    
        <TextView
            android:id="@+id/textView2"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="Copia della stringa" />
    
        <EditText
            android:id="@+id/editText2"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:ems="10"
            android:inputType="textPersonName" />
    
        <ImageView
            android:id="@+id/imageView2"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            app:srcCompat="@mipmap/ic_launcher"
            android:layout_weight="0.72" />
    
        <Button
            android:id="@+id/avanti"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="Avanti" />
    
        <Button
            android:id="@+id/fine"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="Fine" />
    <LinearLayout>
            
  • Codice della prima activity.

    package com.example.emanuele.prova001;
    
    import android.content.Intent;
    import android.support.v7.app.AppCompatActivity;
    import android.os.Bundle;
    import android.view.View;
    import android.widget.Button;
    import android.widget.EditText;
    
    public class MainActivity extends AppCompatActivity {
    
        private View.OnClickListener b2click = new View.OnClickListener() {
            public void onClick(View v) {
                EditText e1 = (EditText) findViewById(R.id.editText);
                EditText e2 = (EditText) findViewById(R.id.editText2);
                String s = e1.getText().toString();
                e2.setText(s);
            }
        };
    
        private View.OnClickListener avanticlick = new View.OnClickListener() {
            public void onClick(View v) {
                Intent myIntent = new Intent(v.getContext(), second.class);
                startActivity(myIntent);
            }
        };
    
        private View.OnClickListener fineclick = new View.OnClickListener() {
            public void onClick(View v) {
                System.exit(0);
            }
        };
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.new_main);
    
            Button bcopia = (Button) findViewById(R.id.copia);
            bcopia.setOnClickListener(b2click);
            Button bavanti = (Button) findViewById(R.id.avanti);
            bavanti.setOnClickListener(avanticlick);
            Button bfine = (Button) findViewById(R.id.fine);
            bfine.setOnClickListener(fineclick);
        }
    }
            
  • Definizione layout seconda activity, con bottone per tornare alla precedente.

    <?xml version="1.0" encoding="utf-8"?>
    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:app="http://schemas.android.com/apk/res-auto"
        android:layout_width="match_parent" android:layout_height="match_parent">
    
        <TextView
            android:id="@+id/lblsecond"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginTop="24dp"
            android:text="Secondo layout e activity"
            android:textSize="24sp"
            android:layout_alignParentTop="true"
            android:layout_centerHorizontal="true" />
    
        <ImageView
            android:id="@+id/imageView3"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_below="@+id/lblsecond"
            android:layout_centerHorizontal="true"
            android:layout_marginTop="47dp"
            app:srcCompat="@mipmap/ic_launcher" />
    
        <Button
            android:id="@+id/Cambia"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_below="@+id/lblsecond"
            android:layout_centerHorizontal="true"
            android:layout_marginTop="20dp"
            android:text="Cambia immagine" />
    
        <Button
            android:id="@+id/torna"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentBottom="true"
            android:layout_centerHorizontal="true"
            android:layout_marginBottom="38dp"
            android:text="Torna" />
    
    <RelativeLayout>
            
  • Codice della seconda activity.

    package com.example.emanuele.prova001;
    
    import android.os.Bundle;
    import android.support.v7.app.AppCompatActivity;
    
    public class second extends AppCompatActivity {
    
        private View.OnClickListener tornaclick = new View.OnClickListener() {
            public void onClick(View v) {
            finish();
            }
        };
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_second);
    
            Button b = (Button) findViewById(R.id.torna);
            b.setOnClickListener(tornaclick);
        }
    
    }        
            

  • Gestione di semplice Toast, con tempo di visualizzazione lungo.

        private View.OnClickListener cambiaclick = new View.OnClickListener() {
            public void onClick(View v) {
                Toast.makeText(v.getContext(), "Elenco immagini vuoto",
                               Toast.LENGTH_LONG).show();
            }
        };
            
  • Gestione di un Toast con specifico layout.

        private View.OnClickListener cambiaclick = new View.OnClickListener() {
            public void onClick(View v) {
                LayoutInflater mInflater=LayoutInflater.from(v.getContext());
                View view=mInflater.inflate(R.layout.toast1, null);
                Toast toast=new Toast(v.getContext());
                view.setBackgroundResource(R.color.colorPrimaryDark);
                toast.setView(view);
                toast.setDuration(Toast.LENGTH_LONG);
                toast.show();
            }
        };
            

  • Gestione cambio immagine su oggetto ImageView.

    ImageView image;
    
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_second);
    
        // immagine
        image = (ImageView) findViewById(R.id.imageView3);
    }
    
    private View.OnClickListener cambiaclick = new View.OnClickListener() {
        public void onClick(View v) {
            // cambio immagine
            //image.setBackgroundResource(R.drawable.ic_localobidos);
            image.setImageResource(R.drawable.ic_localobidos);
        }
    };
            

© Emanuele Scapin 2009-2018

 

Valid HTML 4.01! Valid CSS!