Android开发教程+笔记十--基础UI编程4.pdf

上传人:asd****56 文档编号:70321767 上传时间:2023-01-19 格式:PDF 页数:55 大小:14.62MB
返回 下载 相关 举报
Android开发教程+笔记十--基础UI编程4.pdf_第1页
第1页 / 共55页
Android开发教程+笔记十--基础UI编程4.pdf_第2页
第2页 / 共55页
点击查看更多>>
资源描述

《Android开发教程+笔记十--基础UI编程4.pdf》由会员分享,可在线阅读,更多相关《Android开发教程+笔记十--基础UI编程4.pdf(55页珍藏版)》请在taowenge.com淘文阁网|工程机械CAD图纸|机械工程制图|CAD装配图下载|SolidWorks_CaTia_CAD_UG_PROE_设计图分享下载上搜索。

1、-Android 编程基础1封面封面-Android 编程基础2AndroidAndroidAndroidAndroid 基础基础 UIUIUIUI 编程编程 4 4 4 4专业相框设计专业相框设计ImageView 的堆叠应用新建工程准备三张 png 图片修改 main.xml 布局,添加 UI 元素left.pngright.pngphoto.pngpackagepackagepackagepackage zyf.Ex_Ctrl_7;importimportimportimport android.app.Activity;importimportimportimport android.

2、os.Bundle;importimportimportimport android.view.View;importimportimportimport android.widget.Button;importimportimportimport android.widget.ImageView;publicpublicpublicpublic classclassclassclass Ex_Ctrl_7 extendsextendsextendsextends Activity/*Called when the activity is first created.*/*声明Button、I

3、mageView 对象*/privateprivateprivateprivate ImageView mImageView01;privateprivateprivateprivate ImageView mImageView02;privateprivateprivateprivate Button mButton01;privateprivateprivateprivate Button mButton02;Overridepublicpublicpublicpublic voidvoidvoidvoid onCreate(Bundle savedInstanceState)supers

