Info

4.9 w-iffimmmmffim

4.11 #11: RadioButton

4.12 #12 Activity Activity

Cheese

> S^^^tom-kao.blogspot.com& www.misoo1.com 0 — 36 &»&!!

3. B®T<MENU>I^ffiim ' Item>MM ' ^ff(Title)MHA A : "Delete..." =

4. B®T<MENU>^^mM* ' MMAX<Exit>MM ' fMAMInAT °

package com.misoo.ex01; import android.app.Activity; import android.os.Bundle; import android.view.Menu; import android.view.MenuItem;

public class exOl extends Activity {

public static final int ADDID = Menu.FIRST; public static final int DELETED = Menu.FIRST + l;

public static final int EXITID = Menu.FIRST + 2; @Ovemde public void onCreate(Bundle icicle) { super.onCreate(icicle); setContentView(R.layout.main); } @Override public boolean onCreateOptionsMenu(Menu menu) { super.onCreateOptionsMenu(menu); menu.add(0, ADDID, 0, R.string.menu_add); menu.add(0, DELETEID, 1, R.string.menudelete); menu.add(0, EXIT ID, 2, R.string.menu_exit); return true; }

@Override public boolean onOptionsItemSelected(MenuItem item) { switch (item.getItemId()) {

case ADD ID: setTitle("Insert..."); break;

case DELETEID: setTitle("Delete..."); break;

return super.onOptionsItemSelected(item);

Step-3: flfB^/res/values/strings.xml ' :

<?xml version="1.0" encoding="utf-8"?> <resources>

<string name="app_name">ex01</string> <string name="menu_add">Add Item</string> <string name="menu_delete">Del Item</string> <string name="menu_exit">Exit</string> </resources>

2. Item>M^^ ' g^WM^W onOptionsItemSelected()®^ =

4. ^^ onCreateOSTO ' ftSm^MW^^ Activity ft onCreate()®

m ' ^mnxmmmmmm • mrn^mn ex01::onCreateo®»i&iwn

M 0 ÊH^ÎT^ setContentView(R.layout.main)fgAH# ' main.xml m

5. J^ja onCreateOptionsMenu()' ^A^AbA menu.add(0, ADDID, KTString.menu_add)^^^^/res/values/strings.xml ' ^A#AA"Add item" ' iiAA»®mMmA °

ft onCreate()®mteAgBA^Mrcreate j mfi^ ' MMAS^mïSIS®

ÈAÊM paintO^a^S^m onPaint()®m ' (MASM^AS^ASIt ' AA^arAS^m onPamto - $nAMA»mis • iííama^m

// IGraph.java —^M public interface IGraph { void onPaint(); void paint();

public abstract class Shape implements IGraph{ Graphics m_gr;

public Shape(Graphics gr) { m_gr = gr; } public void onPaint(){ // MXSWS m_gr.setColor(Color.black); m_gr.fillRect(10,30, 200,100);

// Bird.java — import java.awt.*; public class Bird extends Shape { Graphics m_gr; public Bird(Graphics gr) { super(gr); m_gr = gr; } public void onPaint(){

super.onPaint();

m_gr. setColor(Color. cyan);

m_gr.drawArc(30,80,90,110,40,100); m_gr.drawArc(88,93,90,100,40,80); m_gr. setColor(Color. white);

m_gr.drawArc(30,55,90,150,35,75); m_gr.drawArc(90,80,90,90,40,80);

// JMain.java ---import java.awt.*; import javax.swing.*; class JP extends JPanel {

public void paintComponent(Graphics gr){ super.paintComponents(gr) ;

public class JMain extends JFrame {

public JMain(){ setTitle(""); setSize(350, 250); } public static void main(String[] args) { JMain frm = new JMain(); JP panel = new JP(); frm.add(panel);

frm.setDefaultCloseOperation(JFrame.EX7T_0N_CL0SE); frm. setVisible(true);

' AfMAftfgA : cc.pamt()«A^ Bird ft paint() > fBft Bird m paintOM ' ^lifflX!^ Shape ftfl=£ paint()- ftfUSMW onPaint() > MM^WTA^ft onPaint() = > ftA^ paint()«A^

BUft onPaint() ; MAftA^ onPaint()AWA^ft onPaint() = MMftAS^ onPaint()^^A^^ft onPaint() °

* #M&/res/values/strings.xml ^WA' ' ' ^fAM ? SMfi AAMfAR.javaftl^W ' Mmenu.add(0, ADD ID, R.string. menu_add)^At^W

---- 'g(Rjava)ftSM*java l&g^M^W.xml M^^X« ° ---- X res/layout/g^W^*.xml ^ = Eclipse SSi^.xml ^^^MSKM

MKdP Rjava =

---- tmu^m^immm(/res/)mm^\ • Ma*.xml mmm^^ ° mrnm ap u^mmtmmwmmm ° -ma®^ • *.java g^inii

(R.layout.main) fg ^ ffi ^ R.layout.main il-fi^® ' f main.xml > iMT main.xml ^fH£^(Default)^#®«MiMT °

Was this article helpful?

0 0

Post a comment