`

Android开发-菜单与对话框

 
阅读更多
Android开发06—菜单与对话框(下)

1. 进度对话框
ProgressDialog可以显示进度轮和进度条,由于ProgressDialog继承自AlertDialog,所以在进度对话框中也可以添加按钮。
实例说明进度对话框的用法:
Java代码 复制代码
  1. packageqijia.si;
  2. importandroid.app.Activity;
  3. importandroid.app.Dialog;
  4. importandroid.app.ProgressDialog;
  5. importandroid.os.Bundle;
  6. importandroid.os.Handler;
  7. importandroid.os.Message;
  8. importandroid.view.View;
  9. importandroid.view.View.OnClickListener;
  10. importandroid.widget.Button;
  11. publicclassContextMenuDemoextendsActivity{
  12. //声明进度对话框id
  13. finalintPROGRESS_DIALOG=0;
  14. //Handler消息类型
  15. finalintINCREASE=0;
  16. //进度对话框对象引用
  17. ProgressDialogpd;
  18. //Handler对象引用
  19. HandlermyHandler;
  20. /**Calledwhentheactivityisfirstcreated.*/
  21. @Override
  22. publicvoidonCreate(BundlesavedInstanceState){
  23. super.onCreate(savedInstanceState);
  24. setContentView(R.layout.main);
  25. //获得Button对象
  26. Buttonbt=(Button)this.findViewById(R.id.button1);
  27. //设置onClickListener监听器
  28. bt.setOnClickListener(newOnClickListener(){
  29. publicvoidonClick(Viewv){
  30. //TODOAuto-generatedmethodstub
  31. showDialog(PROGRESS_DIALOG);
  32. }
  33. });
  34. //创建handler对象
  35. myHandler=newHandler(){
  36. @Override
  37. publicvoidhandleMessage(Messagemsg){
  38. //TODOAuto-generatedmethodstub
  39. switch(msg.what){
  40. caseINCREASE:
  41. pd.incrementProgressBy(1);
  42. //如果进度走完就关闭
  43. if(pd.getProgress()>=100){
  44. pd.dismiss();
  45. }
  46. break;
  47. }
  48. super.handleMessage(msg);
  49. }
  50. };
  51. }
  52. //重写onCreateDialog方法
  53. publicDialogonCreateDialog(intid){
  54. switch(id){
  55. //创建进度对话框
  56. casePROGRESS_DIALOG:
  57. pd=newProgressDialog(this);
  58. //设置进度对话框
  59. pd.setMax(100);
  60. pd.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
  61. pd.setTitle(R.string.title);
  62. pd.setCancelable(false);
  63. newThread(){
  64. publicvoidrun(){
  65. while(true){
  66. //发送Handler消息
  67. myHandler.sendEmptyMessage(INCREASE);
  68. if(pd.getProgress()>=100){
  69. break;
  70. }
  71. try{
  72. Thread.sleep(100);
  73. }catch(Exceptione){
  74. e.printStackTrace();
  75. }
  76. }
  77. }
  78. }.start();
  79. break;
  80. }
  81. returnpd;
  82. }
  83. //每次弹出对话框时被动态回调以动态更新对话框
  84. publicvoidonPrePareDialog(intid,Dialogdialog){
  85. super.onPrepareDialog(id,dialog);
  86. switch(id){
  87. casePROGRESS_DIALOG:
  88. //对话框进度清零
  89. pd.incrementProgressBy(-pd.getProgress());
  90. //匿名对象
  91. newThread()
  92. {
  93. publicvoidrun(){
  94. while(true){
  95. //发送Handler消息
  96. myHandler.sendEmptyMessage(INCREASE);
  97. if(pd.getProgress()>=100){
  98. break;
  99. }
  100. try{
  101. Thread.sleep(100);
  102. }catch(Exceptione){
  103. e.printStackTrace();
  104. }
  105. }
  106. }
  107. }.start();
  108. break;
  109. }
  110. }
  111. }

2. 消息提示
1) Toast的使用
Toast向用户提供比较快速的即时消息,当Toast被显示时,虽然其悬浮于应用程序的最上方,但是Toast从不获得焦点。因为涉及Toast时就是为了让其在提示有用信息时尽量不显眼。Toast用于用户某项设置成功等。
Toast对象的创建通过Toast类的静态方法makeText来实现,该方法有两个重载实现,主要的不同时一个接受字符串,而另一个接收字符串的资源标识符作为参数。Toast对象创建好之后通过show方法即可将消息提示显示到屏幕上。

实例:
Java代码 复制代码
  1. packageqijia.si;
  2. importandroid.app.Activity;
  3. importandroid.os.Bundle;
  4. importandroid.view.Gravity;
  5. importandroid.view.View;
  6. importandroid.view.View.OnClickListener;
  7. importandroid.widget.Button;
  8. importandroid.widget.ImageView;
  9. importandroid.widget.LinearLayout;
  10. importandroid.widget.Toast;
  11. publicclassContextMenuDemoextendsActivity{
  12. /**Calledwhentheactivityisfirstcreated.*/
  13. @Override
  14. publicvoidonCreate(BundlesavedInstanceState){
  15. super.onCreate(savedInstanceState);
  16. setContentView(R.layout.main);
  17. Buttonbtn=(Button)this.findViewById(R.id.button1);
  18. btn.setOnClickListener(newOnClickListener(){
  19. publicvoidonClick(Viewv){
  20. //TODOAuto-generatedmethodstub
  21. //创建ImageView
  22. ImageViewiv=newImageView(ContextMenuDemo.this);
  23. iv.setImageResource(R.drawable.header);
  24. //创建一个线性布局
  25. LinearLayoutll=newLinearLayout(ContextMenuDemo.this);
  26. Toasttoast=Toast.makeText(ContextMenuDemo.this,"HelloWorld",Toast.LENGTH_LONG);
  27. toast.setGravity(Gravity.CENTER,0,0);
  28. ViewtoastView=toast.getView();
  29. ll.setOrientation(LinearLayout.HORIZONTAL);
  30. //将ImageView添加到线性布局
  31. ll.addView(iv);
  32. //将Toast的View添加到线性布局
  33. ll.addView(toastView);
  34. toast.setView(ll);
  35. //显示toast
  36. toast.show();
  37. }
  38. });
  39. }
  40. }
2) Notification
Notification是另一种消息提示的方式。Notification位于手机的状态栏,在Android手机中用手指按下状态栏并往下拉可以打开状态栏查看系统的提示信息。

实例:

main.xml:
Xml代码 复制代码
  1. <?xmlversion="1.0"encoding="utf-8"?>
  2. <LinearLayoutxmlns:android="http://schemas.android.com/apk/res/android"android:layout_height="fill_parent"android:id="@+id/linearLayout1"android:layout_width="fill_parent"android:orientation="vertical">
  3. <Buttonandroid:layout_height="wrap_content"android:id="@+id/button1"android:text="@string/btn"android:layout_width="fill_parent"></Button>
  4. </LinearLayout>




notified.xml

Xml代码 复制代码
  1. <?xmlversion="1.0"encoding="utf-8"?>
  2. <LinearLayoutandroid:id="@+id/linearLayout1"android:layout_width="fill_parent"android:layout_height="fill_parent"xmlns:android="http://schemas.android.com/apk/res/android">
  3. <EditTextandroid:layout_height="wrap_content"android:id="@+id/editText1"android:text="@string/tv"android:layout_width="fill_parent"android:editable="false"android:layout_gravity="center_horizontal"></EditText>
  4. </LinearLayout>



1. Activity
Java代码 复制代码
  1. packageqijia.si;
  2. importandroid.app.Activity;
  3. importandroid.app.Notification;
  4. importandroid.app.NotificationManager;
  5. importandroid.app.PendingIntent;
  6. importandroid.content.Intent;
  7. importandroid.os.Bundle;
  8. importandroid.view.View;
  9. importandroid.view.View.OnClickListener;
  10. importandroid.widget.Button;
  11. publicclassContextMenuDemoextendsActivity{
  12. /**Calledwhentheactivityisfirstcreated.*/
  13. @Override
  14. publicvoidonCreate(BundlesavedInstanceState){
  15. super.onCreate(savedInstanceState);
  16. setContentView(R.layout.main);
  17. Buttonbtn=(Button)findViewById(R.id.button1);
  18. //添加监听
  19. btn.setOnClickListener(newOnClickListener(){
  20. publicvoidonClick(Viewv){
  21. //TODOAuto-generatedmethodstub
  22. Intenti=newIntent(ContextMenuDemo.this,NotifiedActivity.class);
  23. //建立一个新的Activity
  24. PendingIntentpi=PendingIntent.getActivity(ContextMenuDemo.this,0,i,0);
  25. //创建一个Notification对象
  26. NotificationmyNotification=newNotification();
  27. myNotification.icon=R.drawable.header;
  28. //设置文字和声音
  29. myNotification.tickerText=getResources().getString(R.string.Notification);
  30. myNotification.defaults=Notification.DEFAULT_SOUND;
  31. myNotification.setLatestEventInfo(ContextMenuDemo.this,"Example","HelloWorld",pi);
  32. //建立NotificationManger并发送消息
  33. NotificationManagernotificationManager=(NotificationManager)getSystemService(NOTIFICATION_SERVICE);
  34. notificationManager.notify(0,myNotification);
  35. }
  36. });
  37. }
  38. }
2. Activity
Java代码 复制代码
  1. packageqijia.si;
  2. importandroid.app.Activity;
  3. importandroid.os.Bundle;
  4. //当用户在状态栏点击Notification时会启动该Activity
  5. publicclassNotifiedActivityextendsActivity{
  6. @Override
  7. protectedvoidonCreate(BundlesavedInstanceState){
  8. //TODOAuto-generatedmethodstub
  9. super.onCreate(savedInstanceState);
  10. setContentView(R.layout.notified);
  11. }
  12. }

注意最后在AndroidManifest.xml中添加新的Acitivity,否则无法显示。
Android开发06—菜单与对话框(上)

1. 菜单
1) 选项菜单和子菜单
当Activity在前台工作的时候,按下menu将会弹出相应的选项菜单。这个功能是需要开发人员编成实现的,如果在程序中没有此功能,那么程序运行时按下手机的menu键将不会有反映。
对于有图标的选项菜单,每次最多能显示6个,当多于6个时,将只显示前5个和一个拓展菜单选项。
在Android中通过回调方法来创建菜单并处理菜单按下的事件。
开发选项菜单主要用到Menu,MenuItem及SubMenu
实例:接受用户在菜单中的选项并输出到文本框控件中
main.xml:
Xml代码 复制代码
  1. <?xmlversion="1.0"encoding="utf-8"?>
  2. <LinearLayoutxmlns:android="http://schemas.android.com/apk/res/android"android:orientation="vertical"android:layout_height="fill_parent"android:layout_width="fill_parent"android:id="@+id/linearLayout1">
  3. <ScrollViewandroid:layout_width="fill_parent"android:layout_height="fill_parent"android:id="@+id/scrollView1">
  4. <EditTextandroid:layout_width="fill_parent"android:layout_height="fill_parent"android:id="@+id/editText1"android:editable="false"android:cursorVisible="false"android:text="@string/label"></EditText>
  5. </ScrollView>
  6. </LinearLayout>
Activity:
Java代码 复制代码
  1. packageqijia.si;
  2. importandroid.app.Activity;
  3. importandroid.app.TabActivity;
  4. importandroid.os.Bundle;
  5. importandroid.view.LayoutInflater;
  6. importandroid.view.Menu;
  7. importandroid.view.MenuItem;
  8. importandroid.view.MenuItem.OnMenuItemClickListener;
  9. importandroid.view.SubMenu;
  10. importandroid.view.View;
  11. importandroid.view.ViewGroup;
  12. importandroid.widget.AdapterView;
  13. importandroid.widget.AdapterView.OnItemClickListener;
  14. importandroid.widget.BaseAdapter;
  15. importandroid.widget.EditText;
  16. importandroid.widget.Gallery;
  17. importandroid.widget.ImageView;
  18. importandroid.widget.ProgressBar;
  19. importandroid.widget.RatingBar;
  20. importandroid.widget.TabHost;
  21. publicclassJavaTestextendsActivity{
  22. /**Calledwhentheactivityisfirstcreated.*/
  23. finalintMENU_GENDER_MALE=0;
  24. finalintMENU_GENDER_FEMALE=1;
  25. finalintMENU_HOBBY1=2;
  26. finalintMENU_HOBBY2=3;
  27. finalintMENU_HOBBY3=4;
  28. finalintMENU_OK=5;
  29. finalintMENU_GENDER=6;
  30. finalintMENU_HOBBY=7;
  31. finalintGENDER_GROUP=0;
  32. finalintHOBBY_GROUP=1;
  33. finalintMAIN_GROUP=2;
  34. MenuItem[]miaHobby=newMenuItem[3];
  35. MenuItemmale=null;
  36. @Override
  37. publicvoidonCreate(BundlesavedInstanceState){
  38. super.onCreate(savedInstanceState);
  39. setContentView(R.layout.main);
  40. }
  41. publicbooleanonCreateOptionsMenu(Menumenu){
  42. //初始化菜单通过此函数实现
  43. SubMenusubMenuGender=menu.addSubMenu(MAIN_GROUP,MENU_GENDER,0,R.string.gender);
  44. subMenuGender.setIcon(R.drawable.gender);
  45. subMenuGender.setHeaderIcon(R.drawable.gender);
  46. male=subMenuGender.add(GENDER_GROUP,MENU_GENDER_MALE,0,R.string.male);
  47. male.setChecked(true);
  48. subMenuGender.add(GENDER_GROUP,MENU_GENDER_FEMALE,0,R.string.female);
  49. subMenuGender.setGroupCheckable(GENDER_GROUP,true,true);
  50. SubMenusubMenuHobby=menu.addSubMenu(MAIN_GROUP,MENU_HOBBY,0,R.string.hobby);
  51. subMenuHobby.setIcon(R.drawable.hobby);
  52. miaHobby[0]=subMenuHobby.add(HOBBY_GROUP,MENU_HOBBY1,0,R.string.hobby1);
  53. miaHobby[1]=subMenuHobby.add(HOBBY_GROUP,MENU_HOBBY2,0,R.string.hobby2);
  54. miaHobby[2]=subMenuHobby.add(HOBBY_GROUP,MENU_HOBBY3,0,R.string.hobby3);
  55. miaHobby[0].setCheckable(true);
  56. miaHobby[1].setCheckable(true);
  57. miaHobby[2].setCheckable(true);
  58. MenuItemok=menu.add(GENDER_GROUP+2,MENU_OK,0,R.string.ok);
  59. OnMenuItemClickListenerlsn=newOnMenuItemClickListener(){
  60. publicbooleanonMenuItemClick(MenuItemitem){
  61. //TODOAuto-generatedmethodstub
  62. appendStateStr();
  63. returntrue;
  64. }
  65. };
  66. ok.setOnMenuItemClickListener(lsn);
  67. ok.setAlphabeticShortcut('o');
  68. returntrue;
  69. }
  70. publicbooleanonOptionsItemSelected(MenuItemmi){
  71. switch(mi.getItemId()){
  72. caseMENU_GENDER_MALE:
  73. caseMENU_GENDER_FEMALE:
  74. mi.setChecked(true);
  75. appendStateStr();
  76. break;
  77. caseMENU_HOBBY1:
  78. caseMENU_HOBBY2:
  79. caseMENU_HOBBY3:
  80. mi.setChecked(!mi.isChecked());
  81. appendStateStr();
  82. break;
  83. }
  84. returntrue;
  85. }
  86. publicvoidappendStateStr(){
  87. Stringresult="您选择的性别为:";
  88. if(male.isChecked()){
  89. result=result+"男";
  90. }else{
  91. result+="女";
  92. }
  93. StringhobbyStr="";
  94. for(MenuItemmi:miaHobby){
  95. if(mi.isChecked()){
  96. hobbyStr=hobbyStr+mi.getTitle()+",";
  97. }
  98. }
  99. if(hobbyStr.length()>0){
  100. result=result+",您的爱好为:"+hobbyStr.substring(0,hobbyStr.length()-1)+"。\n";
  101. }
  102. else{
  103. result=result+"。\n";
  104. }
  105. EditTextet=(EditText)JavaTest.this.findViewById(R.id.editText1);
  106. et.append(result);
  107. }
  108. }
2. 上下文菜单
ContextMenu继承自Menu。上下文菜单不同于选项菜单,选项菜单服务于Activity,而上下文菜单是注册到某个View对象上的。如果一个View对象注册了上下文菜单,用户可以通过长按该View对象以呼叫上下文菜单。
上下文菜单不支持快捷键,也不能附带图标,但是可以为上下文菜单的标题指定图标。
用法实例:
main.xml:
Xml代码 复制代码
  1. <?xmlversion="1.0"encoding="utf-8"?>
  2. <LinearLayoutxmlns:android="http://schemas.android.com/apk/res/android"android:layout_height="fill_parent"android:orientation="vertical"android:layout_width="fill_parent"android:id="@+id/linearLayout1">
  3. <EditTextandroid:layout_width="fill_parent"android:layout_height="wrap_content"android:id="@+id/editText1"android:text="@string/et1"></EditText>
  4. <EditTextandroid:layout_width="fill_parent"android:layout_height="wrap_content"android:id="@+id/editText2"android:text="@string/et2"></EditText>
  5. </LinearLayout>


Acticity:
Java代码 复制代码
  1. packageqijia.si;
  2. importandroid.app.Activity;
  3. importandroid.os.Bundle;
  4. importandroid.view.ContextMenu;
  5. importandroid.view.MenuItem;
  6. importandroid.view.View;
  7. importandroid.widget.EditText;
  8. publicclassContextMenuDemoextendsActivity{
  9. //定义菜单编号
  10. finalintMENU1=1;
  11. finalintMENU2=2;
  12. finalintMENU3=3;
  13. finalintMENU4=4;
  14. finalintMENU5=5;
  15. /**Calledwhentheactivityisfirstcreated.*/
  16. @Override
  17. publicvoidonCreate(BundlesavedInstanceState){
  18. super.onCreate(savedInstanceState);
  19. setContentView(R.layout.main);
  20. //注册上下文菜单
  21. this.registerForContextMenu(findViewById(R.id.editText1));
  22. this.registerForContextMenu(findViewById(R.id.editText2));
  23. }
  24. //此方法在每次调用上下文菜单时都会被调用一次
  25. publicvoidonCreateContextMenu(ContextMenumenu,Viewv,
  26. ContextMenu.ContextMenuInfomenuInfo){
  27. //为上下文设置标题图标
  28. menu.setHeaderIcon(R.drawable.header);
  29. //若是第一文本框
  30. if(v==findViewById(R.id.editText1)){
  31. //为上下文菜单添加菜单选项
  32. menu.add(0,MENU1,0,R.string.mi1);
  33. menu.add(0,MENU2,0,R.string.mi2);
  34. menu.add(0,MENU3,0,R.string.mi3);
  35. }elseif(v==findViewById(R.id.editText2)){
  36. menu.add(0,MENU4,0,R.string.mi4);
  37. menu.add(0,MENU5,0,R.string.mi5);
  38. }
  39. }
  40. //菜单选项选中状态变化后的回调方法
  41. publicbooleanonContextItemSelected(MenuItemmi){
  42. //判断被选中的MenuItem
  43. switch(mi.getItemId()){
  44. caseMENU1:
  45. caseMENU2:
  46. caseMENU3:
  47. EditTextet1=(EditText)this.findViewById(R.id.editText1);
  48. et1.append("\n"+mi.getTitle()+"被按下");
  49. break;
  50. caseMENU4:
  51. caseMENU5:
  52. EditTextet2=(EditText)this.findViewById(R.id.editText2);
  53. et2.append("\n"+mi.getTitle()+"被按下");
  54. break;
  55. }
  56. returntrue;
  57. }
  58. }
3. 对话框
对话框是Activity运行时显示的小窗口,当显示对话框时,当前Activity失去焦点而由对话框负责所有的人机交互。一般来说,对话框用于提示消息或弹出一个与程序主进程直接相关的小程序。
Android平台下主要有以下几种对话框:
1) 提示对话框 AlertDialog
AlertDialog对话框可以包含若干按钮和一些可选的单选按钮或复选框。
2) 进度对话框
ProgressDialog可以显示进度轮或进度条,由于ProgressDialog继承自AlertDialog,所以其也可以添加按钮。
3) 日期选择对话框DatePickerDialog
4) 时间选择对话框TimePickerDialog

对话框是作为Activity的一部分被创建和显示的,在程序中通过开发回调方法onCreateDialog来完成对话框的创建,该方法需要传入代表对话框id参数。如果需要显示对话框,则调用showDialog方法传入对话框id来显示指定的对话框。

当对话框第一次被显示时,Android会调用onCreateDialog方法来创建对话框实例,之后将不再重复创建该实例。同时,每次对话框再被显示之前都会调用onPrepareDialog方法,如果补充些该方法,那么每次显示的对话框将是最初创建的那个。

关闭对话框可以调用Dialog类的dismiss方法来实现,但是要注意这种方法不会让对话框彻底消失,如果要对话框被关闭后彻底消失,要调用removeDialog方法并传入Dialog的id。

下面通过普通对话框的例子来说明如何使用:

main.xml:
Xml代码 复制代码
  1. <?xmlversion="1.0"encoding="utf-8"?>
  2. <LinearLayoutxmlns:android="http://schemas.android.com/apk/res/android"android:id="@+id/linearLayout1"android:layout_height="fill_parent"android:orientation="vertical"android:layout_width="fill_parent">
  3. <EditTextandroid:layout_width="fill_parent"android:layout_height="wrap_content"android:id="@+id/editText1"android:text="@string/blank"android:editable="false"android:cursorVisible="false"></EditText>
  4. <Buttonandroid:id="@+id/button1"android:layout_height="wrap_content"android:text="@string/btn"android:layout_width="fill_parent"></Button>
  5. </LinearLayout>

Activity:
Java代码 复制代码
  1. packageqijia.si;
  2. importandroid.app.Activity;
  3. importandroid.app.AlertDialog;
  4. importandroid.app.AlertDialog.Builder;
  5. importandroid.app.Dialog;
  6. importandroid.content.DialogInterface;
  7. importandroid.content.DialogInterface.OnClickListener;
  8. importandroid.os.Bundle;
  9. importandroid.view.ContextMenu;
  10. importandroid.view.MenuItem;
  11. importandroid.view.View;
  12. importandroid.widget.Button;
  13. importandroid.widget.EditText;
  14. publicclassContextMenuDemoextendsActivity{
  15. //普通对话框id
  16. finalintCOMMON_DIALOG=1;
  17. /**Calledwhentheactivityisfirstcreated.*/
  18. @Override
  19. publicvoidonCreate(BundlesavedInstanceState){
  20. super.onCreate(savedInstanceState);
  21. setContentView(R.layout.main);
  22. //获取Button对象
  23. Buttonbtn=(Button)findViewById(R.id.button1);
  24. //为Button设置监听器
  25. btn.setOnClickListener(newView.OnClickListener(){
  26. publicvoidonClick(Viewv){
  27. //TODOAuto-generatedmethodstub
  28. showDialog(COMMON_DIALOG);
  29. }
  30. });
  31. }
  32. //重写onCreateDialog方法
  33. publicDialogonCreateDialog(intid){
  34. //声明一个dialog对象用于返回
  35. Dialogdialog=null;
  36. switch(id){
  37. caseCOMMON_DIALOG:
  38. Builderb=newAlertDialog.Builder(this);
  39. //设置对话框图标
  40. b.setIcon(R.drawable.header);
  41. b.setTitle(R.string.btn);
  42. b.setMessage(R.string.dialog_msg);
  43. //添加按钮
  44. b.setPositiveButton(R.string.ok,
  45. newOnClickListener(){
  46. publicvoidonClick(DialogInterfacedialog,intwhich){
  47. //TODOAuto-generatedmethodstub
  48. EditTextet=(EditText)findViewById(R.id.editText1);
  49. et.setText(R.string.dialog_msg);
  50. }
  51. });
  52. dialog=b.create();
  53. break;
  54. default:
  55. break;
  56. }
  57. returndialog;
  58. }
  59. }
Android开发05—Android常用高级控件(下)

1. 滑块与进度条
1) ProgressBar类
ProgressBar类同样位于android.widget包下,但其继承自View,主要用于显示一些操作的进度。应用程序可以修改其长度表示当前后台操作的完成情况。因为进度条会移动,所以长时间加载某些资源或者执行某些耗时的操作时,不会使用户界面失去响应。

2) SeekBar类
SeekBar类继承自ProgressBar,是用来接收用户输入的控件。SeekBar类似于拖拉条,可以直观地显示用户需要的数据,常用于音量调节等场合。
3) 实例:
main.xml
Xml代码 复制代码
  1. <?xmlversion="1.0"encoding="utf-8"?>
  2. <LinearLayoutxmlns:android="http://schemas.android.com/apk/res/android"android:layout_width="fill_parent"android:layout_height="fill_parent"android:orientation="vertical"android:id="@+id/linearLayout1">
  3. <ProgressBarandroid:id="@+id/progressBar1"android:layout_height="wrap_content"android:layout_width="fill_parent"android:max="100"android:progress="20"style="@android:style/Widget.ProgressBar.Horizontal"></ProgressBar>
  4. <RatingBarandroid:layout_height="wrap_content"android:layout_width="wrap_content"android:max="5"android:rating="1"android:id="@+id/ratingBar1"></RatingBar>
  5. </LinearLayout>

Activity:
Java代码 复制代码
  1. packageqijia.si;
  2. importandroid.app.Activity;
  3. importandroid.os.Bundle;
  4. importandroid.widget.ProgressBar;
  5. importandroid.widget.RatingBar;
  6. publicclassJavaTestextendsActivity{
  7. /**Calledwhentheactivityisfirstcreated.*/
  8. finalstaticdoubleMAX=100;
  9. finalstaticdoubleMAX_STAR=5;
  10. @Override
  11. publicvoidonCreate(BundlesavedInstanceState){
  12. super.onCreate(savedInstanceState);
  13. setContentView(R.layout.main);
  14. RatingBarrb=(RatingBar)findViewById(R.id.ratingBar1);
  15. rb.setOnRatingBarChangeListener(newRatingBar.OnRatingBarChangeListener(){
  16. publicvoidonRatingChanged(RatingBarratingBar,floatrating,
  17. booleanfromUser){
  18. //TODOAuto-generatedmethodstub
  19. ProgressBarpb=(ProgressBar)findViewById(R.id.progressBar1);
  20. RatingBarrb=(RatingBar)findViewById(R.id.ratingBar1);
  21. floatrate=rb.getRating();
  22. pb.setProgress((int)(rate/MAX_STAR*MAX));
  23. }
  24. });
  25. }
  26. }
2. 选项卡
TabHost类位于android.widget包下,是选项卡的封装类,用于创建创建选项卡的窗口。TabHost继承自FrameLayout是帧布局的一种。
实例:
main.xml:
Xml代码 复制代码
  1. <?xmlversion="1.0"encoding="utf-8"?>
  2. <FrameLayoutandroid:id="@+id/frameLayout1"android:layout_width="fill_parent"android:layout_height="fill_parent"xmlns:android="http://schemas.android.com/apk/res/android">
  3. <LinearLayoutandroid:id="@+id/linearLayout1"android:layout_width="fill_parent"android:layout_height="fill_parent"android:gravity="center_horizontal"android:orientation="vertical">
  4. <ImageViewandroid:layout_height="wrap_content"android:layout_width="wrap_content"android:id="@+id/imageView1"android:scaleType="fitXY"android:layout_gravity="center"android:src="@drawable/andy"></ImageView>
  5. <TextViewandroid:layout_height="wrap_content"android:id="@+id/textView1"android:layout_width="wrap_content"android:text="@string/andy"android:textSize="24dip"></TextView>
  6. </LinearLayout>
  7. <LinearLayoutandroid:layout_width="fill_parent"android:layout_height="fill_parent"android:gravity="center_horizontal"android:orientation="vertical"android:id="@+id/linearLayout2">
  8. <ImageViewandroid:layout_width="wrap_content"android:layout_height="wrap_content"android:id="@+id/imageView2"android:scaleType="fitXY"android:layout_gravity="center"android:src="@drawable/bill"></ImageView>
  9. <TextViewandroid:layout_height="wrap_content"android:text="@string/bill"android:layout_width="wrap_content"android:id="@+id/textView2"android:textSize="24dip"></TextView>
  10. </LinearLayout>
  11. <LinearLayoutandroid:layout_width="fill_parent"android:orientation="vertical"android:gravity="center_horizontal"android:layout_height="fill_parent"android:id="@+id/linearLayout3">
  12. <ImageViewandroid:layout_height="wrap_content"android:layout_width="wrap_content"android:id="@+id/imageView3"android:scaleType="fitXY"android:layout_gravity="center"android:src="@drawable/torvalds"></ImageView>
  13. <TextViewandroid:layout_height="wrap_content"android:text="@string/linus"android:layout_width="wrap_content"android:id="@+id/textView3"android:textSize="24dip"></TextView>
  14. </LinearLayout>
  15. </FrameLayout>


Activity:
Java代码 复制代码
  1. packageqijia.si;
  2. importandroid.app.Activity;
  3. importandroid.app.TabActivity;
  4. importandroid.os.Bundle;
  5. importandroid.view.LayoutInflater;
  6. importandroid.widget.ProgressBar;
  7. importandroid.widget.RatingBar;
  8. importandroid.widget.TabHost;
  9. publicclassJavaTestextendsTabActivity{
  10. /**Calledwhentheactivityisfirstcreated.*/
  11. privateTabHostmyTabhost;
  12. @Override
  13. publicvoidonCreate(BundlesavedInstanceState){
  14. super.onCreate(savedInstanceState);
  15. myTabhost=this.getTabHost();
  16. LayoutInflater.from(this).inflate(R.layout.main,myTabhost.getTabContentView(),true);
  17. myTabhost.addTab(myTabhost.newTabSpec("选项卡1").setIndicator("选项卡1",getResources().getDrawable(R.drawable.png1)).setContent(R.id.linearLayout1));
  18. myTabhost.addTab(myTabhost.newTabSpec("选项卡2").setIndicator("选项卡2",getResources().getDrawable(R.drawable.png2)).setContent(R.id.linearLayout2));
  19. myTabhost.addTab(myTabhost.newTabSpec("选项卡3").setIndicator("选项卡3",getResources().getDrawable(R.drawable.png3)).setContent(R.id.linearLayout3));
  20. }
  21. }
3. 画廊控件
Gallery是一种水平滚动的列表,一般情况下用来显示图片等资源,可以使图片在屏幕上滑来滑去。Gallery所显示的图片资源同样来自适配器。
Gallery是View的子类,Gallery控件可以在XML中配置,也可以再代码中操作。
实例:将需要显示的控件存放在BaseAdapter中,然后在适当的时间将此BaseAdapter设置Gallery控件使之显示。

main.xml
Xml代码 复制代码
  1. <?xmlversion="1.0"encoding="utf-8"?>
  2. <LinearLayoutxmlns:android="http://schemas.android.com/apk/res/android"android:layout_width="fill_parent"android:layout_height="fill_parent"android:orientation="vertical"android:id="@+id/linearLayout1"android:gravity="center_vertical">
  3. <Galleryandroid:layout_height="wrap_content"android:layout_width="fill_parent"android:id="@+id/gallery1"android:spacing="10dip"android:unselectedAlpha="1"></Gallery>
  4. </LinearLayout>


Activity:
Java代码 复制代码
  1. packageqijia.si;
  2. importandroid.app.Activity;
  3. importandroid.app.TabActivity;
  4. importandroid.os.Bundle;
  5. importandroid.view.LayoutInflater;
  6. importandroid.view.View;
  7. importandroid.view.ViewGroup;
  8. importandroid.widget.AdapterView;
  9. importandroid.widget.AdapterView.OnItemClickListener;
  10. importandroid.widget.BaseAdapter;
  11. importandroid.widget.Gallery;
  12. importandroid.widget.ImageView;
  13. importandroid.widget.ProgressBar;
  14. importandroid.widget.RatingBar;
  15. importandroid.widget.TabHost;
  16. publicclassJavaTestextendsActivity{
  17. /**Calledwhentheactivityisfirstcreated.*/
  18. int[]imageIds={
  19. R.drawable.bbta,R.drawable.bbtb,R.drawable.bbtc,
  20. R.drawable.bbtd,R.drawable.bbte,R.drawable.bbtf,
  21. R.drawable.bbtg
  22. };
  23. @Override
  24. publicvoidonCreate(BundlesavedInstanceState){
  25. super.onCreate(savedInstanceState);
  26. setContentView(R.layout.main);
  27. Galleryg1=(Gallery)this.findViewById(R.id.gallery1);
  28. BaseAdapterba=newBaseAdapter(){
  29. publicintgetCount(){
  30. //TODOAuto-generatedmethodstub
  31. returnimageIds.length;
  32. }
  33. publicObjectgetItem(intposition){
  34. //TODOAuto-generatedmethodstub
  35. returnnull;
  36. }
  37. publiclonggetItemId(intposition){
  38. //TODOAuto-generatedmethodstub
  39. return0;
  40. }
  41. publicViewgetView(intposition,ViewconvertView,ViewGroupparent){
  42. //TODOAuto-generatedmethodstub
  43. ImageViewiv=newImageView(JavaTest.this);
  44. iv.setImageResource(imageIds[position]);
  45. iv.setScaleType(ImageView.ScaleType.FIT_XY);
  46. iv.setLayoutParams(newGallery.LayoutParams(188,250));
  47. returniv;
  48. }
  49. };
  50. g1.setAdapter(ba);
  51. g1.setOnItemClickListener(
  52. newOnItemClickListener(){
  53. publicvoidonItemClick(AdapterView<?>arg0,Viewarg1,
  54. intarg2,longarg3){
  55. //TODOAuto-generatedmethodstub
  56. Galleryg1=(Gallery)findViewById(R.id.gallery1);
  57. g1.setSelection(arg2);
  58. }
  59. });
  60. }
  61. }

Android开发04—Android常用高级控件(上)

1. 自动完成文本框
AutoCompleteTextView类继承自EditText类。自动完成文本框的外观与文本框没什么区别,只是当用户输入某些文字时,会自动出现下拉菜单显示与输入文字相关的信息。
自动完成文本框可以在XML文件中使用属性进行设置,也可以在Java代码中通过方法进行设置。

实例:
main.xml:
Xml代码 复制代码
  1. <?xmlversion="1.0"encoding="utf-8"?>
  2. <LinearLayoutxmlns:android="http://schemas.android.com/apk/res/android"android:layout_width="fill_parent"android:layout_height="fill_parent"android:orientation="vertical"android:id="@+id/linearLayout1">
  3. <AutoCompleteTextViewandroid:text="AutoCompleteTextView"android:layout_width="fill_parent"android:layout_height="wrap_content"android:id="@+id/autoCompleteTextView1"></AutoCompleteTextView>
  4. </LinearLayout>

Activity:
Java代码 复制代码
  1. packageqijia.si;
  2. importandroid.app.Activity;
  3. importandroid.graphics.drawable.AnimationDrawable;
  4. importandroid.os.Bundle;
  5. importandroid.view.View;
  6. importandroid.view.View.OnClickListener;
  7. importandroid.widget.ArrayAdapter;
  8. importandroid.widget.AutoCompleteTextView;
  9. importandroid.widget.Button;
  10. importandroid.widget.ImageView;
  11. publicclassMyAndroidProjectextendsActivity{
  12. privatestaticfinalString[]myStr=newString[]{
  13. "aaa","bbb","ccc","aab","aac","aad"
  14. };//常量数组用于提示
  15. /**Calledwhentheactivityisfirstcreated.*/
  16. @Override
  17. publicvoidonCreate(BundlesavedInstanceState){
  18. super.onCreate(savedInstanceState);
  19. setContentView(R.layout.main);
  20. ArrayAdapter<String>aa=newArrayAdapter<String>(
  21. this,
  22. android.R.layout.simple_dropdown_item_1line,
  23. myStr
  24. );//使用适配器
  25. AutoCompleteTextViewmyAuto=(AutoCompleteTextView)findViewById(R.id.autoCompleteTextView1);
  26. myAuto.setAdapter(aa);
  27. myAuto.setThreshold(1);
  28. }
  29. }

2. 滚动视图
ScrollView类继承自FrameLayout类。ScrollView其实是一个帧布局,一般情况下,其中的控件是按照线性进行布局的,用户可以对其进行滚动。
ScrollView既可以在XML中配置也可在Java代码中进行配置。
ScrollView的使用方法比较简单,只需要将需要滚动的控件添加到ScrollView中即可。ScrollView同一时刻只能包含一个View。

具体方法:
Java代码 复制代码
  1. packageqijia.si;
  2. importandroid.app.Activity;
  3. importandroid.graphics.drawable.AnimationDrawable;
  4. importandroid.os.Bundle;
  5. importandroid.view.View;
  6. importandroid.view.View.OnClickListener;
  7. importandroid.widget.ArrayAdapter;
  8. importandroid.widget.AutoCompleteTextView;
  9. importandroid.widget.Button;
  10. importandroid.widget.ImageView;
  11. importandroid.widget.ScrollView;
  12. importandroid.widget.TextView;
  13. publicclassMyAndroidProjectextendsActivity{
  14. ScrollViewscrollView;
  15. Stringmsg="我是字符串,我很长很长!我是字符串,我很长很长!";
  16. Stringstr="";
  17. /**Calledwhentheactivityisfirstcreated.*/
  18. @Override
  19. publicvoidonCreate(BundlesavedInstanceState){
  20. super.onCreate(savedInstanceState);
  21. setContentView(R.layout.main);
  22. scrollView=newScrollView(this);
  23. TextViewtv=newTextView(this);
  24. tv.setTextSize(23);
  25. for(inti=0;i<10;i++){
  26. str=str+msg;
  27. }
  28. tv.setText(str);
  29. scrollView.addView(tv);
  30. setContentView(scrollView);
  31. }
  32. }


3. 列表视图
ListView类是一种列表视图,将ListAdapter所提供的各个控件显示在一个垂直且可以滚动的列表中。
该类使用方法简单,只需要先初始化所需要得数据,然后创建适配器并将其设置给ListView,ListView便将信息以列表的形式显示到页面中。

使用案例:

string.xml
Xml代码 复制代码
  1. <?xmlversion="1.0"encoding="utf-8"?>
  2. <resources>
  3. <stringname="app_name">AbsoluteExample</string>
  4. <stringname="hello">您选择了</string>
  5. <stringname="andy">AndyRubin\nAndroid的创造者之一</string>
  6. <stringname="Bill">BillJoy\nJava创造者之一</string>
  7. <stringname="edgar">EdgarF.Codd\n关系数据库之父</string>
  8. <stringname="torvalds">LinusTorvalds\nLinux之父</string>
  9. <stringname="turing">TuringAlan\nIT的祖师爷</string>
  10. <stringname="ys">您选择了</string>
  11. </resources>

colors.xml
Xml代码 复制代码
  1. <?xmlversion="1.0"encoding="utf-8"?>
  2. <resources>
  3. <colorname="white">#ffffff</color>
  4. <colorname="red">#ff0000</color>
  5. <colorname="black">#000000</color>
  6. <colorname="green">#00ff00</color>
  7. <colorname="gray">#050505</color>
  8. <colorname="blue">#0000ff</color>
  9. </resources>
main.xml
Xml代码 复制代码
  1. <?xmlversion="1.0"encoding="utf-8"?>
  2. <LinearLayoutxmlns:android="http://schemas.android.com/apk/res/android"android:orientation="vertical"android:id="@+id/linearLayout1"android:layout_width="fill_parent"android:layout_height="fill_parent">
  3. <TextViewandroid:layout_height="wrap_content"android:id="@+id/textView1"android:layout_width="fill_parent"android:textSize="24dip"android:textColor="@color/white"android:text="@string/hello"></TextView>
  4. <ListViewandroid:layout_height="wrap_content"android:layout_width="fill_parent"android:id="@+id/listView1"android:choiceMode="singleChoice"></ListView>
  5. </LinearLayout>





Activity:
Java代码 复制代码
  1. packageqijia.si;
  2. importandroid.app.Activity;
  3. importandroid.graphics.drawable.AnimationDrawable;
  4. importandroid.os.Bundle;
  5. importandroid.view.View;
  6. importandroid.view.View.OnClickListener;
  7. importandroid.view.ViewGroup;
  8. importandroid.widget.AdapterView;
  9. importandroid.widget.AdapterView.OnItemClickListener;
  10. importandroid.widget.AdapterView.OnItemSelectedListener;
  11. importandroid.widget.ArrayAdapter;
  12. importandroid.widget.AutoCompleteTextView;
  13. importandroid.widget.BaseAdapter;
  14. importandroid.widget.Button;
  15. importandroid.widget.Gallery;
  16. importandroid.widget.ImageView;
  17. importandroid.widget.LinearLayout;
  18. importandroid.widget.ListView;
  19. importandroid.widget.ScrollView;
  20. importandroid.widget.TextView;
  21. publicclassMyAndroidProjectextendsActivity{
  22. int[]drawableIds={R.drawable.andy,R.drawable.bill,R.drawable.edgar,R.drawable.torvalds,
  23. R.drawable.turing};
  24. int[]msgIds={R.string.andy,R.string.Bill,R.string.edgar,R.string.torvalds,R.string.turing};
  25. /**Calledwhentheactivityisfirstcreated.*/
  26. @Override
  27. publicvoidonCreate(BundlesavedInstanceState){
  28. super.onCreate(savedInstanceState);
  29. setContentView(R.layout.main);
  30. ListViewlv=(ListView)findViewById(R.id.listView1);
  31. BaseAdapterba=newBaseAdapter(){
  32. publicintgetCount(){return5;}
  33. publicObjectgetItem(intarg0){returnnull;}
  34. publiclonggetItemId(intarg0){return0;}
  35. publicViewgetView(intarg0,Viewarg1,ViewGrouparg2){
  36. //动态生成每一个下拉项对应的View,每个下拉项View由LinearLayout
  37. //中包含一个ImageView及一个TextView,由代码动态生成
  38. LinearLayoutll=newLinearLayout(MyAndroidProject.this);
  39. ll.setOrientation(LinearLayout.HORIZONTAL);
  40. ll.setPadding(5,5,5,5);
  41. ImageViewii=newImageView(MyAndroidProject.this);
  42. ii.setImageDrawable(getResources().getDrawable(drawableIds[arg0]));
  43. ii.setScaleType(ImageView.ScaleType.FIT_XY);
  44. ii.setLayoutParams(newGallery.LayoutParams(100,98));
  45. ll.addView(ii);
  46. TextViewtv=newTextView(MyAndroidProject.this);
  47. tv.setText(getResources().getText(msgIds[arg0]));
  48. tv.setTextSize(24);
  49. tv.setTextColor(MyAndroidProject.this.getResources().getColor(R.color.white));
  50. tv.setPadding(5,5,5,5);
  51. ll.addView(tv);
  52. returnll;
  53. }
  54. };
  55. lv.setAdapter(ba);
  56. lv.setOnItemSelectedListener(newOnItemSelectedListener(){
  57. publicvoidonItemSelected(AdapterView<?>arg0,Viewarg1,intarg2,longarg3){
  58. TextViewtv=(TextView)findViewById(R.id.textView1);
  59. LinearLayoutll=(LinearLayout)arg1;
  60. TextViewtvn=(TextView)ll.getChildAt(1);
  61. StringBuildersb=newStringBuilder();
  62. sb.append(getResources().getText(R.string.ys));
  63. sb.append(":");
  64. sb.append(tvn.getText());
  65. Stringstemp=sb.toString();
  66. tv.setText(stemp.split("\\n")[0]);
  67. }
  68. publicvoidonNothingSelected(AdapterView<?>arg0){}
  69. });
  70. lv.setOnItemClickListener(newOnItemClickListener(){
  71. publicvoidonItemClick(AdapterView<?>arg0,Viewarg1,intarg2,longarg3){
  72. TextViewtv=(TextView)findViewById(R.id.textView1);
  73. LinearLayoutll=(LinearLayout)arg1;
  74. TextViewtvn=(TextView)ll.getChildAt(1);
  75. StringBuildersb=newStringBuilder();
  76. sb.append(getResources().getText(R.string.ys));
  77. sb.append(":");
  78. sb.append(tvn.getText());
  79. Stringstemp=sb.toString();
  80. tv.setText(stemp.split("\\n")[0]);
  81. }
  82. });
  83. }
  84. }
4. 网络视图
GridView类同样位于android.widget包下。该视图将其空间以二维格式显示到表格中,而这些控件全部来自于ListAdapter适配器。
strings.xml
Xml代码 复制代码
  1. <?xmlversion="1.0"encoding="utf-8"?>
  2. <resources>
  3. <stringname="hello">当前无选中选项</string>
  4. <stringname="app_name">Sample_5_3</string>
  5. <stringname="andy">AndyRubin</string>
  6. <stringname="bill">BillJoy</string>
  7. <stringname="edgar">EdgarF.Codd</string>
  8. <stringname="torvalds">LinusTorvalds</string>
  9. <stringname="turing">TuringAlan</string>
  10. <stringname="andydis">Android的创造者</string>
  11. <stringname="billdis">Java创造者之一</string>
  12. <stringname="edgardis">关系数据库之父</string>
  13. <stringname="torvaldsdis">Linux之父</string>
  14. <stringname="turingdis">IT的祖师爷</string>
  15. </resources>

colors.xml
Xml代码 复制代码
  1. <?xmlversion="1.0"encoding="utf-8"?>
  2. <resources>
  3. <colorname="red">#fd8d8d</color>
  4. <colorname="green">#9cfda3</color>
  5. <colorname="blue">#8d9dfd</color>
  6. <colorname="white">#FFFFFF</color>
  7. <colorname="black">#000000</color>
  8. <colorname="gray">#050505</color>
  9. </resources>
main.xml
Xml代码 复制代码
  1. <?xmlversion="1.0"encoding="utf-8"?>
  2. <LinearLayoutxmlns:android="http://schemas.android.com/apk/res/android"
  3. android:orientation="vertical"
  4. android:layout_width="fill_parent"
  5. android:layout_height="fill_parent">
  6. <TextView
  7. android:id="@+id/TextView01"
  8. android:layout_width="fill_parent"
  9. android:layout_height="wrap_content"
  10. android:text="@string/hello"
  11. android:textColor="@color/white"
  12. android:textSize="24dip"/>
  13. <GridView
  14. android:id="@+id/GridView01"
  15. android:layout_width="fill_parent"
  16. android:layout_height="fill_parent"
  17. android:verticalSpacing="5dip"
  18. android:horizontalSpacing="5dip"
  19. android:stretchMode="columnWidth"/>
  20. </LinearLayout>
grid_row.xml
Xml代码 复制代码
  1. <?xmlversion="1.0"encoding="utf-8"?>
  2. <LinearLayout
  3. android:id="@+id/LinearLayout01"
  4. android:layout_width="fill_parent"
  5. android:layout_height="wrap_content"
  6. android:orientation="horizontal"
  7. xmlns:android="http://schemas.android.com/apk/res/android">
  8. <ImageView
  9. android:id="@+id/ImageView01"
  10. android:scaleType="fitXY"
  11. android:layout_width="100dip"
  12. android:layout_height="98dip"/>
  13. <TextView
  14. android:id="@+id/TextView02"
  15. android:layout_width="100dip"
  16. android:layout_height="wrap_content"
  17. android:textColor="@color/white"
  18. android:textSize="24dip"
  19. android:paddingLeft="5dip"/>
  20. <TextView
  21. android:id="@+id/TextView03"
  22. android:layout_width="wrap_content"
  23. android:layout_height="wrap_content"
  24. android:textColor="@color/white"
  25. android:textSize="24dip"
  26. android:paddingLeft="5dip"/>
  27. </LinearLayout>
Activity:

Java代码 复制代码
  1. packageqijia.si;
  2. importjava.util.ArrayList;
  3. importjava.util.HashMap;
  4. importjava.util.List;
  5. importjava.util.Map;
  6. importandroid.app.Activity;
  7. importandroid.os.Bundle;
  8. importandroid.view.View;
  9. importandroid.widget.AdapterView;
  10. importandroid.widget.GridView;
  11. importandroid.widget.LinearLayout;
  12. importandroid.widget.SimpleAdapter;
  13. importandroid.widget.TextView;
  14. importandroid.widget.AdapterView.OnItemClickListener;
  15. importandroid.widget.AdapterView.OnItemSelectedListener;
  16. publicclassGridDemoextendsActivity{
  17. //所有资源图片(andy、bill、edgar、torvalds、turing)id的数组
  18. int[]drawableIds=
  19. {R.drawable.andy,R.drawable.bill,R.drawable.edgar,R.drawable.torvalds,R.drawable.turing};
  20. //所有资源字符串(andy、bill、edgar、torvalds、turing)id的数组
  21. int[]nameIds=
  22. {R.string.andy,R.string.bill,R.string.edgar,R.string.torvalds,R.string.turing};
  23. int[]msgIds=
  24. {R.string.andydis,R.string.billdis,R.string.edgardis,
  25. R.string.torvaldsdis,R.string.turingdis};
  26. publicList<?extendsMap<String,?>>generateDataList(){
  27. ArrayList<Map<String,Object>>list=newArrayList<Map<String,Object>>();;
  28. introwCounter=drawableIds.length;//得到表格的行数
  29. for(inti=0;i<rowCounter;i++){//循环生成每行的包含对应各个列数据的Map;col1、col2、col3为列名
  30. HashMap<String,Object>hmap=newHashMap<String,Object>();
  31. hmap.put("col1",drawableIds[i]);//第一列为图片
  32. hmap.put("col2",this.getResources().getString(nameIds[i]));//第二例为姓名
  33. hmap.put("col3",this.getResources().getString(msgIds[i]));//第三列为描述
  34. list.add(hmap);
  35. }
  36. returnlist;
  37. }
  38. publicvoidonCreate(BundlesavedInstanceState){
  39. super.onCreate(savedInstanceState);
  40. setContentView(R.layout.main);
  41. GridViewgv=(GridView)this.findViewById(R.id.GridView01);
  42. SimpleAdaptersca=newSimpleAdapter(
  43. this,
  44. generateDataList(),//数据List
  45. R.layout.grid_row,//行对应layoutid
  46. newString[]{"col1","col2","col3"},//列名列表
  47. newint[]{R.id.ImageView01,R.id.TextView02,R.id.TextView03}//列对应控件id列表
  48. );
  49. gv.setAdapter(sca);//为GridView设置数据适配器
  50. gv.setOnItemSelectedListener(//设置选项选中的监听器
  51. newOnItemSelectedListener(){
  52. publicvoidonItemSelected(AdapterView<?>arg0,Viewarg1,
  53. intarg2,longarg3){//重写选项被选中事件的处理方法
  54. TextViewtv=(TextView)findViewById(R.id.TextView01);//获取主界面TextView
  55. LinearLayoutll=(LinearLayout)arg1;//获取当前选中选项对应的LinearLayout
  56. TextViewtvn=(TextView)ll.getChildAt(1);//获取其中的TextView
  57. TextViewtvnL=(TextView)ll.getChildAt(2);//获取其中的TextView
  58. StringBuildersb=newStringBuilder();
  59. sb.append(tvn.getText());//获取姓名信息
  60. sb.append("");
  61. sb.append(tvnL.getText());//获取描述信息
  62. tv.setText(sb.toString());//信息设置进主界面TextView
  63. }
  64. publicvoidonNothingSelected(AdapterView<?>arg0){}
  65. }
  66. );
  67. gv.setOnItemClickListener(//设置选项被单击的监听器
  68. newOnItemClickListener(){
  69. publicvoidonItemClick(AdapterView<?>arg0,Viewarg1,intarg2,
  70. longarg3){//重写选项被单击事件的处理方法
  71. TextViewtv=(TextView)findViewById(R.id.TextView01);//获取主界面TextView
  72. LinearLayoutll=(LinearLayout)arg1;//获取当前选中选项对应的LinearLayout
  73. TextViewtvn=(TextView)ll.getChildAt(1);//获取其中的TextView
  74. TextViewtvnL=(TextView)ll.getChildAt(2);//获取其中的TextView
  75. StringBuildersb=newStringBuilder();
  76. sb.append(tvn.getText());//获取姓名信息
  77. sb.append("");
  78. sb.append(tvnL.getText());//获取描述信息
  79. tv.setText(sb.toString());//信息设置进主界面TextView
  80. }
  81. }
  82. );
  83. }
  84. }
Android的adapter总结和深入研究

Adapter是把数据和用户界面视图绑定的桥梁类。Adapter负责创建用来表示父视图中的每一个条目的子视图,并且提供对底层数据的访问。
支持Adapter绑定的用户界面必须对AdapterView抽象类进行拓展。也可以创建新的由AdapterView派生的控件,创建新的Adapter类绑定他们。

两个最通用的本地Adapter
ArrayAdapter ArrayAdapter使用泛型来把Adapter视图(View)绑定到一个指定类的对象的数组。默认情况下,ArrayAdapter使用数组中每个对象的toString值来创建和填充文本视图。

SimpleCursorAdapter SimpleCursorAdapter可以把一个布局中指定的视图和内容提供器查询返回的游标列绑定在一起。
定制ArrayAdapter
默认情况下,ArrayAdapter将使用它绑定到的对象数组的toString值来填充指定布局中可用的TextView
最常见的例子便是ListView
我们在定义一个ListView组件后,在向ListView组件装载数据之前需要创建一个Adapter对象,

ArrayAdapter<String> AdapterName = new ArrayAdapter<String>(
this,
android.R.layout.simple_list_itme_1,
data);
以上代码中创建了一个android.wedget.ArrayAdapter 对象。ArrayAdapter类的构造方法需要一个android.content.Context对象,因此传入当前Activity的对象实例(this)作为ArrayAdapter类的构造方法的第一个参数值。除此之外,ArrayAdapter还需要完成:
指定列表项模板(第二个参数)
指定列表项中的数据。(第三个参数)
在创建玩Adapter对象后,调用setAdapter方法,ListView组件的每一个列表都会使用simple_list_item_1.xml文件定义的模板来显示,并将data数组中的每一个元素复制给每一个列表项

下面我们来讨论下定制ArrayAdapter
在大多数情况下,我们都要定制用于表示每个视图的布局。所以,需要使用特定类型的变体Adapter来扩展ArrayAdapter,并重写getView方法来向布局视图分配对象属性。
getView方法用于构造,扩充和填充将在父AdapterView类(如ListView)中显示的视图,该父AdapterView类使用这个Adapter绑定到底层的数组。
getView方法接收描述要显示的条目的位置,要更新的视图(或null),以及将包含这个视图的视图组作为参数。调用getItem将返回存储在底层数组的指定索引位置的值。
例如对与ListView来说:
自定义的Adapter类一般需要从android.widget.BaseAdapter类继承。在BaseAdapter类中有两个重要方法:getView和getCount。其中ListView在显示某一列表项是会调用getView方法返回要显示的列表项的View对象,这一点我们上面已经提到了。getCount方法返回当前ListView组件中列表项的总数。
注意:列表项的View对象一定要在getView方法中创建。不能事先创建好View对象,然后再getView方法中返回这些View对象。如果这样做,系统可能会出现一些异常现象

Android开发03—Android常用基本控件(下)



1. 单选按钮和复选按钮
CheckBox和RadioButton控件都只有选中和未选中两种状态,不同的是RadioButton是单选按钮,需要便知道RadioGroup中,同一时刻一个RadioGroup中只能有一个按钮处于选中状态。

使用举例:
Java代码 复制代码
  1. publicclassSampleDemoextendsActivity
  2. {
  3. publicvoidonCreate(BundlesavedInstanceState){
  4. super.onCreate(savedInstatceState);
  5. setContentView(R.layout.main);
  6. CheckBoxcb=(CheckBox)findViewById(R.id.CheckBox1);
  7. cb.setOnCheckedChangeListener(newOnCheckedChangeListener(){
  8. publicvoidonCheckedChanged(CompoundButtonbuttonView,booleanisChecked){
  9. setBulbState(isChecked);
  10. }
  11. });
  12. RadioButtonrb=(RadioButton)findViewById(R.id.off);
  13. rb.setOnCheckedChangeListener(newOnCheckedChangeListener(){
  14. publicvoidonCheckedChanged(CompoundButtonbuttonView,booleanisChecked){
  15. setBulbState(isChecked);
  16. }
  17. });
  18. }
  19. publicvoidsetBulbState(booleanstate){
  20. ImageViewiv=(ImageView)findViewById(R.id.ImageView01);
  21. iv.setImageResource((state)?R.drawable.bulb_on:R.drawable.bulb_off);
  22. CheckBoxcb=(CheckBox)this.findViewById(R.id.CheckBox1);
  23. cb.setText((state)?R.string.off:R.string.on);
  24. cb.setChecked(state);
  25. RadioButtonrb=(RadioButton)this.findViewById(R.id.off);
  26. rb.setChecked(state);
  27. rb=(RadioButton)findViewById(R.id.on);
  28. rb.setChecked(state);
  29. }
  30. }
2. 图片控件
ImageView控件负责显示图片,其图片的来源既可以是资源文件的id,也可以是Drawable对象或BitMap对象,还可以是ContentProvider的Uri。

实例:ImageView的用法
string.xml
Xml代码 复制代码
  1. <?xmlversion="1.0"encoding="utf-8"?>
  2. lt;resources>
  3. <stringname="app_name">AbsoluteExample</string>
  4. <stringname="next">下一张</string>
  5. <stringname="previous">上一张</string>
  6. <stringname="alpha_plus">透明度增加</string>
  7. <stringname="alpha_minus">透明度减少</string>
  8. lt;/resources>
main.xml
Xml代码 复制代码
  1. <?xmlversion="1.0"encoding="utf-8"?>
  2. <LinearLayoutxmlns:android="http://schemas.android.com/apk/res/android"android:id="@+id/linearLayout1"android:layout_width="fill_parent"android:layout_height="fill_parent"android:orientation="vertical">
  3. <ImageViewandroid:layout_width="wrap_content"android:layout_height="wrap_content"android:id="@+id/iv"android:layout_gravity="center_horizontal"android:src="@drawable/pic1"></ImageView>
  4. <LinearLayoutandroid:layout_height="wrap_content"android:id="@+id/linearLayout2"android:layout_gravity="center_horizontal"android:layout_width="fill_parent"android:orientation="horizontal">
  5. <Buttonandroid:layout_height="wrap_content"android:layout_width="wrap_content"android:id="@+id/previous"android:text="@string/previous"android:gravity="center_horizontal"android:layout_gravity="center_horizontal"></Button>
  6. <Buttonandroid:layout_height="wrap_content"android:id="@+id/alpha_plus"android:layout_width="wrap_content"android:text="@string/alpha_plus"android:gravity="center_horizontal"android:layout_gravity="center_horizontal"></Button>
  7. <Buttonandroid:layout_height="wrap_content"android:id="@+id/alpha_minus"android:layout_width="wrap_content"android:text="@string/alpha_minus"android:gravity="center_horizontal"android:layout_gravity="center_horizontal"></Button>
  8. <Buttonandroid:layout_height="wrap_content"android:id="@+id/next"android:layout_width="wrap_content"android:text="@string/next"android:gravity="center_horizontal"android:layout_gravity="center_horizontal"></Button>
  9. </LinearLayout>
  10. </LinearLayout>

Activity部分代码:
Java代码 复制代码
  1. importandroid.app.Activity;
  2. importandroid.os.Bundle;
  3. importandroid.view.View;
  4. importandroid.widget.Button;
  5. importandroid.widget.EditText;
  6. importandroid.widget.ImageView;
  7. publicclassMyAndroidProjectextendsActivity{
  8. ImageViewiv;
  9. ButtonbtnNext;
  10. ButtonbtnPrevious;
  11. ButtonbtnAlphaPlus;
  12. ButtonbtnAlphaMinus;
  13. intcurrImgId=0;
  14. intalpha=255;
  15. int[]imgId={
  16. R.drawable.pic1,
  17. R.drawable.pic2,
  18. R.drawable.pic3
  19. };
  20. privateView.OnClickListenermyListener=newView.OnClickListener(){
  21. publicvoidonClick(Viewv){
  22. //TODOAuto-generatedmethodstub
  23. if(v==btnNext){
  24. currImgId=(currImgId+1)%imgId.length;
  25. iv.setImageResource(imgId[currImgId]);
  26. }
  27. elseif(v==btnPrevious){
  28. currImgId=(currImgId-1+imgId.length)%imgId.length;
  29. iv.setImageResource(imgId[currImgId]);
  30. }
  31. elseif(v==btnAlphaPlus){
  32. alpha-=25;
  33. if(alpha<0){
  34. alpha=0;
  35. }
  36. iv.setAlpha(alpha);
  37. }
  38. elseif(v==btnAlphaMinus){
  39. alpha+=25;
  40. if(alpha>255){
  41. alpha=255;
  42. }
  43. iv.setAlpha(alpha);
  44. }
  45. }
  46. };
  47. /**Calledwhentheactivityisfirstcreated.*/
  48. @Override
  49. publicvoidonCreate(BundlesavedInstanceState){
  50. super.onCreate(savedInstanceState);
  51. setContentView(R.layout.main);
  52. iv=(ImageView)findViewById(R.id.iv);
  53. btnNext=(Button)findViewById(R.id.next);
  54. btnPrevious=(Button)findViewById(R.id.previous);
  55. btnAlphaPlus=(Button)findViewById(R.id.alpha_plus);
  56. btnAlphaMinus=(Button)findViewById(R.id.alpha_minus);
  57. btnNext.setOnClickListener(myListener);
  58. btnPrevious.setOnClickListener(myListener);
  59. btnAlphaPlus.setOnClickListener(myListener);
  60. btnAlphaMinus.setOnClickListener(myListener);
  61. }
  62. }

3. 时钟控件
AnalogClock和DigitalClock都负责显示时钟,所不同的是AnalogClock控件显示模拟时钟,且只显示时针和分针,而digital显示数字时钟,可精确到秒
直接在main.xml中定义时钟控件即可。


4. 日期与时间选择控件
DatePicker继承自FrameLayout类,主要功能是向用户提供包含年,月,日的日期数据,并允许用户进行选择。如果要捕获用户修改日期选择控件中数据的时间,需要为DatePicker添加onDateChangedListener监听器。
TimePicker同样继承自FrameLayout类。时间选择控件向用户显示一天中的时间,并允许用户进行选择,如果要捕获用户修改时间数据的时间,便需要为TimePicker添加OnTimeChangedListener监听器。

案例:
main.xml
Xml代码 复制代码
  1. <?xmlversion="1.0"encoding="utf-8"?>
  2. <LinearLayoutxmlns:android="http://schemas.android.com/apk/res/android"android:id="@+id/linearLayout1"android:orientation="vertical"android:layout_width="fill_parent"android:layout_height="fill_parent">
  3. <DatePickerandroid:layout_gravity="center_horizontal"android:layout_width="wrap_content"android:layout_height="wrap_content"android:id="@+id/datePicker1"></DatePicker>
  4. <EditTextandroid:layout_height="wrap_content"android:text="EditText"android:layout_width="fill_parent"android:id="@+id/editText1"android:cursorVisible="false"android:editable="false"></EditText>
  5. <TimePickerandroid:layout_width="wrap_content"android:layout_height="wrap_content"android:id="@+id/timePicker1"android:layout_gravity="center_horizontal"></TimePicker>
  6. <EditTextandroid:layout_height="wrap_content"android:text="EditText"android:layout_width="fill_parent"android:id="@+id/editText2"android:cursorVisible="false"android:editable="false"></EditText>
  7. </LinearLayout>


Activity:
Java代码 复制代码
  1. packageqijia.si;
  2. importjava.util.Calendar;
  3. importandroid.app.Activity;
  4. importandroid.os.Bundle;
  5. importandroid.widget.DatePicker;
  6. importandroid.widget.EditText;
  7. importandroid.widget.DatePicker.OnDateChangedListener;
  8. importandroid.widget.TimePicker;
  9. importandroid.widget.TimePicker.OnTimeChangedListener;
  10. publicclassMyAndroidProjectextendsActivity{
  11. /**Calledwhentheactivityisfirstcreated.*/
  12. @Override
  13. publicvoidonCreate(BundlesavedInstanceState){
  14. super.onCreate(savedInstanceState);
  15. setContentView(R.layout.main);
  16. DatePickerdp=(DatePicker)findViewById(R.id.datePicker1);
  17. TimePickertp=(TimePicker)findViewById(R.id.timePicker1);
  18. Calendarc=Calendar.getInstance();
  19. intyear=c.get(Calendar.YEAR);
  20. intmonthOfYear=c.get(Calendar.MONTH);
  21. intdayOfMonth=c.get(Calendar.DAY_OF_MONTH);
  22. dp.init(year,monthOfYear,dayOfMonth,newOnDateChangedListener(){
  23. publicvoidonDateChanged(DatePickerview,intyear,intmonthOfYear,intdayOfMonth){
  24. flushDate(year,monthOfYear,dayOfMonth);
  25. }
  26. });
  27. tp.setOnTimeChangedListener(newOnTimeChangedListener(){
  28. publicvoidonTimeChanged(TimePickerview,inthourOfDay,intminute){
  29. flushTime(hourOfDay,minute);
  30. }
  31. });
  32. }
  33. publicvoidflushDate(intyear,intmonthOfYear,intdayOfMonth){
  34. EditTextet=(EditText)findViewById(R.id.editText1);
  35. et.setText("您选择的日期是:"+year+"年"+(monthOfYear+1)+"月"+dayOfMonth+"日");
  36. }
  37. publicvoidflushTime(inthourOfDay,intminute){
  38. EditTextet=(EditText)findViewById(R.id.editText2);
  39. et.setText("您选择的时间是:"+hourOfDay+"时"+minute+"分");
  40. }
  41. }

5. 动画播放技术
动画播放技术主要有两种:帧动画和补间动画。
1) 帧动画
帧动画主要用到的类是AnimationDrawable,每个帧动画都是一个AnimationDrawable对象
定义帧动画可以在代码中直接进行,也可以通过XML文件进行定义,定义帧动画的XML文件将存放在res/anim目录下。XML文件中指定了图片图片帧出现的顺序及每个帧的连续时间。

anim.xml
Xml代码 复制代码
  1. <?xmlversion="1.0"encoding="utf-8"?>
  2. <animation-listxmlns:android="http://schemas.android.com/apk/res/android"
  3. android:oneshot="false">
  4. <itemandroid:drawable="@drawable/f1"android:duration="500"android:visible=
  5. "true"/>
  6. <itemandroid:drawable="@drawable/f2"android:duration="500"android:visible=
  7. "true"/>
  8. <itemandroid:drawable="@drawable/f3"android:duration="500"android:visible=
  9. "true"/>
  10. <itemandroid:drawable="@drawable/f4"android:duration="500"android:visible=
  11. "true"/>
  12. </animation-list>


main.xml
Xml代码 复制代码
  1. <?xmlversion="1.0"encoding="utf-8"?>
  2. <LinearLayoutxmlns:android="http://schemas.android.com/apk/res/android"android:layout_width="fill_parent"android:layout_height="fill_parent"android:orientation="vertical"android:id="@+id/linearLayout1">
  3. <ImageViewandroid:layout_width="wrap_content"android:layout_height="wrap_content"android:id="@+id/imageView1"android:layout_gravity="center_horizontal"android:src="@anim/anim"></ImageView>
  4. <Buttonandroid:layout_height="wrap_content"android:id="@+id/button1"android:text="click"android:layout_width="fill_parent"android:layout_gravity="center_horizontal"></Button>
  5. </LinearLayout>



Activity:
Java代码 复制代码
  1. packageqijia.si;
  2. importandroid.app.Activity;
  3. importandroid.graphics.drawable.AnimationDrawable;
  4. importandroid.os.Bundle;
  5. importandroid.view.View;
  6. importandroid.view.View.OnClickListener;
  7. importandroid.widget.Button;
  8. importandroid.widget.ImageView;
  9. publicclassMyAndroidProjectextendsActivity{
  10. /**Calledwhentheactivityisfirstcreated.*/
  11. @Override
  12. publicvoidonCreate(BundlesavedInstanceState){
  13. super.onCreate(savedInstanceState);
  14. setContentView(R.layout.main);
  15. Buttonbtn=(Button)findViewById(R.id.button1);
  16. btn.setOnClickListener(newOnClickListener(){
  17. publicvoidonClick(Viewv){
  18. ImageViewiv=(ImageView)findViewById(R.id.imageView1);
  19. iv.setBackgroundResource(R.anim.anim);
  20. AnimationDrawablead=(AnimationDrawable)iv.getBackground();
  21. ad.start();
  22. }
  23. });
  24. }
  25. }
2)补间动画
补间动画作用于View对象,主要包括View对象的位置,尺寸,旋转角度和透明度的变换。
补间动画可以通过XML声明也可以在代码中动态定义。推荐使用XML因为XML文件可读性及可用性高,方便替换。

Android开发03—Android常用基本控件(上)

1. 文本控件介绍
1) TextView类
TextView类继承自View类。TextView控件的功能是向用户显示文本内容,同时可选择性的让用户编辑文本。其自身被设置为不可编辑,其子类EditText被设置为可以编辑。
2) EditText类
EditText继承自TextView。EditText与TextView最大的不同就是用户可以对EditText控件进行编辑,同时还可以为EditText设置监听器,用来检测用户输入是否合法。
实例1:接收用户输入的电子邮箱地址和电话号码:
color.xml
Xml代码 复制代码
  1. <?xmlversion="1.0"encoding="utf-8"?>
  2. <resources>
  3. <colorname="shadow">#fd8d8d</color>
  4. </resources>


string.xml
Xml代码 复制代码
  1. <?xmlversion="1.0"encoding="utf-8"?>
  2. <resources>
  3. <stringname="hello">HelloWorld,AndroidTest!</string>
  4. <stringname="app_name">AndroidTest</string>
  5. <stringname="tvEmail">邮箱地址</string>
  6. <stringname="etEmail">请输入电子邮件地址</string>
  7. <stringname="tvPhone">电话号码</string>
  8. <stringname="etPhone">请输入电话号码</string>
  9. <stringname="etInfo">此处显示登记信息</string>
  10. </resources>

main.xml
Xml代码 复制代码
  1. <?xmlversion="1.0"encoding="utf-8"?>
  2. <TableLayoutxmlns:android="http://schemas.android.com/apk/res/android"android:shrinkColumns="0,2"android:layout_width="fill_parent"android:id="@+id/tableLayout1"android:layout_height="fill_parent">
  3. <TableRowandroid:id="@+id/tableRow1"android:layout_width="wrap_content"android:layout_height="wrap_content">
  4. <TextViewandroid:layout_width="wrap_content"android:layout_height="wrap_content"android:text="@string/tvEmail"android:ellipsize="end"android:autoLink="email"android:id="@+id/tvEmail"></TextView>
  5. <EditTextandroid:layout_width="wrap_content"android:layout_height="wrap_content"android:selectAllOnFocus="true"android:hint="@string/etEmail"android:id="@+id/etEmail"></EditText>
  6. </TableRow>
  7. <TableRowandroid:id="@+id/tableRow2"android:layout_width="wrap_content"android:layout_height="wrap_content">
  8. <TextViewandroid:layout_width="wrap_content"android:layout_height="wrap_content"android:text="@string/tvPhone"android:ellipsize="middle"android:autoLink="phone"android:id="@+id/tvPhone"></TextView>
  9. <EditTextandroid:layout_width="wrap_content"android:layout_height="wrap_content"android:hint="@string/etPhone"android:selectAllOnFocus="true"android:maxWidth="160px"android:phoneNumber="true"android:singleLine="true"android:id="@+id/etPhone"></EditText>
  10. </TableRow>
  11. <EditTextandroid:id="@+id/editText3"android:layout_width="wrap_content"android:layout_height="wrap_content"android:text="EditText"android:editable="false"android:hint="@string/etInfo"android:cursorVisible="false"android:lines="5"android:shadowColor="@color/shadow"android:shadowDx="2.5"android:shadowDy="2.5"android:shadowRadius="5.0"></EditText>
  12. </TableLayout>


MyAndroidTest.java
Java代码 复制代码
  1. packageqijia.si;
  2. importandroid.app.Activity;
  3. importandroid.os.Bundle;
  4. importandroid.view.KeyEvent;
  5. importandroid.view.View;
  6. importandroid.view.View.OnKeyListener;
  7. importandroid.widget.EditText;
  8. publicclassAndroidTestextendsActivity{
  9. /**Calledwhentheactivityisfirstcreated.*/
  10. @Override
  11. publicvoidonCreate(BundlesavedInstanceState){
  12. super.onCreate(savedInstanceState);
  13. setContentView(R.layout.main);
  14. EditTextetEmail=(EditText)findViewById(R.id.etEmail);
  15. etEmail.setOnKeyListener(myOnKeyListener);
  16. }
  17. privateOnKeyListenermyOnKeyListener=newOnKeyListener(){
  18. publicbooleanonKey(Viewv,intkeyCode,KeyEventevent){
  19. EditTextetInfo=(EditText)findViewById(R.id.editText3);
  20. EditTextetEmail=(EditText)findViewById(R.id.etEmail);
  21. etInfo.setText("您输入的邮箱地址是:"+etEmail.getText());
  22. returntrue;
  23. }
  24. };
  25. }

2. 按钮控件
1) Button类简介
Button继承自TextView类,用户可以对Button控件执行按下或单击操作。Button控件的用法简单,主要是为Button控件设置View.OnClickListener监听器并在监听器的实现代码中开发按钮按下的事件。
一般格式:
Java代码 复制代码
  1. publicvoidonCreate(BundlesavedInstanceState){
  2. super.onCreate(savedInstanceState);
  3. setContentView(R.layout.main);
  4. Buttonbtn=(Button)findViewById(R.Id.ID);
  5. btn.setOnClickListener(newOnClickListener){
  6. publicvoidonClick(Viewv){
  7. //处理的事件
  8. }
  9. };
  10. }

2) ImageButton类
ImageButton类继承自ImageView类,ImageButton控件与Button控件的主要区别在于,ImageButton类没有text属性,即按钮显示图片而不是文本。ImageButton控件设置按钮显示的图片通过android:src属性,也可以通过setImageResource(int)方法来实现。
默认情况下,ImageButton同Button一样具有背景色,当按钮处于不同状态喜爱时,背景色也会变化。当ImageButton所显示图片不能完全覆盖掉背景色时,这种显示效果会相当恶心,所以使用ImageButton一般要将背景色设置为其他图片或直接设置为透明。
注意:新建xml文件文件名不能是大写字母
实例2:为ImageButton按钮控件的不同状态设置不同的图片。
color.xml:
Xml代码 复制代码
  1. <?xmlversion="1.0"encoding="utf-8"?>
  2. <resources>
  3. <colorname="back">#000000</color>
  4. </resources>


myselector.xml:

Xml代码 复制代码
  1. <?xmlversion="1.0"encoding="utf-8"?>
  2. <selectorxmlns:android="http://schemas.android.com/apk/res/android">
  3. <item
  4. android:state_pressed="false"
  5. android:drawable="@drawable/back"/>
  6. <item
  7. android:state_pressed="true"
  8. android:drawable="@drawable/backdown"/>
  9. </selector>

main.xml:
Xml代码 复制代码
  1. <?xmlversion="1.0"encoding="utf-8"?>
  2. <LinearLayoutxmlns:android="http://schemas.android.com/apk/res/android"android:orientation="vertical"android:id="@+id/linearLayout1"android:layout_height="fill_parent"android:layout_width="fill_parent">
  3. <ImageButtonandroid:id="@+id/imageButton1"android:layout_height="wrap_content"android:layout_width="wrap_content"android:background="@color/back"android:src="@drawable/myselector"></ImageButton>
  4. </LinearLayout>


3. 状态开关
1) ToggleButton类
ToggleButton的状态只能是选中和未选中,并且需要为不同的状态设置不同的文本。
android:Off 未被选中时显示的文本
android:On 选中时显示的文本

实例3:ToggleButton的用法:

strings.xml:
Xml代码 复制代码
  1. <?xmlversion="1.0"encoding="utf-8"?>
  2. <resources>
  3. <stringname="hello">HelloWorld,AndroidTest!</string>
  4. <stringname="app_name">AndroidTest</string>
  5. <stringname="off">关灯</string>
  6. <stringname="on">开灯</string>
  7. </resources>


main.xml
Xml代码 复制代码
  1. <?xmlversion="1.0"encoding="utf-8"?>
  2. <LinearLayoutandroid:id="@+id/linearLayout1"android:layout_width="fill_parent"android:layout_height="fill_parent"xmlns:android="http://schemas.android.com/apk/res/android"android:orientation="vertical">
  3. <ImageViewandroid:layout_height="wrap_content"android:layout_width="wrap_content"android:src="@drawable/bulb_off"android:id="@+id/ImageView01"android:layout_gravity="center_horizontal"></ImageView>
  4. <ToggleButtonandroid:layout_gravity="center_horizontal"android:layout_height="wrap_content"android:layout_width="140dip"android:textOff="@string/on"android:textOn="@string/off"android:id="@+id/Tb"></ToggleButton>
  5. </LinearLayout>


MyAnroidTest.java:
package qijia.si;

Java代码 复制代码
  1. importandroid.app.Activity;
  2. importandroid.os.Bundle;
  3. importandroid.view.KeyEvent;
  4. importandroid.view.View;
  5. importandroid.view.View.OnKeyListener;
  6. importandroid.widget.CompoundButton;
  7. importandroid.widget.CompoundButton.OnCheckedChangeListener;
  8. importandroid.widget.EditText;
  9. importandroid.widget.ImageView;
  10. importandroid.widget.ToggleButton;
  11. publicclassAndroidTestextendsActivity{
  12. /**Calledwhentheactivityisfirstcreated.*/
  13. @Override
  14. publicvoidonCreate(BundlesavedInstanceState){
  15. super.onCreate(savedInstanceState);
  16. setContentView(R.layout.main);
  17. ToggleButtontb=(ToggleButton)findViewById(R.id.Tb);
  18. tb.setOnCheckedChangeListener(newOnCheckedChangeListener(){
  19. //添加监听器
  20. publicvoidonCheckedChanged(CompoundButtonbuttonView,
  21. booleanisChecked){
  22. setBulbState(isChecked);
  23. }
  24. });
  25. }
  26. publicvoidsetBulbState(booleanstate){
  27. ImageViewiv=(ImageView)findViewById(R.id.ImageView01);
  28. iv.setImageResource((state)?R.drawable.bulb_on:R.drawable.bulb_off);
  29. //设置图片资源
  30. ToggleButtontb=(ToggleButton)this.findViewById(R.id.Tb);
  31. tb.setChecked(state);
  32. //设置为选中状态
  33. }
  34. }

Android系统开发02—Android布局管理器

1. View,ViewGroup
1) View类
View类为所有可视化控件的基类,主要提供了空间绘制和事件处理。
2)ViewGroup类
ViewGroup类也是View类的子类,但是可以充当其他控件的容器。

2. 线性布局
线性布局是最简单的布局,它提供了控件水平或者垂直排列的模型。使用此布局时可以通过设置控件的weight参数控制各个控件在容器中的相对大小。
线性布局实例:
string.xml:
Xml代码 复制代码
  1. <?xmlversion="1.0"encoding="utf-8"?>
  2. <resources>
  3. <stringname="app_name">LinearExample</string>
  4. <stringname="button">按钮</string>
  5. <stringname="add">添加</string>
  6. </resources>

main.xml:
Xml代码 复制代码
  1. <?xmlversion="1.0"encoding="utf-8"?>
  2. <LinearLayoutxmlns:android="http://schemas.android.com/apk/res/android"android:layout_height="fill_parent"android:layout_width="fill_parent"android:id="@+id/lla"android:orientation="vertical"android:gravity="right">
  3. <Buttonandroid:layout_height="wrap_content"android:layout_width="wrap_content"android:id="@+id/Button01"android:text="@string/add"></Button>
  4. </LinearLayout>

MyAndroidProject.java:
Java代码 复制代码
  1. packageqijia.si;
  2. importandroid.app.Activity;
  3. importandroid.os.Bundle;
  4. importandroid.view.View;
  5. importandroid.widget.Button;
  6. importandroid.widget.LinearLayout;
  7. publicclassMyAndroidProjectextendsActivity{
  8. intcount=0;
  9. /**Calledwhentheactivityisfirstcreated.*/
  10. @Override
  11. publicvoidonCreate(BundlesavedInstanceState){
  12. super.onCreate(savedInstanceState);
  13. setContentView(R.layout.main);
  14. Buttonbtn=(Button)findViewById(R.id.Button01);
  15. btn.setOnClickListener(
  16. newView.OnClickListener(){
  17. publicvoidonClick(Viewv){
  18. //TODOAuto-generatedmethodstub
  19. LinearLayoutll=(LinearLayout)findViewById(R.id.lla);
  20. Stringmsg=MyAndroidProject.this.getResources().getString(R.string.button);
  21. Buttontempbutton=newButton(MyAndroidProject.this);
  22. tempbutton.setText(msg+(++count));
  23. tempbutton.setWidth(80);
  24. ll.addView(tempbutton);
  25. }
  26. }
  27. );
  28. }
  29. }
3. 表格布局
TableLayout类以行和列的形式管理控件,每行为一个TableRow对象,也可以为一个View对象,当为View对象时,该View对象将跨越该行的所有列。在TableRow中可以添加子空间,每添加一个子空间为一列。
在TableLayout中,可以为列设置三种属性:
1) Shrinkable,该列宽度可以进行收缩,以使表格能够适应期父容器的大小
2) Stretchable,该列的宽度可以进行拉伸,以使填满表格中空闲的空间。
3) Collapsed,该列将被隐藏。
注意:一个列可以同时拥有1,2的属性。
案例:
color.xml
Xml代码 复制代码
  1. <?xmlversion="1.0"encoding="utf-8"?>
  2. <resources>
  3. <colorname="white">#ffffff</color>
  4. <colorname="red">#ff0000</color>
  5. <colorname="black">#000000</color>
  6. <colorname="green">#00ff00</color>
  7. <colorname="blue">#0000ff</color>
  8. </resources>

string.xml
Xml代码 复制代码
  1. <?xmlversion="1.0"encoding="utf-8"?>
  2. <resources>
  3. <stringname="app_name">TableExample</string>
  4. <stringname="tv1">我自己是一行…………我自己是一行</string>
  5. <stringname="tvShort">我的内容少</string>
  6. <stringname="tvStrech">我是被拉伸的一行</string>
  7. <stringname="tvShrink">我是被收缩的一行被收缩的一行</string>
  8. <stringname="tvLong">我的内容很多很多很多很多很多很多</string>
  9. </resources>

main.xml

Xml代码 复制代码
  1. <?xmlversion="1.0"encoding="utf-8"?>
  2. <LinearLayoutxmlns:android="http://schemas.android.com/apk/res/android"android:id="@+id/LinearLayout01"android:layout_width="fill_parent"android:layout_height="fill_parent"android:orientation="vertical"android:background="@drawable/pic"android:gravity="bottom">
  3. <TableLayoutandroid:layout_height="wrap_content"android:layout_width="fill_parent"android:id="@+id/TableLayout03"android:background="@color/white"android:stretchColumns="0"android:collapseColumns="1">
  4. </TableLayout>
  5. <TableLayoutandroid:layout_height="wrap_content"android:layout_width="fill_parent"android:id="@+id/TableLayout02"android:background="@color/white"android:stretchColumns="0">
  6. <TableRowandroid:layout_height="wrap_content"android:layout_width="wrap_content"android:id="@+id/TableRow01">
  7. <TextViewandroid:layout_width="wrap_content"android:layout_height="wrap_content"android:id="@+id/TextView02"android:text="@string/tvStrech"android:layout_margin="4px"android:background="@color/green"android:textColor="@color/blue"></TextView>
  8. <TextViewandroid:layout_width="wrap_content"android:layout_height="wrap_content"android:id="@+id/TextView03"android:text="@string/tvShort"android:textColor="@color/black"android:background="@color/blue"android:layout_margin="4px"></TextView>
  9. </TableRow>
  10. </TableLayout>
  11. <TableLayoutandroid:layout_height="wrap_content"android:layout_width="fill_parent"android:id="@+id/TableLayout01"android:background="@color/white">
  12. <TextViewandroid:textColor="@color/black"android:layout_margin="4px"android:text="@string/tv1"android:background="@color/red"android:layout_height="wrap_content"android:layout_width="wrap_content"android:id="@+id/TextView01"></TextView>
  13. </TableLayout>
  14. </LinearLayout>

MyAndroidProject.java
Java代码 复制代码
  1. packageqijia.si;
  2. importandroid.app.Activity;
  3. importandroid.os.Bundle;
  4. publicclassMyAndroidProjectextendsActivity{
  5. /**Calledwhentheactivityisfirstcreated.*/
  6. @Override
  7. publicvoidonCreate(BundlesavedInstanceState){
  8. super.onCreate(savedInstanceState);
  9. setContentView(R.layout.main);
  10. }
  11. }

4. 相对布局
在相对布局中,子控件的位置是相对兄弟控件或父容器而决定的。
需要注意的是,相对布局中要避免出现循环依赖。
5. 帧布局
FrameLayout帧布局在屏幕上开辟出了一块区域,在这块区域中可以添加多个子控件,但是所有的子空间都被对齐到屏幕的左上角。帧布局的大小由子控件中尺寸最大的那个控件决定。
6. 绝对布局
绝对布局是指屏幕中所有控件的摆放由开发人员通过设置控件的坐标来指定,控件容器不再负责管理其子控件的位置。
案例:
string.xml
Xml代码 复制代码
  1. <?xmlversion="1.0"encoding="utf-8"?>
  2. <resources>
  3. <stringname="app_name">AbsoluteExample</string>
  4. <stringname="uid">用户名</string>
  5. <stringname="pwd">密码</string>
  6. <stringname="ok">确定</string>
  7. <stringname="cancel">取消</string>
  8. </resources>

color.xml
Xml代码 复制代码
  1. <?xmlversion="1.0"encoding="utf-8"?>
  2. <resources>
  3. <colorname="white">#ffffff</color>
  4. <colorname="red">#ff0000</color>
  5. <colorname="black">#000000</color>
  6. <colorname="green">#00ff00</color>
  7. <colorname="blue">#0000ff</color>
  8. </resources>

main.xml
Xml代码 复制代码
  1. <?xmlversion="1.0"encoding="utf-8"?>
  2. <AbsoluteLayoutxmlns:android="http://schemas.android.com/apk/res/android"android:layout_width="fill_parent"android:background="@color/white"android:id="@+id/absoluteLayout1"android:layout_height="fill_parent">
  3. <TextViewandroid:layout_height="wrap_content"android:layout_width="wrap_content"android:layout_x="20dip"android:layout_y="20dip"android:id="@+id/TextView01"android:text="@string/uid"></TextView>
  4. <TextViewandroid:layout_height="wrap_content"android:layout_width="wrap_content"android:id="@+id/TextView02"android:text="@string/pwd"android:layout_x="20dip"android:layout_y="80dip"></TextView>
  5. <EditTextandroid:text="EditText"android:layout_height="wrap_content"android:layout_y="20dip"android:id="@+id/EditText01"android:layout_x="80dip"android:layout_width="180dip"></EditText>
  6. <EditTextandroid:text="EditText"android:layout_height="wrap_content"android:id="@+id/EditText02"android:password="true"android:layout_x="80dip"android:layout_y="80dip"android:layout_width="180dip"></EditText>
  7. <Buttonandroid:layout_height="wrap_content"android:layout_width="wrap_content"android:id="@+id/Button01"android:text="@string/ok"android:layout_x="155dip"android:layout_y="140dip"></Button>
  8. <Buttonandroid:layout_height="wrap_content"android:layout_width="wrap_content"android:id="@+id/Button02"android:text="@string/cancel"android:layout_x="210dip"android:layout_y="140dip"></Button>
  9. <ScrollViewandroid:layout_x="10dip"android:layout_y="200dip"android:layout_height="150dip"android:layout_width="250dip"android:id="@+id/ScrollView01">
  10. <EditTextandroid:text="EditText"android:layout_height="wrap_content"android:layout_width="fill_parent"android:singleLine="false"android:gravity="top"android:id="@+id/EditText03"></EditText>
  11. </ScrollView>
  12. </AbsoluteLayout>

MyAndroidProject.java
Java代码 复制代码
  1. packageqijia.si;
  2. importandroid.app.Activity;
  3. importandroid.os.Bundle;
  4. importandroid.view.View;
  5. importandroid.widget.Button;
  6. importandroid.widget.EditText;
  7. publicclassMyAndroidProjectextendsActivity{
  8. /**Calledwhentheactivityisfirstcreated.*/
  9. @Override
  10. publicvoidonCreate(BundlesavedInstanceState){
  11. super.onCreate(savedInstanceState);
  12. setContentView(R.layout.main);
  13. finalButtonokButton=(Button)findViewById(R.id.Button01);
  14. finalButtoncancelButton=(Button)findViewById(R.id.Button02);
  15. finalEditTextuid=(EditText)findViewById(R.id.EditText01);
  16. finalEditTextpwd=(EditText)findViewById(R.id.EditText02);
  17. finalEditTextlog=(EditText)findViewById(R.id.EditText03);
  18. okButton.setOnClickListener(
  19. newView.OnClickListener(){
  20. publicvoidonClick(Viewv){
  21. //TODOAuto-generatedmethodstub
  22. StringuidStr=uid.getText().toString();
  23. StringpwdStr=pwd.getText().toString();
  24. log.append("用户名:"+uidStr+"密码:"+pwdStr+"\n");
  25. }
  26. }
  27. );
  28. cancelButton.setOnClickListener(
  29. newView.OnClickListener(){
  30. publicvoidonClick(Viewv){
  31. //TODOAuto-generatedmethodstub
  32. uid.setText("");
  33. pwd.setText("");
  34. }
  35. }
  36. );
  37. }
  38. }
Android系统开发01—Android基本组件

1. 应用程序生命周期
应用程序进程从创建到结束的全过程便是应用程序的生命周期。与其他系统不同,Android应用程序的生命周期是不受进程自身控制的,而是由Android系统决定的。
Android系统将所有的进程分为5类进行管理:
1. 前台进程
2. 可见进程:还在屏幕中,但是用户并没有直接与之进行交互。
3. 服务进程
4. 后台进程
5. 空进程
从1到5,重要顺序递减。
注意:应用程序在运行时,其状态的切换可能是通过自身实现的,可也能是系统将其改变的。
2. Activity
Activity是Android中最常用的组件,是应用程序的表示层,Activity一般通过View来实现应用程序的用户界面,相当于一个屏幕,用户与程序的交互式通过该类实现的。
Activity的生命周期主要包含三个状态:运行态,暂停态,停止态。
Activity的显示内容跟是由View对象提供的,View对象继承自View类,其中每个View对象管理屏幕中的一个矩形区域。Android自带了许多View对象,而除了使用Android自带的View外,还可以自定义View。
例子:
Java代码 复制代码
  1. MyView.java:
  2. packageqijia.si;
  3. importandroid.content.Context;
  4. importandroid.graphics.Canvas;
  5. importandroid.graphics.Color;
  6. importandroid.graphics.Paint;
  7. importandroid.view.View;
  8. publicclassMyViewextendsView{
  9. Paintpaint;
  10. publicMyView(Contextcontext){
  11. super(context);
  12. paint=newPaint();
  13. paint.setColor(Color.WHITE);
  14. paint.setTextSize(20);
  15. paint.setAntiAlias(true);
  16. }
  17. protectedvoidonDraw(Canvascanvas){
  18. super.onDraw(canvas);
  19. canvas.drawColor(Color.GRAY);
  20. canvas.drawRect(10,10,110,110,paint);
  21. canvas.drawText("fuckyou",60,170,paint);
  22. }
  23. }
  24. MyAndroidProject.java:
  25. packageqijia.si;
  26. importandroid.app.Activity;
  27. importandroid.os.Bundle;
  28. publicclassMyAndroidProjectextendsActivity{
  29. /**Calledwhentheactivityisfirstcreated.*/
  30. MyViewmyView;
  31. @Override
  32. publicvoidonCreate(BundlesavedInstanceState){
  33. super.onCreate(savedInstanceState);
  34. myView=newMyView(this);
  35. this.setContentView(myView);
  36. }
  37. }
3. Service
Service是一个具有较长的生命周期但是并没有用户界面的程序。
Service一般由Activity启动,但不依赖于Activity,Service有两种启动方式:
1. startService
当Activity调用startService方法启动Service时,会依次调用onCreate和onStart方法来启动Service。当结束时,调用onDestroy方法结束Service。
2. bindService

BoardReceiver
BoardReceiver为用户接收广播通知的组件,当系统或某个应用程序发送广播时,可以使用BoardReceiver组件来接收广播信息并作出相应的处理。
使用过程:
1. 将需要广播的消息封装到Intent中。
2. Context.sendBroadcast(),sendOrderedBroadcast(),sendStickyBroadcast()中的一种将Intent发送
3. 通过IntentFilter对象过滤所发送的实体Intent
4. 重写onReceive方法的BoardReceiver。

4. ContentProvider
ContentProvider是用来实现应用程序之间数据共享的类。
5. Intent和IntentFilter
1) Intent类简介
Intent是一种运行时绑定机制,在应用程序运行时连接两个不同的组件。一般的应用是通过Intent向Android系统发出某种请求,然后Android系统会根据请求查询各个组件声明的IntentFilter,找到需要的组件并运行它。
前面所说的Activity,Service及BroadcastReceiver组件之间的通信全部使用的是Intent但是每个机制不同。
Activity组件:当需要激活一个Activity组件时,需要调用Context.startActivity或Context.startActivityForResult方法来传递Intent,此时的Intent参数成为Activity Action Intent
Service组件:一般通过Context.startService和Context.bindService
BroadcastReceiver组件:上面已经介绍

Intent是由组件名称,Action,Data,Category,Extra和Flag组成:

1. 组件名称:组件名称实际上就是一个ComponentName对象,用于标识唯一的应用程序组件。
2. Action:一个描述Intent所触发动作名称的字符串。如
ACTION_CALL,ACTION_EDIT,ACTION_VIEW,ACTION_MAIN等
3. Data:主要对Intent消息中数据的封装,主要描述Intent的动作所操作到的数据的URI及类型
4. Category:是对目标组件类型的描述
5. Extra:封装了一些额外的附加信息。
2) IntentFilter
IntentFilter实际上相当于Intent的过滤器。IntentFilter过滤Intent时,主要通过Action,Data及Category三方面进行监测:
1. 检查Action:一个Intent只能设置一种Action,但是一个IntentFilter却可以设置多个Action过滤。当IntentFilter设置了多个Action时,只需一个满足就可完成Action验证。
2. 检查Data:主要检查URI及数据类型
3. 检查Category:当Intent中的Category与IntentFilter中的一个Category完全匹配时,便会通过Category检查。
Intent案例:
判断输入的电话号码是否符合规范当符合规范时,调用系统自带的拨号程序进行拨号。

Java代码 复制代码
  1. packageqijia.si;
  2. importandroid.app.Activity;
  3. importandroid.content.Intent;
  4. importandroid.net.Uri;
  5. importandroid.os.Bundle;
  6. importandroid.telephony.PhoneNumberUtils;
  7. importandroid.view.View;
  8. importandroid.widget.Button;
  9. importandroid.widget.EditText;
  10. importandroid.widget.Toast;
  11. publicclassMyAndroidProjectextendsActivity{
  12. /**Calledwhentheactivityisfirstcreated.*/
  13. @Override
  14. publicvoidonCreate(BundlesavedInstanceState){
  15. super.onCreate(savedInstanceState);
  16. setContentView(R.layout.main);
  17. ButtonbCall=(Button)this.findViewById(R.id.Button01);
  18. bCall.setOnClickListener(
  19. newView.OnClickListener(){
  20. publicvoidonClick(Viewv){
  21. //TODOAuto-generatedmethodstub
  22. EditTexteTel=(EditText)findViewById(R.id.myEditText);
  23. StringstrTel=eTel.getText().toString();
  24. if(PhoneNumberUtils.isGlobalPhoneNumber(strTel)){
  25. Intenti=newIntent(Intent.ACTION_DIAL,Uri.parse("tel://"+strTel));
  26. MyAndroidProject.this.startActivity(i);
  27. }else{
  28. Toast.makeText(
  29. MyAndroidProject.this,
  30. "号码格式不正确",
  31. 5000
  32. ).show();
  33. }
  34. }
  35. }
  36. );
  37. }
  38. }
分享到:
评论

相关推荐

    老罗Android入门到开发视频

    Android-03-百度地图实战开发(10集) Android-04-HTTP协议编程(4集) Android-05-解析XML数据(3集) Android-06-解析JSON数据(4集) Android-07-服务器端JDBC编程(2集) Android-08-服务器端Web编程(6集) ...

    老罗android开发视频教程全集百度网盘下载

    【第一版第十二章】老罗Android开发视频--菜单的使用(4集) 【第一版第十三章】老罗Android开发视频--异步加载数据库(2集) 【第一版第十四章】老罗Android开发视频--多线程编程(7集) 【第一版第十五章】老罗...

    Android游戏开发系列教程第六讲(菜单对话框)源码

    Android游戏开发系列教程第六讲(菜单对话框)源码

    Android SDK离线包合集(Android 4.0-5.0)

    这是Android开发所需的sdk,下载并解压后,将解压出的整个文件夹复制或者移动到your sdk 路径/platforms文件夹,然后打开SDK Manager,打开 Tools(工具)菜单选择Options(选项)菜单项打开 Android SDK Manager ...

    Android应用开发案例教程 (毋建军、徐振东、林瀚 编著) pdf

    全书论述了Android开发概述,Android应用程序组成,Android UI(用户界面)基础,Android UI系统控件基础,Android UI系统控件进阶,Android UI菜单、对话框,Android组件广播消息与服务,Android数据存储与访问,...

    android开发揭秘PDF

    第1章 Android开发简介 1.1 Android基本概念 1.1.1 Android简介 1.1.2 Android的系统构架 1.1.3 Android应用程序框架 1.2 OMS介绍 1.2.1 OPhone介绍 1.2.2 Widget介绍 1.3 小结 第2章 Android开发环境搭建 2.1 ...

    Android应用开发案例教程

    全书论述了android开发概述,android应用程序组成,android ui(用户界面)基础,android ui系统控件基础,android ui系统控件进阶,android ui菜单、对话框,android 组件广播消息与服务,android数据存储与访问,...

    android开发资料大全

    新版Android开发教程及笔记-完整版 《Android中文教程》中文版 《android基础教程合集》 Android实例教程 会员贡献索引贴 实用Android开发工具和资源精选 APK权限大全 - Android必懂知识 最无私的Android资料...

    《Google Android开发入门与实战》

    第7章 良好的学习开端——Android基本组件介绍之友好的菜单——menu介绍与实例 第7章 良好的学习开端——Android基本组件介绍之Android应用的灵魂——Intent和Activity介绍与实例 第7章 良好的学习开端——Android...

    Android 自定义弹出菜单和对话框功能实例代码

    Android 开发当中,可能会存在许多自定义布局的需求,比如自定义弹出菜单(popupWindow),以及自定义对话框(Dialog)。下面通过本文给大家介绍Android 自定义弹出菜单和对话框功能实例代码,感兴趣的朋友一起看看...

    android开发入门与实战(下)

    第2章 工欲善其事 必先利其器——搭建Android开发环境 2.1 开发Android应用前的准备 2.1.1 Android开发系统要求 2.1.2 Android软件开发包 2.1.3 其他注意事项 2.2 Windows开发环境搭建 2.2.1 JDK、Eclipse、Android...

    Android高级编程--源代码

    因为没有了人为制造的障碍,所以Android开发人员可以自由地编写能够充分利用日益强大的手机硬件的应用程序。因此,对Android感兴趣的开发人员都把Google在2008年发布Android这一举措作为移动技术发展史上的一个非常...

    Android开发案例驱动教程 配套代码

    《Android开发案例驱动教程》 配套代码。 注: 由于第12,13,14章代码太大,无法上传到一个包中。 这三节代码会放到其他压缩包中。 作者:关东升,赵志荣 Java或C++程序员转变成为Android程序员 采用案例驱动模式...

    Android应用开发揭秘pdf高清版

    第1章 Android开发简介 1.1 Android基本概念 1.1.1 Android简介 1.1.2 Android的系统构架 1.1.3 Android应用程序框架 1.2 OMS介绍 1.2.1 OPhone介绍 1.2.2 Widget介绍 1.3 小结 第2章 Android开发环境搭建 2.1 ...

    android开发入门与实战(上)

    第2章 工欲善其事 必先利其器——搭建Android开发环境 2.1 开发Android应用前的准备 2.1.1 Android开发系统要求 2.1.2 Android软件开发包 2.1.3 其他注意事项 2.2 Windows开发环境搭建 2.2.1 JDK、Eclipse、Android...

    60个Android开发精典案例 Android软件源码.zip

    60个Android开发精典案例 Android软件源码: 2-1(Activity生命周期) 3-1(Button与点击监听器) 3-10-1(列表之ArrayAdapter适配) 3-10-2(列表之SimpleAdapter适配) 3-11(Dialog对话框) 3-12-5(Activity跳转与操作) 3-12...

    Google Android SDK开发范例大全 源码

    安装Android SDK与ADT plug-in  建立第一个Android项目(Hello Android!)  Android应用程序架构——从此开始  可视化的界面开发工具  部署应用程序到Android手机  该作品是PDF文件格式请下载 福昕PDF阅读器...

    Android开发权威指南 第二版

    《Android开发权威指南(第二版)》全面介绍了Android应用开发的各种技术,主要内容包括Android的四大应用程序组件(Activity、Service、Content Provider和Broadcast Receiver)、布局、菜单、控件、资源和本地化、可视...

    《Google Android开发入门与实战》.pdf

    在程序实例的讲解方面,主要将实例安插在android开发的精髓知识章节,这为初学者学习与实践结合提供了很好的指导。.  本书配套有400多分钟的全程开发视频光盘,指导读者快速、无障碍地学通android实战开发技术。.. ...

Global site tag (gtag.js) - Google Analytics