4、upersupersuper.onCreate(savedInstanceState);setContentView(R.layout.main);/*取得Button、ImageView 对象*/mImageView01=(ImageView)findViewById(R.id.myImageView1);mImageView02=(ImageView)findViewById(R.id.myImageView2);mButton01=(Button)findViewById(R.id.myButton1);mButton02=(Button)findViewById(R.id.myButt

5、on2);/*设置ImageView 背景图*/mImageView01.setImageDrawable(getResources().getDrawable(R.drawable.right);-Android 编程基础4结果mImageView02.setImageDrawable(getResources().getDrawable(R.drawable.photo);/*用OnClickListener 事件来启动*/mButton01.setOnClickListener(newnewnewnew Button.OnClickListener()Overridepublicpubl

6、icpublicpublic voidvoidvoidvoid onClick(View v)/*当启动后,ImageView 立刻换背景图*/mImageView01.setImageDrawable(getResources().getDrawable(R.drawable.right););mButton02.setOnClickListener(newnewnewnew Button.OnClickListener()Overridepublicpublicpublicpublic voidvoidvoidvoid onClick(View v)mImageView01.setImag

7、eDrawable(getResources().getDrawable(R.drawable.left););-Android 编程基础5ImageButton 的堆叠应用新建项目准备三张 png 图片修改 main.xml 布局,添加 UI 元素修改 mainActivity.javaleft.pngright.pngphoto.pngpackagepackagepackagepackage zyf.Ex_Ctrl_7_B;importimportimportimport android.app.Activity;importimportimportimport android.os.Bu

8、ndle;importimportimportimport android.view.View;importimportimportimport android.widget.ImageButton;设置成堆叠-Android 编程基础6结果publicpublicpublicpublic classclassclassclass Ex_Ctrl_7_B extendsextendsextendsextends Activity/*Called when the activity is first created.*/*声明 ImageButton*/privateprivateprivate

9、private ImageButton back_Imagebutton,photo_Imagebutton;privateprivateprivateprivate booleanbooleanbooleanboolean Tag=truetruetruetrue;Overridepublicpublicpublicpublic voidvoidvoidvoid onCreate(Bundle savedInstanceState)supersupersupersuper.onCreate(savedInstanceState);setContentView(R.layout.main);/

10、*从XML中获取控件对象*/back_Imagebutton=(ImageButton)findViewById(R.id.myImageButton_Back);photo_Imagebutton=(ImageButton)findViewById(R.id.myImageButton_Photo);/设置默认的背景图片back_Imagebutton.setBackgroundResource(R.drawable.left);photo_Imagebutton.setBackgroundResource(R.drawable.photo);/给ImageButton设置事件监听器phot

11、o_Imagebutton.setOnClickListener(newnewnewnew ImageButton.OnClickListener()Overridepublicpublicpublicpublic voidvoidvoidvoid onClick(View v)/TODOTODOTODOTODO Auto-generated method stubTag=!Tag;/更改背景图片ifififif(Tag)back_Imagebutton.setBackgroundResource(R.drawable.right);elseelseelseelseback_Imagebutt

12、on.setBackgroundResource(R.drawable.left););-Android 编程基础7自定义下拉菜单自定义下拉菜单Spinner 与 setDropDownViewResource新建项目,在res 目录下新建一个 anim 文件夹,存放动画效果用,在其中新建一个 my_anim.xml 文件在 res 目录下的 layout 文件夹中新建一个 myspinner_dropdown.xml 文件,用来存放下拉菜单弹出内容的布局修改 main.xml,添加 UI 元素privateprivateprivateprivate Spinner mySpinner;/*以

13、 findViewById()取得对象*/mySpinner=(Spinner)findViewById(R.id.spinner_City);privateprivateprivateprivate staticstaticstaticstatic finalfinalfinalfinal StringcountriesStr=北京市,上海市,天津市,重庆市 ;privateprivateprivateprivate ArrayAdapter adapter;adapter=newnewnewnew ArrayAdapter(thisthisthisthis,android.R.layout

14、.simple_spinner_item,countriesStr);/*myspinner_dropdown 为自定义下拉菜单样式定义在 res/layout 目录下*/adapter.setDropDownViewResource(R.layout.myspinner_dropdown);/*将 ArrayAdapter 添加 Spinner 对象中*/mySpinner.setAdapter(adapter);/*取得Animation 定义在res/anim 目录下*/myAnimation=AnimationUtils.loadAnimation(thisthisthisthis,R

15、.anim.my_anim);/*将mySpinner 运行Animation*/mySpinner.startAnimation(myAnimation);packagepackagepackagepackage zyf.Ex_Ctrl_8;importimportimportimport android.app.Activity;importimportimportimport android.os.Bundle;-Android 编程基础9importimportimportimport android.view.MotionEvent;importimportimportimport

16、android.view.View;importimportimportimport android.view.animation.Animation;importimportimportimport android.view.animation.AnimationUtils;importimportimportimport android.widget.AdapterView;importimportimportimport android.widget.ArrayAdapter;importimportimportimport android.widget.Spinner;importim

17、portimportimport android.widget.TextView;publicpublicpublicpublic classclassclassclass Ex_Ctrl_8 extendsextendsextendsextends Activity/*Called when the activity is first created.*/privateprivateprivateprivate staticstaticstaticstatic finalfinalfinalfinal StringcountriesStr=北京市,上海市,天津市,重庆市 ;privatepr

18、ivateprivateprivate TextView myTextView;privateprivateprivateprivate Spinner mySpinner;privateprivateprivateprivate ArrayAdapter adapter;Animation myAnimation;Overridepublicpublicpublicpublic voidvoidvoidvoid onCreate(Bundle savedInstanceState)supersupersupersuper.onCreate(savedInstanceState);/*载入 m

19、ain.xml Layout*/setContentView(R.layout.main);/*以 findViewById()取得对象*/myTextView=(TextView)findViewById(R.id.TextView_Show);mySpinner=(Spinner)findViewById(R.id.spinner_City);/*取得Animation 定义在res/anim 目录下*/myAnimation=AnimationUtils.loadAnimation(thisthisthisthis,R.anim.my_anim);adapter=newnewnewnew

20、ArrayAdapter(thisthisthisthis,android.R.layout.simple_spinner_item,countriesStr);/*myspinner_dropdown 为自定义下拉菜单样式定义在 res/layout 目录下*/adapter.setDropDownViewResource(R.layout.myspinner_dropdown);/*将 ArrayAdapter 添加 Spinner 对象中*/mySpinner.setAdapter(adapter);/*下拉菜单弹出的内容选项被选中事件处理*/mySpinner.setOnItemSel

21、ectedListener(newnewnewnewSpinner.OnItemSelectedListener()publicpublicpublicpublic voidvoidvoidvoid onItemSelected(AdapterView arg0,View arg1,intintintint arg2,longlonglonglong arg3)/TODOTODOTODOTODO Auto-generated method stub/*将所选mySpinner 的值带入myTextView 中*/myTextView.setText(您选择的是:+countriesStrarg

22、2);/*将mySpinner 显示*/-Android 编程基础10结果arg0.setVisibility(View.VISIBLE);publicpublicpublicpublic voidvoidvoidvoid onNothingSelected(AdapterView arg0)/TODOTODOTODOTODO Auto-generated method stubmyTextView.setText(NONE););/*下拉菜单弹出的内容选项 触屏事件处理*/mySpinner.setOnTouchListener(newnewnewnew Spinner.OnTouchLis

23、tener()publicpublicpublicpublic booleanbooleanbooleanboolean onTouch(View v,MotionEvent event)/TODOTODOTODOTODO Auto-generated method stub/*将mySpinner 运行Animation*/v.startAnimation(myAnimation);/*将mySpinner 隐藏*/v.setVisibility(View.INVISIBLE);returnreturnreturnreturn falsefalsefalsefalse;);/*下拉菜单弹出的

24、内容选项 焦点改变事件处理*/mySpinner.setOnFocusChangeListener(newnewnewnew Spinner.OnFocusChangeListener()publicpublicpublicpublic voidvoidvoidvoid onFocusChange(View v,booleanbooleanbooleanboolean hasFocus)/TODOTODOTODOTODO Auto-generated method stub);-Android 编程基础11动态添加动态添加动态添加动态添加删除的删除的删除的删除的 SpinnerSpinnerS

25、pinnerSpinner 菜单菜单菜单菜单ArrayList 与 Widget 的依赖性创建新工程修改 mian.xml 布局,添加 UI 元素Buttonandroid:id=+id/Button_DELandroid:layout_width=fill_parentandroid:layout_height=wrap_content-Android 编程基础12定义创建一个 List定义创建一个 ArrayAdapter设置 ArrayAdapter在下拉菜单中的显示布局给下拉菜单添加内容 Adapter 适配器android:text=删除/*定义*/privateprivatepri

26、vateprivate String cities;privateprivateprivateprivate List cityList;/*初始化字符串数组*/cities=newnewnewnew String Android,BlackBerry,J2ME,Symbian,Broncho,LinuxMobile,Palm,WindwosMobile;/*初始化List实例 ArrayList的对象*/cityList=newnewnewnew ArrayList();/*遍历,把字符串数组添加到ArrayList中*/forforforfor(intintintint i=0;i cit

27、ies.length;i+)cityList.add(citiesi);/*定义*/privateprivateprivateprivate ArrayAdapter arrayAdapter;/*这里应该使用List,如果使用String则会出错*/*初始化下拉菜单的内容适配器*/arrayAdapter=newnewnewnew ArrayAdapter(thisthisthisthis,android.R.layout.simple_spinner_item,cityList);/*设置下拉菜单显示内容的风格*/arrayAdapter.setDropDownViewResource(a

28、ndroid.R.layout.simple_spinner_dropdown_item);/*给下拉菜单对象添加内容适配器*/city_Spinner.setAdapter(arrayAdapter);-Android 编程基础13修改该 mainActivity.java,实现动态添加和删除packagepackagepackagepackage zyf.Ex_Ctrl_9ME;/*使用的包导入*/importimportimportimport java.util.ArrayList;importimportimportimport java.util.List;importimport

29、importimport android.app.Activity;importimportimportimport android.os.Bundle;importimportimportimport android.view.Menu;importimportimportimport android.view.MenuItem;importimportimportimport android.view.View;importimportimportimport android.widget.AdapterView;importimportimportimport android.widge

30、t.ArrayAdapter;importimportimportimport android.widget.Button;importimportimportimport android.widget.EditText;importimportimportimport android.widget.Spinner;importimportimportimport android.widget.TextView;importimportimportimport android.widget.Toast;publicpublicpublicpublic classclassclassclass

31、Ex_Ctrl_9ME extendsextendsextendsextends Activity implementsimplementsimplementsimplements Button.OnClickListener,Spinner.OnItemSelectedListener/*Called when the activity is first created.*/*声明程序使用的对象*/privateprivateprivateprivate TextView show_yourChoice_TextView;privateprivateprivateprivate EditTe

32、xt input_City_EditText;privateprivateprivateprivate Button Add_Button,Del_Button;privateprivateprivateprivate Spinner city_Spinner;privateprivateprivateprivate ArrayAdapter arrayAdapter;privateprivateprivateprivate String cities;privateprivateprivateprivate String addString;privateprivateprivatepriv

33、ate List cityList;Overridepublicpublicpublicpublic voidvoidvoidvoid onCreate(Bundle savedInstanceState)supersupersupersuper.onCreate(savedInstanceState);/*设置主屏显示布局为main.xml*/setContentView(R.layout.main);/*findViewByID()获取XML中的UI元素*/show_yourChoice_TextView=(TextView)findViewById(R.id.TextView_Show_

34、yourChoice);input_City_EditText=(EditText)findViewById(R.id.EditView_Input);Add_Button=(Button)findViewById(R.id.Button_ADD);Del_Button=(Button)findViewById(R.id.Button_DEL);city_Spinner=(Spinner)findViewById(R.id.Spinner_Slecte);/*初始化字符串数组*/cities=newnewnewnew String Android,BlackBerry,J2ME,-Androi

35、d 编程基础14Symbian,Broncho,LinuxMobile,Palm,WindwosMobile;/*初始化List实例 ArrayList的对象*/cityList=newnewnewnew ArrayList();/*遍历,把字符串数组添加到ArrayList中*/forforforfor(intintintint i=0;i cities.length;i+)cityList.add(citiesi);/*这里应该使用List,如果使用String则会出错*/*初始化下拉菜单的内容适配器*/arrayAdapter=newnewnewnew ArrayAdapter(this

36、thisthisthis,android.R.layout.simple_spinner_item,cityList);/*设置下拉菜单显示内容的风格*/arrayAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);/*给下拉菜单对象添加内容适配器*/city_Spinner.setAdapter(arrayAdapter);/*默认启动时 文本标题显示*/show_yourChoice_TextView.setText(arrayAdapter.getItem(0);/*默认启动时 下拉

37、菜单第一项被选中*/city_Spinner.setSelection(0);/*为按钮添加点击事件监听器*/Add_Button.setOnClickListener(thisthisthisthis);Del_Button.setOnClickListener(thisthisthisthis);/*为下拉菜单添加选项选中事件监听器*/city_Spinner.setOnItemSelectedListener(thisthisthisthis);Overridepublicpublicpublicpublic voidvoidvoidvoid onClick(View v)/TODOTO

38、DOTODOTODO Auto-generated method stubswitchswitchswitchswitch(v.getId()/*区别按钮来进行不同动作*/casecasecasecase R.id.Button_ADD:/*显示Toast提示*/Toast.makeText(thisthisthisthis,添加,Toast.LENGTH_SHORT).show();/*获得输入框中的将要添加的字符串*/addString=input_City_EditText.getText().toString();/*遍历,比较是否和下拉菜单中内容重复*/forforforfor(in

39、tintintint i=0;i arrayAdapter.getCount();i+)ifififif(addString.equals(arrayAdapter.getItemId(i)returnreturnreturnreturn;/*重复了,则跳出*/-Android 编程基础15/*如果添加字符串不为*/ifififif(!addString.equals()/*添加进适配器中*/arrayAdapter.add(addString);/*获取刚刚添加进的字符串位置*/intintintint post=arrayAdapter.getPosition(addString);/*设

40、置刚刚添加进的下拉菜单内容被选中*/city_Spinner.setSelection(post);/*清空输入框*/input_City_EditText.setText();breakbreakbreakbreak;casecasecasecase R.id.Button_DEL:ifififif(city_Spinner.getSelectedItem()!=nullnullnullnull)/*删除mySpinner 的值*/arrayAdapter.remove(city_Spinner.getSelectedItem().toString();/*将myEditText 清空*/i

41、nput_City_EditText.setText();ifififif(arrayAdapter.getCount()=0)/*将myTextView 清空*/Toast.makeText(thisthisthisthis,删完了,Toast.LENGTH_SHORT).show();show_yourChoice_TextView.setText();breakbreakbreakbreak;defaultdefaultdefaultdefault:breakbreakbreakbreak;/*下拉菜单选项被选中事件处理*/Overridepublicpublicpublicpublic

42、 voidvoidvoidvoid onItemSelected(AdapterView arg0,View arg1,intintintint arg2,longlonglonglong arg3)/TODOTODOTODOTODO Auto-generated method stubshow_yourChoice_TextView.setText(arrayAdapter.getItem(arg2);/*未被选中 事件处理*/Overridepublicpublicpublicpublic voidvoidvoidvoid onNothingSelected(AdapterView arg

43、0)/TODOTODOTODOTODO Auto-generated method stub-Android 编程基础16结果/*添加Menu菜单进行退出操作*/Overridepublicpublicpublicpublic booleanbooleanbooleanboolean onCreateOptionsMenu(Menu menu)/TODOTODOTODOTODO Auto-generated method stubmenu.add(Exit);returnreturnreturnreturn supersupersupersuper.onCreateOptionsMenu(me

44、nu);/*Menu菜单退出*/Overridepublicpublicpublicpublic booleanbooleanbooleanboolean onOptionsItemSelected(MenuItem item)/TODOTODOTODOTODO Auto-generated method stubfinish();returnreturnreturnreturn supersupersupersuper.onOptionsItemSelected(item);-Android 编程基础17相簿浏览相簿浏览 GalleryGalleryGalleryGalleryGallery

45、 与衍生 BaseAdapter 容器新建项目定义 layout 外部 resource 的 xml 文件,用来改变 layout 的背景修改 main.xml 布局,添加一个 Gallery 和一个 ImageView-Android 编程基础18新建一个 myImageAdapter 类-Gallery 的适配器,它继承于 BaseAdapter 类.修改 mainActivity.java,添加 Gallery 相关操作packagepackagepackagepackage zyf.Ex_Ctrl_10ME;importimportimportimport android.view.V

46、iew;importimportimportimport android.view.ViewGroup;importimportimportimport android.widget.BaseAdapter;publicpublicpublicpublic classclassclassclass myImageAdapter extendsextendsextendsextends BaseAdapter Overridepublicpublicpublicpublic intintintint getCount()/TODOTODOTODOTODO Auto-generated metho

47、d stubreturnreturnreturnreturn 0;Overridepublicpublicpublicpublic Object getItem(intintintint position)/TODOTODOTODOTODO Auto-generated method stubreturnreturnreturnreturn nullnullnullnull;Overridepublicpublicpublicpublic longlonglonglong getItemId(intintintint position)/TODOTODOTODOTODO Auto-genera

48、ted method stubreturnreturnreturnreturn 0;Overridepublicpublicpublicpublic View getView(intintintint position,View convertView,ViewGroup parent)/TODOTODOTODOTODO Auto-generated method stubreturnreturnreturnreturn nullnullnullnull;packagepackagepackagepackage zyf.Ex_Ctrl_10ME;importimportimportimport

49、 android.app.Activity;importimportimportimport android.os.Bundle;importimportimportimport android.view.View;importimportimportimport android.widget.AdapterView;importimportimportimport android.widget.Gallery;importimportimportimport android.widget.ImageView;importimportimportimport android.widget.To

50、ast;publicpublicpublicpublic classclassclassclass Ex_Ctrl_10ME extendsextendsextendsextends Activity/*Called when the activity is first created.*/*定义要使用的对象*/privateprivateprivateprivate Gallery gallery;privateprivateprivateprivate ImageView imageview;-Android 编程基础19修改 myImageAdapter.java 文件,实现相簿浏览效果

展开阅读全文
相关资源
相关搜索

当前位置:首页 > 技术资料 > 其他杂项

本站为文档C TO C交易模式,本站只提供存储空间、用户上传的文档直接被用户下载,本站只是中间服务平台,本站所有文档下载所得的收益归上传人(含作者)所有。本站仅对用户上传内容的表现方式做保护处理,对上载内容本身不做任何修改或编辑。若文档所含内容侵犯了您的版权或隐私,请立即通知淘文阁网,我们立即给予删除!客服QQ:136780468 微信:18945177775 电话:18904686070

工信部备案号:黑ICP备15003705号© 2020-2023 www.taowenge.com 淘文阁