Android笔记资源.docx
- 文档编号:16728270
- 上传时间:2023-07-16
- 格式:DOCX
- 页数:43
- 大小:740.64KB
Android笔记资源.docx
《Android笔记资源.docx》由会员分享,可在线阅读,更多相关《Android笔记资源.docx(43页珍藏版)》请在冰点文库上搜索。
Android笔记资源
四什么是Intent
1mainBtn=(Button)findViewById(R.id.mainBtn);
mainBtn.setOnClickListener(listener);
}添加OnClickListener事件在mainactivity里
创建privateOnClickListenerlistener=newOnClickListener()
{
2拨打电话
意图IntentpublicvoidonClick(Viewv)
{
Intentintent=newIntent();
intent.setAction(Intent.ACTION_SENDTO);
intent.setData(Uri.Parse(“smsto:
110”))
startActivity(intent);
把相关的权限放在AndroidManifest.xml里
得下面 name="android.permission.CALL_PHONE"/> 3发送短信 Intentintent=newIntent(Intent.ACTION_SENDTO,Uri.parse("smsto: 5554"));短信电话号 intent.putExtra("sms_body","Hello");短信内容 startActivity(intent); 把相关的权限放在AndroidManifest.xml里 得下面 name="android.permission.SEND_SMS"/> 4启动新的Activity 增加使用父类的方法或快捷键shift+alt+s 调用Intent方法 intent.setClass(MainActivity.this,SecondActivity class);第一个参数指向当前对象,第二个指向跳转的Activty 5传参数到第二行Activty(f2聚焦) intent.putExtra("str","IntentDemo"); 注意: 每创建一个Activty在AndroidManifest.xml注册 name=".SecondActivity" android: label="@string/app_name"> 接受传值 Intentintent=getIntent(); Bundlebundle=intent.getExtras(); Stringstr=bundle.getString("str"); 当结束时结束当前avtivity返回到调用的avtivity ctrl+shift+s变成大写 结束时 secondTxt=(TextView)findViewById(R.id.secondTxt); secondTxt.setText(str); secondBtn=(Button)findViewById(R.id.secondBtn); secondBtn.setOnClickListener(listener); } privateOnClickListenerlistener=newOnClickListener() { @Override publicvoidonClick(Viewv) { Intentintent=newIntent(); intent.putExtra("back","BackData"); setResult(RESULT_CODE,intent); finish(); } }; }························· 返回的Activity startActivityForResult(intent,REQUEST_CODE); @Override protectedvoidonActivityResult(intrequestCode,intresultCode,Intentdata) { if(requestCode==REQUEST_CODE) { if(resultCode==SecondActivity.RESULT_CODE) { Bundlebundle=data.getExtras(); Stringstr=bundle.getString("back"); Toast.makeText(MainActivity.this,str,Toast.LENGTH_LONG).show(); } 五: Activity生命周期 1Log类简单的使用 添加过滤器; Log.v(Stringtag,Stringmsg);//VERBOSE Log.d(Stringtag,Stringmsg);//DEBUG Log.i(Stringtag,Stringmsg);//INFO Log.w(Stringtag,Stringmsg);//WARN Log.e(Stringtag,Stringmsg);//ERROR ··Activity生命周期有点晕! protectedvoidonCreate(BundlesavedInstanceState); 第一个调用的方法如布局文件静态的操作 protectedvoidonStart(); 当第二个activity能被我们看到的时候, protectedvoidonRestart(); 当我们的activity获得用户的焦点的能操作的时候 protectedvoidonResume(); protectedvoidonPause(); 启动另一个activity的时候 protectedvoidonStop(); 结束的时候,处于不可看见的时候。 protectedvoidonDestroy(); 销毁 1、onCreate() Acitivity首次创建时被调用。 用于设置Acitivity的布局文件,绑定按钮监听器等一些普通静态操作。 2、onStart() Acitivity对用户可见时被调用。 3、onResume() Acitivity获得用户焦点,即用户可操作该Acitivity时被调用。 4、onPause() 应用程序启动了其它Acitivity时被调用。 一般用于保存当前Acitivity中的数据。 5、onStop() Acitivity不可见时被调用。 6、onRestart() 已停止的Acitivity重新启动时被调用。 7、onDestroy() 调用Acitivity的finish()方法或Android系统资源不足时被调用 ··保存Activity状态 onSaveInstanceState() publicvoidonCreate(BundlesavedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); txt=(EditText)findViewById(R.id.txt); if(null! =savedInstanceState&&savedInstanceState.containsKey(CONTENT)) { txt.setText(savedInstanceState.getString(CONTENT)); } secondBtn=(Button)findViewById(R.id.secondBtn); secondBtn.setOnClickListener(listener); Log.i(TAG,"MainActivity-->onCreate"); } ················· protectedvoidonSaveInstanceState(BundleoutState) { super.onSaveInstanceState(outState); Log.i(TAG,"MainActivity-->onSaveInstanceState"); Stringcontent=txt.getText().toString(); outState.putString(CONTENT,content); } ···android: theme android: theme="@android: style/Theme.Dialog" android: theme="@android: style/Theme.NoTitleBar" android: theme="@android: style/Theme.NoTitleBar.Fullscreen"全屏效果 android: theme="Theme.Light"背景颜色变白色 android: theme="Theme.Light.NoTitleBar" android: theme="Theme.Light.NoTitleBar.Fullscreen" android: theme="Theme.Black" android: theme="Theme.Black.NoTitleBar" android: theme="Theme.Black.NoTitleBar.Fullscreen" ···· android: theme="Theme.Wallpaper"和背景一样的效果 android: theme="Theme.Wallpaper.NoTitleBar" android: theme="Theme.Wallpaper.NoTitleBar.Fullscreen" android: theme="Translucent"透明的 android: theme="Theme.Translucent.NoTitleBar" android: theme="Theme.Translucent.NoTitleBar.Fullscreen" android: theme="Theme.Panel"显示文本框等 android: theme="Theme.Light.Panel" 六;LinearLayout布局线性布局 通过android: orientation属性可以设置线性布局的方向 vertical垂直方向 android: gravity该属性用于控制布局中控件的对齐方式 android: layout_weight属性以控制各个控件在布局中的相对大小。 layout_weight属性是一个非负整数值。 线性布局会根据该控件layout_weight值与其所处布局中所有控件layout_weight值之和的比值为该控件分配占用的区域。 例如,在水平布局的LinearLayout中有两个Button,这两个Button的layout_weight属性值都为1,那么这两个按钮都会被拉伸到整个屏幕宽度的一半。 如果layout_weight指为0,控件会按原大小显示,不会被拉伸;对于其余layout_weight属性值大于0的控件,系统将会减去layout_weight属性值为0的控件的宽度或者高度,再用剩余的宽度或高度按相应的比例来分配每一个控件显示的宽度或高度 android=" android: orientation="vertical" android: layout_width="fill_parent" android: layout_height="fill_parent"> android: orientation="horizontal" android: layout_width="fill_parent" android: layout_height="fill_parent" android: layout_weight="1"> android: text="红色" android: gravity="center_horizontal|center_vertical"水平垂直居中 android: background="#aa0000" android: layout_width="wrap_content" android: layout_height="fill_parent" android: layout_weight="1"/> android: text="绿色" android: gravity="center_horizontal"水平居中 android: background="#00aa00" android: layout_width="wrap_content" android: layout_height="fill_parent" android: layout_weight="1"/> android: text="蓝色" android: gravity="center|bottom"底部居中 android: background="#0000aa" android: layout_width="wrap_content" android: layout_height="fill_parent" android: layout_weight="1"/> android: text="黄色" android: gravity="bottom"底部默认左边 android: background="#aaaa00" android: layout_width="wrap_content" android: layout_height="fill_parent" android: layout_weight="1"/> android: orientation="vertical" android: layout_width="fill_parent" android: layout_height="fill_parent" android: layout_weight="1"> android: text="第一行" android: textSize="15sp" android: layout_width="fill_parent" android: layout_height="wrap_content" android: layout_weight="1"/> android: text="第二行" android: textSize="15sp" android: layout_width="fill_parent" android: layout_height="wrap_content" android: layout_weight="1"/> android: text="第三行" android: textSize="15sp" android: layout_width="fill_parent" android: layout_height="wrap_content" android: layout_weight="1"/> android: text="第四列" android: textSize="15sp" android: layout_width="fill_parent" android: layout_height="wrap_content" android: layout_weight="1"/> 七AbsoluteLayout指定子控件的xy精确坐标的布局。 绝对布局缺乏灵活性,在没有绝对定位的情况下相比其他类型的布局更难维护。 确定用户屏幕大小android: layout_x="10px" android: layout_y="10px" FrameLayout所有添加到这个布局中的视图都以层叠的方式显示。 第一个添加的组件放到最底层,最后添加到框架中的视图显示在最上面。 上一层的会覆盖下一层的控件 ··对大体的客户 默认在左上角的 android: layout_gravity=”left|top”容器 八RelativeLayout相对布局,在这个容器内部的子元素们可以使用彼此之间的相对位置或者和容器间的相对位置来进行定位。 注意: 不能在RelativeLayout容器本身和他的子元素之间产生循环依赖,比如说,不能将RelativeLayout的高设置成为WRAP_CONTENT的时候将子元素的高设置成为ALIGN_PARENT_BOTTOM。 位置相关 android: layout_above将该控件置于给定ID的控件之上 android: layout_below将该控件的置于给定ID控件之下 android: layout_above=”@id/txt01” android: layout_toLeftOf将该控件置于给定ID的控件之左 android: layout_toRightOf将该控件置于给定ID的控件之右 对齐相关 android: layout_alignBaseline该控件基线对齐给定ID的基线 android: layout_alignBottom该控件于给定ID的控件底部对齐 android: layout_alignLeft该控件于给定ID的控件左对齐 android: layout_alignRight该控件于给定ID的控件右对齐 android: layout_alignTop该控件于给定ID的控件顶对齐 ··控件与容器之间的 android: layout_alignParentLeft如果为True,该控件位于父控件的左部 android: layout_alignParentRight如果为True,该控件位于父控件的右部 android: layout_alignParentTop如果为True,该控件位于父控件的顶部 android: layout_alignParentBottom如果为True,该控件位于父控件的底部 ···水平与垂直方向 android: layout_centerHorizontal如果为True,该控件将被置于水平方向的中央 android: layout_centerInParent如为Ture,该控件将被置于父控件水平方向和垂直方向 android: layout_centerVertical如果为True,该控件将被置于垂直方向的中央 ···控件与容器之间的间隔大小 android: padding=“10px” ··改变控件之间的间隔 android: layout_margin= ``TableLayout 表格布局是一个ViewGroup以表格显示它的子视图(view)元素,即行和列标识一个视图的位置。 Android的表格布局跟HTML中的表格布局非常类似,TableRow就像HTML表格的 放在tablelayout下 android: collapseColumns: 隐藏指定的列=(从0开始的)(0,1,2)隐藏三列 android: shrinkColumns: 收缩指定的列以适合屏幕,不会挤出屏幕(会自动换行) android: stretchColumns: 尽量把指定的列填充空白部分(自动补全) 放在TableRow的子控件里 android: layout_column: 控件放在指定的列(从0开始) android: layout_span: 该控件所跨越的列数(必须大于等于1) android: layout_alignbaseline=”@+id/btn”按钮中中文汉字对齐 九单元测试 Step1: 在AndroidManifest.xml中天下如下配置信息: name="android.test.runner"/> name="android.test.InstrumentationTestRunner"android: targetPackage="szy.android.activity"android: label="Testing"/> Step2: 编写单元测试方法 publicclassTestCaseextendsAndroidTestCase {} 表示测试通过 创建测试类对创建的类,右击选择JunitTestCase再Superclass选择andriodtestcase下一步选择测试的方法。 完成。 推荐创建SourceFolder目录与SRC是同级的放一些测试相关的类 Log日志输出 Windowreset恢复原来的窗口 Debug技能学习缺少资源 十Service服务 相当于后台服务功能 Service是在要执行很长时间的耗时的操作,不需要与用户进行交互和进程Thread需要和数据进行交互。 线程 使用Service的方式 Started startService(): 调用者和服务之间没有联系,即使调用者退出了,服务仍然进行[onCreate()-->onStart()-->startService()-onDestory()] Bound bindService(): 调用者和服务绑在一起,调用者一旦退出服务也就终止[onCreate()-->onBind()-->onUnbind()-->onDestory()] 通过startService()使用Service 编写类继承Service或其子类 复写方法 onStartCommand()onBind()onCreate()onDestroy() onBind()不需要提供任何操作 在manifest文件中声服务 name=".Service"/> 和acti 如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。标记。