Baixe o app para aproveitar ainda mais
Prévia do material em texto
PDM - Aula07- Ver 01 - 18/10/2012 09:14 Programação de Dispositivos Móveis Aula 07 Interface Gráfica – Toast Continuação da Aplicação Android (Aula07B) O aplicativo que agora vai permitir o usuário verificar a funcionalidade do Toast. Código – Aula07B Toast O aplicativo que agora vai permitir o usuário verificar a funcionalidade do Toast. Listagem 1. exemplo_toast.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" > <TextView android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="O Toast abriu com uma View em XML. \nClick no botão Repetir para ver Views no Toast também, não somente textos" /> <LinearLayout android:orientation="horizontal" android:layout_width="fill_parent" android:layout_height="fill_parent" android:gravity="center"> <Button android:id="@+id/buttonRepetir" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center" android:text="Repetir" /> <Button android:id="@+id/buttonSair" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center" android:text="Sair" /> </LinearLayout> </LinearLayout> Listagem 2. exemplo_toast_view_inflate.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" > <View android:layout_height="2dip" android:layout_width="fill_parent" android:background="#FF909090" /> <TextView android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="Exemplo para o LayoutInflater" /> <TextView android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="O Android permite abrir View dentro de Mensagem!" /> <ImageView android:layout_width="fill_parent" android:layout_height="wrap_content" android:src="@drawable/smile1" /> <View android:layout_height="2dip" android:layout_width="fill_parent" android:background="#FF909090" /> </LinearLayout> Listagem 3. ExemploToast.java package com.pdm.aula07bex1; import android.app.Activity; import android.content.Context; import android.os.Bundle; import android.view.LayoutInflater; import android.view.View; import android.view.View.OnClickListener; import android.widget.Button; import android.widget.Toast; public class ExemploToast extends Activity implements OnClickListener{ Button bSair,bRepetir; Toast toast; @Override public void onCreate(Bundle icicle) { super.onCreate(icicle); setContentView(R.layout.exemplo_toast); bSair = (Button) this.findViewById(R.id.buttonSair); bSair.setOnClickListener(this); bRepetir = (Button) this.findViewById(R.id.buttonRepetir); bRepetir.setOnClickListener(this); // Toast toast = Toast.makeText(this, "Teste de Mensagem", Toast.LENGTH_SHORT); // toast.show(); View tv = getView(); this.toast = new Toast(this); this.toast.setView(tv); this.toast.setDuration(Toast.LENGTH_LONG); this.toast.show(); } private View getView() { //View view = new TextView(this); //view.setText("Podemos mostrar qualquer coisa no Toast"); //Abrir uma View a partir de um xml LayoutInflater inflate = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE); View view = inflate.inflate(R.layout.exemplo_toast_view_inflate, null); //ImageView view = new ImageView(this); //view.setImageResource(R.drawable.smile1); return view; } public void onClick(View v){ if (v.getId() == bSair.getId()){ finish(); } if (v.getId() == bRepetir.getId()){ this.toast.show(); } } } Listagem 4. 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() == b3.getId()){ startActivity(new Intent(this, ExemploToast.class)); } } } Listagem 5. 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=".ExemploAlertDialog"></activity> <activity android:name=".ExemploToast"></activity> <!-- Atenção - ACRESCENTAR A MEDIDA QUE FOR CODIFICANDO CADA VIEW --> <activity android:name=".ExemploListView"></activity> <activity android:name=".ExemploSpinner"></activity> <activity android:name=".ExemploProgressDialog"></activity> </application> </manifest> Página 4 de 4
Compartilhar