Mm

1- fs^ '■ btn.setOnClickListener(listener);

a^OT btn listener ffrft onClick()®MfflA °

2- ' ^AMft rm 2 föjffi' mmrm^ ■ btn.setOnClickListener(this);

aam^ btn immmmm^^mmm onCHckosMffl °

1) ffM Button mmft setBackgroundResource()®^ '

package com.misoo.ex03;

import android.app.Activity;

import android.os.Bundle;

import android.view.View;

import android.view.View.OnClickListener;

import android.widget.ImageButton;

public class ex03 extends Activity { @Override public void onCreate(Bundle icicle) { super.onCreate(icicle); setContentView(R.layout.main);

ImageButton btn = (ImageButton)findViewById(R.id.£M#on); ImageButton btn2 = (ImageButton)findViewById(R.id.£M#on2); btn. setOnClickListener(listener); btn2. setOnClickListener(listener2);

OnClickListener listener = new OnClickListener() { public void onClick(View v) {

setTitle("this is OK button");

OnClickListener listener2 = new OnClickListener() { public void onClick(View v) { finish(); }

^SSÍSffiffli/res/drawable/ffiftMÍSHÍt : ok.jpg ^ exit.jpg = M ImageButton ®ffiMH^£IW#IJ®^/res/layout/mam.xml ^ffi ' $PTH :

exGl exG2 exG3 exG3_a ¡¿* src s Android Library 12 assets IS res

□ Android Manifest .xml exQ4

ex05

ex06

i!, exQ3.jaya main.xml

<?xml version="1.0" encoding="utf-8"?> -iLinear Layout xmlns:android="http://schema android: orientat.ion= "vertical" android:layout width="fill parent"

andr o i d: 1 ay o ut_he i gilt = " f i 1 l_p ar e nt"

<TextVieur andro id: layout_nridth= "t i 1 l_parent" andro id:layout_he ight = "wrap_content" android:text=""

-ilrnageButton android: id="@+id/button" android:layout width="rarap content" andro id:layout_he ight = "weap_content" android: src="@drawable/ok"

AVvVvVvVvWvVvVvV, iWA

<ImageButton android: id="@+id/kiutton2 " android: layout_nridth= "nrrap_content" android: layout height="wrap content" android:src="@drawable/ex it"

MWvVvVvWvVvVvVl

</L inearLayout>

Xf Xl rXg^XIH Android fM^ftXXWM^

Android °

^ft^fe(view, m Button #) 'M^fiMftA^MftA^'S-^SAA-gfA^ftS

° M-^Affift^fe - AKftAMm«®ffi^(Layout) o n ft'I^gffMftffilS^"

l. ftfMAftMAi

LSHItffimA :

:fS(Title)MHAAAA : "this is Cancel

2. mASA<OK>gffi ' *®ffS(Title)MHAAAA : "this is OK button" -

3. mASAfffeft<Cancel>gffi > I

Step-2: MM Activity ftASBU : ex04 > SfMAfiimA

// ---- ex04.java fMAfif package com.misoo.ex04; import android.app.Activity;

import android.graphics.Color;

import android.graphics.drawable.Drawable;

import android.os.Bundle;

import android.view.View;

import android.view.View.OnClickListener;

import android.widget.Button;

import android.widget.ImageButton;

import android.widget.LinearLayout;

public class ex04 extends Activity {

private final int WC = LinearLayout.LayoutParams. WRAP_CONTENT; @Override public void onCreate(Bundle icicle) { super.onCreate(icicle);

LinearLayout layout = new LinearLayout(this); layout.setOrientation(LinearLayout. VERTICAL);

ImageButton btn = new ImageButton(this); Drawable dw = this.getResources().getDrawable(R.drawable.o^); btn.setImageDrawable(dw); btn.setOnClickListener(listener); LinearLayout.LayoutParams param =

new LinearLayout.LayoutParams(WC, WC); layout.addView(btn, param); ImageButton btn2 = new ImageButton(this); dw = this.getResources().getDrawable(R.drawable.ex/Y); btn2. setImageDrawable(dw); btn2. setOnClickListener(listener2); layout.addView(btn2, param);

LinearLayout out_layout = new LinearLayout(this); out_layoutsetOrientation(LinearLayout.HOR/ZONTAL); Button btn3 = new Button(this);

btn3.setText("Cancel"); btn3.setTextColor(Color. WHITE);

btn3. setBackgroundColor(Color.RED); btn3. setOnClickListener(listener3);

out_lay out. addView(lay out, param);

LinearLayout.LayoutParams param2 =

new LinearLayout.LayoutParams(WC, WC); param2.topMargin = 30; out_layout.addView(btn3, param2); setContentView(out_layout);

OnClickListener listener = new OnClickListener(){

public void onClick(View v) { setTitle("this is OK button"); }

OnClickListener listener2 = new OnClickListener() {

OnClickListener listener3 = new OnClickListener() {

public void onClick(View v){ setTitle("this is Cancel button"); }

0 0

Post a comment