criei uma aplicação simples simulando uma cadastro de cliente mas que serve apenas para mim exibir algo no AlertDialog.Builder veja abaixo o codigo :
activity_main.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/LinearLayout1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
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="com.example.alertbilder.MainActivity" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Cadastro de Cliente" />
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Nome" />
<EditText
android:id="@+id/t1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:ems="10" />
<TextView
android:id="@+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Cpf" />
<EditText
android:id="@+id/editText1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:ems="10"
android:digits="1234567890" >
<requestFocus />
</EditText>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Cadastrar" />
<Button
android:id="@+id/button2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Cancelar" />
</LinearLayout>
</LinearLayout>
MainActivity.java
package com.example.alertbilder;
import android.support.v7.app.ActionBarActivity;
import android.app.AlertDialog;
import android.app.AlertDialog.Builder;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
public class MainActivity extends ActionBarActivity {
EditText t1,t2;
Button b1,b2;
AlertDialog.Builder alerta;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
t1 = (EditText)findViewById(R.id.t1);
t2 = (EditText)findViewById(R.id.editText1);
b1 = (Button)findViewById(R.id.button1);
b2 = (Button)findViewById(R.id.button2);
b1.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
alerta = new AlertDialog.Builder(MainActivity.this);//instanciando o AlertDialog.Builder MainActivity.this
//quer dizer que ele sera aberto dentro desta tela
alerta.setTitle("Situação do Cadastro");// colocando titulo
alerta.setMessage("Cliente "+t1.getText().toString()+"com CPF "+t2.getText().toString()+"\n foi cadastrado com sucesso !");//Colocando a menssagem
alerta.setNeutralButton("ok",null);//colocando o botao ok para sair do alert
alerta.show();// exibindo na tela
}
});
b2.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
t1.setText("");
t2.setText("");
alerta = new AlertDialog.Builder(MainActivity.this);
alerta.setTitle("Situação do Cadastro");
alerta.setMessage("Cadastro Cancelado!");
alerta.setNeutralButton("ok",null);
alerta.show();
}
});
}
}
Em caso de Duvidas Comente a e
Nenhum comentário:
Postar um comentário