Baixe o app para aproveitar ainda mais
Prévia do material em texto
PDM - Aula07- Ver 01 - 18/10/2012 10:16 Programação de Dispositivos Móveis Aula 07 Interface Gráfica –Image e ImageButton Permitem criar botões clicáveis na tela Button -subclasse de TextView ImageButton -subclasse de ImageView Setando imagem via Java e XML imageButton.setImageResource(R.drawable.image_id) button.setCompoundDrawablesWithIntrinsicBounds(left,top,right,bottom) <ImageButton android:src="@drawable/..." ... /> <Button android:drawableLeft="@drawable/..." android:drawableTop="..." android:drawableRight="..." android:drawableBottom="..." ... /> Detectando clique: final Button button = (Button) findViewById(R.id.button_id); button.setOnClickListener(new View.OnClickListener() { public void onClick(View v) { // Perform action on click } }); Continuação da Aplicação Android (Aula07B) O aplicativo que agora vai permitir o usuário verificar a funcionalidade do Image e ImageButton. Código – Aula07B Image e ImageButton Listagem 1. exemplo_image_imagebutton.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" > <ImageView android:id="@+id/imageView1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center" /> <ImageView android:id="@+id/imageView2" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center" /> <ImageButton android:id="@+id/imageButton1" android:layout_width="fill_parent" android:layout_height="wrap_content" android:src="@drawable/smile1" /> <ImageButton android:id="@+id/imageButton2" android:layout_width="wrap_content" android:layout_height="wrap_content" /> <Button android:id="@+id/buttonSair" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center" android:text="Sair" /> </LinearLayout> Listagem 2. ExemploImageImageButton.java package com.pdm.aula07bex1; import android.app.Activity; import android.os.Bundle; import android.view.View; import android.view.View.OnClickListener; import android.widget.Button; import android.widget.ImageButton; import android.widget.ImageView; import android.widget.Toast; public class ExemploImageImageButton extends Activity implements OnClickListener { Button bSair; ImageButton bImagem1,bImagem2; public void onCreate(Bundle icicle) { super.onCreate(icicle); setContentView(R.layout.exemplo_imagem_imagebutton); ImageView imgView = (ImageView)findViewById(R.id.imageView1); imgView.setImageResource(R.drawable.balloons); imgView = (ImageView)findViewById(R.id.imageView2); imgView.setImageResource(R.drawable.icon); bImagem1 = (ImageButton) findViewById(R.id.imageButton1); bImagem1.setOnClickListener(this); bImagem2 = (ImageButton) findViewById(R.id.imageButton2); bImagem2.setImageResource(R.drawable.smile2); bImagem2.setOnClickListener(this); bSair = (Button) this.findViewById(R.id.buttonSair); bSair.setOnClickListener(this); } public void onClick(View v){ if (v.getId() == bSair.getId()){ finish(); } if (v.getId() == bImagem1.getId()){ Toast.makeText(ExemploImageImageButton.this, "ImagemButton 1 OK", Toast.LENGTH_SHORT).show(); } if (v.getId() == bImagem2.getId()){ Toast.makeText(ExemploImageImageButton.this, "ImagemButton 2 OK", Toast.LENGTH_SHORT).show(); } } } 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() == b1.getId()){ startActivity(new Intent(this, ExemploImageImageButton.class)); } if (v.getId() == b5.getId()){ startActivity(new Intent(this, ExemploAutoCompleteTextView.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> <!-- Atenção - ACRESCENTAR A MEDIDA QUE FOR CODIFICANDO CADA VIEW --> <activity android:name=".ExemploAlertDialog"></activity> <activity android:name=".ExemploToast"></activity> <activity android:name=".ExemploListView"></activity> <activity android:name=".ExemploSpinner"></activity> <activity android:name=".ExemploProgressDialog"></activity> </application> </manifest> Página 4 de 5
Compartilhar