Baixe o app para aproveitar ainda mais
Prévia do material em texto
PDM - Aula07- Ver 01 - 18/10/2012 09:37 Programação de Dispositivos Móveis Aula 07 Interface Gráfica – AlertDialog AlertDialog Exibe um alerta ao usuário Conteúdo e botões de escolha personalizados Classe AlertDialog.Builder para construir alertas setIcon(int resId): Determina o ícone a ser mostrado setTitle(String): Determina o título do alerta setMessage(String): Mensagem a ser mostrada no interior do alerta setPositiveButton(String, listener): Texto do botão positivo (Sim, Yes, Ok, etc) setNegativeButton(String, listener): Texto do botão negativo (Não, No, etc) setItems(String [], listener): Items a serem mostrados para usuário selecionar setSingleChoiceItems(String [], int checkedItem, listener): Determina lista de RadioButtons a serem mostrados ao usuário setCancelable(boolean): Alerta cancelável ou não. Cancelável significa que usuário não pode fechar com botão voltar. show(): Exibe o alerta para o usuário cancel(): Cancela o alerta Nota: listener é uma instância de DialogInterface.OnClickListener Continuação da Aplicação Android (Aula07B) O aplicativo que agora vai permitir o usuário verificar a funcionalidade do AlertDialog. Continuação da Aula07B AlertDialog O aplicativo que agora vai permitir o usuário verificar a funcionalidade do AlertDialog. Listagem 1. exemplo_alertdialog.xml <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" > <Button android:id="@+id/b" android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="Teste Alerta" /> </LinearLayout> Listagem 2. ExemploAlertDialog.java package com.pdm.aula07bex1; import android.app.Activity; import android.app.AlertDialog; import android.content.DialogInterface; import android.os.Bundle; import android.view.View; import android.widget.Button; import android.widget.Toast; import android.view.View.OnClickListener; public class ExemploAlertDialog extends Activity{ Button b; String msg; //@Override protected void onCreate(Bundle icicle) { super.onCreate(icicle); setContentView(R.layout.exemplo_alertdialog); // Exibe o alerta de confirmação ao clicar no botão b = (Button) findViewById(R.id.b); b.setOnClickListener(new OnClickListener() { public void onClick(View arg0) { AlertDialog diaBox = criarDialogBox(); diaBox.show(); // WARNING: (in general...) // after showing a dialog you should have NO more code. Let the buttons of // the dialog box handle the rest of the logic. For instance, in this // example a modal dialog box is displayed (once shown you can not do // anything to the parent until the child is closed) however the code in // the parent continues to execute after the show() method is // called. //txtMsg.setText("I am here!"); } }); }// fim do onCreate private AlertDialog criarDialogBox(){ AlertDialog meuDialogo = new AlertDialog.Builder(this) //configura messagem, título, e imagem .setTitle("Finalização") .setMessage("Tem certeza que deseja sair?") .setIcon(R.drawable.smile1) //configura os três botões de opção .setPositiveButton("Sim", new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int whichButton) { //o que deve ser feito quando a resposta for SIM msg = "SIM "+ Integer.toString(whichButton); //txtMsg.setText(msg); Toast.makeText(ExemploAlertDialog.this, "Sim! " + msg, Toast.LENGTH_SHORT).show(); finish(); } })//setPositiveButton .setNeutralButton("Cancela",new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int whichButton) { //o que deve ser feito quando a resposta for CANCELA msg = "CANCELA " + Integer.toString(whichButton); //txtMsg.setText(msg); Toast.makeText(ExemploAlertDialog.this, "Cancela! " + msg, Toast.LENGTH_SHORT).show(); }//OnClick })//setNeutralButton .setNegativeButton("Não", new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int whichButton) { //o que deve ser feito quando a resposta for NÃO msg = "NÃO " + Integer.toString(whichButton); //txtMsg.setText(msg); Toast.makeText(ExemploAlertDialog.this, "Não! " + msg, Toast.LENGTH_SHORT).show(); } })//setNegativeButton .create(); return meuDialogo; } } Listagem 3. Aula07BActivity.java package com.pdm.aula07bex1; import com.pdm.aula07bex1.R; import android.app.Activity; import android.content.Intent; import android.os.Bundle; import android.view.View; import android.view.View.OnClickListener; import android.widget.Button; public class Aula7BActivity extends Activity implements OnClickListener{ /** Called when the activity is first created. */ Button b1,b2,b3,b4,b5,b6,b7; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); b1 = (Button) this.findViewById(R.id.buttonImageImageButton); b1.setOnClickListener(this); b2 = (Button) this.findViewById(R.id.buttonAlertDialog); b2.setOnClickListener(this); b3 = (Button) this.findViewById(R.id.buttonToast); b3.setOnClickListener(this); b4 = (Button) this.findViewById(R.id.buttonListView); b4.setOnClickListener(this); b5 = (Button) this.findViewById(R.id.buttonAutoComplete); b5.setOnClickListener(this); b6 = (Button) this.findViewById(R.id.buttonSpinner); b6.setOnClickListener(this); b7 = (Button) this.findViewById(R.id.buttonProgressDialog); b7.setOnClickListener(this); } public void onClick(View v){ if (v.getId() == b2.getId()){ startActivity(new Intent(this, ExemploAlertDialog.class)); } } } Listagem 4. AndroidManifest.xml <?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.pdm.veiculo" android:versionCode="1" android:versionName="1.0"> <uses-sdk android:minSdkVersion="8" /> <application android:icon="@drawable/ic_launcher" android:label="@string/app_name" > <activity android:name=".Aula7BActivity" android:label="@string/app_name" > <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> <activity android:name=".ExemploAutoCompleteTextView"></activity> <activity android:name=".ExemploImageImageButton"></activity> <activity android:name=".ExemploSpinner"></activity> <activity android:name=".ExemploAlertDialog"></activity> <!-- Atenção - ACRESCENTAR A MEDIDA QUE FOR CODIFICANDO CADA VIEW --> <activity android:name=".ExemploToast"></activity> <activity android:name=".ExemploListView"></activity> <activity android:name=".ExemploProgressDialog"></activity> </application> </manifest> Página 3 de 4
Compartilhar