
----------------------------------- Android 编程基础
1
封面

----------------------------------- Android 编程基础
2
应用
为程序添加 Menu
Menu
Menu
Menu 菜单
// 创建 OptionsMenu public
public
public
public boolean
boolean
boolean
boolean onC reateO ptionsM enu
onC reateO ptionsM enu
onC reateO ptionsM enu
onC reateO ptionsM enu (
(
(
(
M enu
M enu
M enu
M enu
m enu
m enu
m enu
m enu
)
)
)
)
// 处理选择事件 public
public
public
public boolean
boolean
boolean
boolean onO ptionsItem Selected
onO ptionsItem Selected
onO ptionsItem Selected
onO ptionsItem Selected (
(
(
(
M enuItem
M enuItem
M enuItem
M enuItem
item
item
item
item )
)
)
)
public
public
public
public boolean
boolean
boolean
boolean onCreateOptionsMenu(Menu menu) {
// TODO
TODO
TODO
TODO Auto-generated method stub
boolean
boolean
boolean
boolean result = super
super
super
super .onCreateOptionsMenu(menu);
menu.add(0,
INSERT_ID_Play
, 0, R.string.
menu_toPlay
);
menu.add(0,
INSERT_ID_Stop
, 0, R.string.
menu_toStop
);
return
return
return
return result ;
} // 创建菜单
public
public
public
public boolean
boolean
boolean
boolean onOptionsItemSelected(MenuItem item) {
// TODO
TODO
TODO
TODO Auto-generated method stub
if
if
if
if (item.getItemId()==
INSERT_ID_Play
){
Play_Music();
}
if
if
if
if (item.getItemId()==
INSERT_ID_Stop
){
Stop_Music();
}
return
return
return
return super
super
super
super .onOptionsItemSelected(item);
}

----------------------------------- Android 编程基础
3
应用 SDCard
SDCard
SDCard
SDCard
① 打开 CMD
② 进入 C:\Documents and Settings\ 地狱怒兽 \Local Settings\Application Data\Android\SDK-1.1 目录
③ 创建 sdcard 镜像 mksdcard 256M ./sdcard.img
④ 往 SDCard 中添加资源: adb push zyf.mp3 /sdcard/zyf.mp3
⑤ 往 SDCard 中获取资源: adb pull /sdcard/mybaby.jpg C:\
⑥ 重启模拟器后,文件即在虚拟 SDCard 中
向模拟器安装 APK
APK
APK
APK 软件包
① 打开 cmd
② 切换到 Android SDK tools 目录下
③ 把 APK 软件包复制到 Android SDK tools 目录下
④ adb install Snake.pak
删除模拟器中 APK
APK
APK
APK 软件包
① 打开模拟器
② 打开 cmd
③ adb shell
④
cd
data/app
⑤ ls -l
⑥ rm 文件名 .apk

----------------------------------- Android 编程基础
4
对话框的简单应用
AlertDialog.Builder
Dialog
排版布局 Layout
Layout
Layout
Layout
public
public
public
public class
class
class
class DialogDemo extends
extends
extends
extends Activity implements
implements
implements
implements OnClickListener{
public
public
public
public void
void
void
void onCreate(Bundle savedInstanceState) {
super
super
super
super .onCreate(savedInstanceState);
setContentView(R.layout.
main
);
AlertDialog.Builder myBuilder= new
new
new
new AlertDialog.Builder( this
this
this
this );
myBuilder.setIcon(R.drawable.
hermes
);
myBuilder. setTitle( " 我的对话框 " );
myBuilder.setPositiveButton( " 退出 " , this
this
this
this );
myBuilder.show();
}
public
public
public
public void
void
void
void onClick(DialogInterface dialog, int
int
int
int which) {
}
}
public
public
public
public class
class
class
class DialogDemo extends
extends
extends
extends Activity {
public
public
public
public void
void
void
void onCreate(Bundle savedInstanceState) {
super
super
super
super .onCreate(savedInstanceState);
setContentView(R.layout.
main
);
Dialog myDialog= new
new
new
new Dialog( this
this
this
this );
myDialog.setTitle( "this is a Dialog" );
myDialog.show();
}
}
元件名稱
説明
FrameLayout
單一物件的容器
AbsoluteLayout
以絕對座標排版的容器
LinearLayout
線性 ( 水平或垂直 ) 排版的容器
RelativeLayout
以相對座標 ( 相對於父元件或兄弟元件 ) 排版的容器
TableLayout
以表格方式排版的容器

----------------------------------- Android 编程基础
5
TextView
TextView
TextView
TextView 中的超链接文本
< TextView
android:id =
"@+id/linkText"
android:layout_width =
"fill_parent"
android:layout_height =
"wrap_content"
android:text =
"@string/linktext"
android:autoLink =
"all"
>
</ TextView >
关键点:设置了该属性则 可
以自动链接,否则没有链 接