sexta-feira, 3 de julho de 2015

#2 Tutorial Android - Criando duas activitys e passando dados de uma para outra

Fontes

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

    <TextView 
        android: text="@string/lbNome"
        android: layout_width="wrap_content"
        android: layout_height="wrap_content"
        android: id="@+id/textView" />

    <EditText
        android: layout_width="500px"
        android: layout_height="wrap_content"
        android: id="@+id/txtNome"
        android: layout_below="@+id/textView"
        android: layout_alignParentStart="true" />

    <Button
        android: layout_width="wrap_content"
        android: layout_height="wrap_content"
        android: text="@string/btnOk"
        android: id="@+id/btnOk"
        android: layout_centerVertical="true"
        android: layout_alignEnd="@+id/txtNome" />

    <TextView
        android: text="@string/lbSobreNome"
        android: layout_width="wrap_content"
        android: layout_height="wrap_content"
        android: id="@+id/textView2"
        android: layout_below="@+id/txtNome"
        android: layout_alignParentStart="true"
        android: layout_marginTop="47dp" />

    <EditText
        android: layout_width="500px"
        android: layout_height="wrap_content"
        android: id="@+id/txtSobrenome"
        android: layout_alignParentStart="true"
        android: layout_below="@+id/textView2"
        android: layout_alignParentLeft="true" />


</RelativeLayout>

	
MainActivity.java
package com.infoplace.helloworld;

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


public class  MainActivity extends  Activity {

    private Button BtnOk;
    private EditText txtNome, txtSobreNome;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        BtnOk = (Button) findViewById(R.id.btnOk);
        txtNome = (EditText) findViewById(R.id.txtNome);
        txtSobreNome = (EditText) findViewById(R.id.txtSobrenome);

        BtnOk.setOnClickListener(new  View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //Toast.makeText(MainActivity.this, "Nome: " + txtNome.getText() + "   Sobre Nome: " + txtSobreNome.getText(), Toast.LENGTH_SHORT).show();
                Intent it = new Intent(MainActivity.this, ActivityRecebe.class);
                Bundle params = new Bundle();
                params.putString("nome", txtNome.getText().toString());
                params.putString("sobrenome", txtSobreNome.getText().toString());
                it.putExtras(params);
                startActivity(it);
                finish();
            }
        });

    }

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

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();

        //noinspection SimplifiableIfStatement
        if (id == R.id.action_settings) {
            return true;
        }

        return super.onOptionsItemSelected(item);
    }
}


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

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="New Text"
        android:textSize="50dp"
        android:textColor="@color/azulEscuro"
        android:id="@+id/lbNome"
        android:layout_alignParentTop="true"
        android:layout_alignParentStart="true" />

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="New Text"
        android:textColor="@color/vermelho"
        android:textSize="50dp"
        android:id="@+id/lbSobreNome"
        android:layout_below="@+id/lbNome"
        android:layout_alignParentStart="true"
        android:layout_marginTop="47dp" />

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="New Button"
        android:id="@+id/button"
        android:layout_centerVertical="true"
        android:layout_alignParentStart="true" />

</RelativeLayout>
	
ActivityRecebe.java
package com.infoplace.helloworld;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;


public class ActivityRecebe extends Activity {

    private TextView lbNome, lbSobreNome;
    private Button btnFechar;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_activity_recebe);

        lbNome = (TextView) findViewById(R.id.lbNome);
        lbSobreNome = (TextView) findViewById(R.id.lbSobreNome);
        btnFechar = (Button) findViewById(R.id.button);

        Intent it = getIntent();
        Bundle params = new Bundle();
        params = it.getExtras();

        if (params != null){
            lbNome.setText(params.getString("nome"));
            lbSobreNome.setText(params.getString("sobrenome"));
        }

        btnFechar.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });

    }

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

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();

        //noinspection SimplifiableIfStatement
        if (id == R.id.action_settings) {
            return true;
        }

        return super.onOptionsItemSelected(item);
    }
}

	
Essa foi mais um vídeo aula seguindo o projeto de Android. Espero que gostem e principalmente entendam!

Fiquem atentos e vamos para os estudos!! Se tiverem alguma dúvida ou sugestões de novos posts favor me avise pelo Facebook ou então deixando seu comentário no blog.

Nenhum comentário:

Postar um comentário