Android切换显示窗体-Intent

看书-学习-实操-总结-消化

Intent的基本作用 首先,Android应用程序是由数个Activity所组成的,当用户切换显示窗体时,就需要调用Intent类来完成。 Intent可以带有数据,简单地说,它是android设备不同组件连接的桥梁。 Intent用法说明 Intent使用分为显式和隐式两种。 1.显式Intent启动Activity 显式启动Activity,通常就是在一个应用程序中由其中Activity启动另一个Activity。每个Activity都要在manifest当中给以定义。 显式启动一个Activity的过程为:创建一个新的Intent,并指定当前Activity和要启动的Activity,进而使用startActivity(Intent intent)方法进行启动。将这个过程添加在操作代码中(如button,menuItem等的响应部分) 例如,从Activity01切换到Activity02 Intentintent= newIntent();
intent.setClass(Activity01. this,Activity02. class);
Activity01. this.startActivity(intent);

上面这代码是不带数据的跳转,下面以一个简单的例子记录数据在Activity中的传递方法。

Activity01传入string数据 Intentintent= newIntent();
intent.putExtra("TEXT",text);
intent.setClass(Activity01. this,Activity02. class);
Activity01. this.startActivity(intent); 在Activity02中接受数据 Intentintent=getIntent();
Stringstr=intent.getStringExtra("TEXT"); 2.隐式Intent启动Activity 隐式调用Intent首先需要了解intent的携带的一个重要参数action,在android.intent.action下包括许多Activity方法,比如VIEW,WEB_SEARCH,CALL等等。系统会根据所指定的action来自动去匹配最合适的程序Activity来执行该动作。 例如android源码中Music应用程序的VideoBrowserActivity.java中,当点击列表中的某个视频文件后,跳转去调用系统自带播放器播放。 Intentintent= newIntent(Intent.ACTION_VIEW);
mCursor.moveToPosition(position);
Stringtype=mCursor.getString(mCursor.getColumnIndexOrThrow(MediaStore.Video.Media.MIME_TYPE));
intent.setDataAndType(ContentUris.withAppendedId(MediaStore.Video.Media.EXTERNAL_CONTENT_URI,id),type);
Log.i("VIDEOFILENAME",mCursor.getString(mCursor.getColumnIndexOrThrow(MediaStore.Video.Media.TITLE)));
startActivity(intent);


Layout

关于layout, 在此记录下常用的三种:

1.LinearLayout LinearLayout分为vertical和horizontal两种 不同的layout可以嵌套使用 例子 <? xmlversion="1.0"encoding="utf-8" ?>
< LinearLayout xmlns:android ="http://schemas.android.com/apk/res/android"
android:orientation
="vertical"
android:layout_width
="fill_parent"
android:layout_height
="fill_parent"
>
< LinearLayout
android:orientation ="horizontal"
android:layout_width
="fill_parent"
android:layout_height
="fill_parent"
android:layout_weight
="1" >
< TextView
android:text ="red"
android:gravity
="center_horizontal"
android:background
="#aa0000"
android:layout_width
="wrap_content"
android:layout_height
="fill_parent"
android:layout_weight
="1" />
< TextView
android:text ="green"
android:gravity
="center_horizontal"
android:background
="#00aa00"
android:layout_width
="wrap_content"
android:layout_height
="fill_parent"
android:layout_weight
="1" />
< TextView
android:text ="blue"
android:gravity
="center_horizontal"
android:background
="#0000aa"
android:layout_width
="wrap_content"
android:layout_height
="fill_parent"
android:layout_weight
="1" />
< TextView
android:text ="yellow"
android:gravity
="center_horizontal"
android:background
="#aaaa00"
android:layout_width
="wrap_content"
android:layout_height
="fill_parent"
android:layout_weight
="1" />
</ LinearLayout >


< LinearLayout
android:orientation ="vertical"
android:layout_width
="fill_parent"
android:layout_height
="fill_parent"
android:layout_weight
="1" >
< TextView
android:text ="rowone"
android:textSize
="15pt"
android:layout_width
="fill_parent"
android:layout_height
="wrap_content"
android:layout_weight
="1" />
< TextView
android:text ="rowtwo"
android:textSize
="15pt"
android:layout_width
="fill_parent"
android:layout_height
="wrap_content"
android:layout_weight
="1" />
< TextView
android:text ="rowthree"
android:textSize
="15pt"
android:layout_width
="fill_parent"
android:layout_height
="wrap_content"
android:layout_weight
="1" />
< TextView
android:text ="rowfour"
android:textSize
="15pt"
android:layout_width
="fill_parent"
android:layout_height
="wrap_content"
android:layout_weight
="1" />
</ LinearLayout >
</ LinearLayout > 一个LinearLayout中嵌套水平和垂直两种LinearLayout排列

2.TableLayout 例子 <? xmlversion="1.0"encoding="utf-8" ?>
< LinearLayout xmlns:android ="http://schemas.android.com/apk/res/android"
android:orientation
="vertical" android:layout_width ="fill_parent"
android:layout_height
="fill_parent" >
< LinearLayout
android:orientation ="horizontal"
android:layout_width
="fill_parent"
android:layout_height
="fill_parent"
android:layout_weight
="1" >
< TextView
android:text ="red"
android:gravity
="center_horizontal"
android:background
="#aa0000"
android:layout_width
="wrap_content"
android:layout_height
="fill_parent"
android:layout_weight
="1" />
< TextView
android:text ="green"
android:gravity
="center_horizontal"
android:background
="#00aa00"
android:layout_width
="wrap_content"
android:layout_height
="fill_parent"
android:layout_weight
="1" />
< TextView
android:text ="blue"
android:gravity
="center_horizontal"
android:background
="#0000aa"
android:layout_width
="wrap_content"
android:layout_height
="fill_parent"
android:layout_weight
="1" />
< TextView
android:text ="yellow"
android:gravity
="center_horizontal"
android:background
="#aaaa00"
android:layout_width
="wrap_content"
android:layout_height
="fill_parent"
android:layout_weight
="1" />
</ LinearLayout >


< LinearLayout
android:orientation ="horizontal"
android:layout_width
="fill_parent"
android:layout_height
="fill_parent"
android:layout_weight
="1" >
< TableLayout
xmlns:android ="http://schemas.android.com/apk/res/android"
android:layout_width
="fill_parent"
android:layout_height
="fill_parent"
android:stretchColumns
="0" >
< TableRow >
< TextView
android:text ="@string/row1_column1"
android:padding
="3dip" />
< TextView
android:text ="@string/row1_column1"
android:padding
="3dip"
android:gravity
="center_horizontal" >
</ TextView >
< TextView
android:text ="@string/row1_column2"
android:gravity
="right"
android:padding
="3dip" />
</ TableRow >

< TableRow >
< TextView
android:text ="@string/row2_column1"
android:padding
="3dip" />
< TextView
android:text ="@string/row2_column2"
android:gravity
="right"
android:padding
="3dip" />
</ TableRow >
</ TableLayout >
</ LinearLayout >
</ LinearLayout >

3.RelativeLayout 相对布局的布局方式是设定单元和单元的上下左右关联,至少要定义两个位置之间的关联。 对于相对布局中可以定义的参数,Mars老师的一个归纳很详细 android:layout_above将该控件的底部至于给定ID的控件之上
android:layout_below将该控件的顶部至于给定ID的控件之下
android:layout_toLeftOf将该控件的右边缘和给定ID的控件的左边缘对齐
android:layout_toRightOf将该控件的左边缘和给定ID的控件的右边缘对齐

android:layout_alignBaseline该控件的baseline和给定ID的控件的baseline对齐
android:layout_alignBottom将该控件的底部边缘与给定ID控件的底部边缘
android:layout_alignLeft将该控件的左边缘与给定ID控件的左边缘对齐
android:layout_alignRight将该控件的右边缘与给定ID控件的右边缘对齐
android:layout_alignTop将给定控件的顶部边缘与给定ID控件的顶部对齐

android:alignParentBottom如果该值为true,则将该控件的底部和父控件的底部对齐
android:layout_alignParentLeft如果该值为true,则将该控件的左边与父控件的左边对齐
android:layout_alignParentRight如果该值为true,则将该控件的右边与父控件的右边对齐
android:layout_alignParentTop如果该值为true,则将空间的顶部与父控件的顶部对齐

android:layout_centerHorizontal如果值为真,该控件将被至于水平方向的中央
android:layout_centerInParent如果值为真,该控件将被至于父控件水平方向和垂直方向的中央
android:layout_centerVertical如果值为真,该控件将被至于垂直方向的中央 google官方的一个例子: < RelativeLayout xmlns:android ="http://schemas.android.com/apk/res/android"
android:layout_width
="fill_parent"
android:layout_height
="wrap_content"
android:padding
="10px" >

< TextView android:id ="@+id/label"
android:layout_width
="fill_parent"
android:layout_height
="wrap_content"
android:text
="Typehere:" />

< EditText android:id ="@+id/entry"
android:layout_width
="fill_parent"
android:layout_height
="wrap_content"
android:background
="@android:drawable/editbox_background"
android:layout_below
="@id/label" />

< Button android:id ="@+id/ok"
android:layout_width
="wrap_content"
android:layout_height
="wrap_content"
android:layout_below
="@id/entry"
android:layout_alignParentRight
="true"
android:layout_marginLeft
="10px"
android:text
="OK" />

< Button android:layout_width ="wrap_content"
android:layout_height
="wrap_content"
android:layout_toLeftOf
="@id/ok"
android:layout_alignTop
="@id/ok"
android:text
="Cancel" />
</ RelativeLayout > 效果如下

更多相关文章

  1. Android 从properties配置文件读取数据
  2. 我的android 第16天 - SQLite数据库
  3. android 数据话持久化——SQLite
  4. Android数据持久化之SQLite数据库用法分析
  5. Android之进度条控件和常用资源分类总结
  6. Android评分控件RatingBar使用实例解析
  7. android SQLite数据库的基本操作
  8. Android五种数据存储方式

随机推荐

  1. Android开发之旅:进程与线程
  2. HorizontalScrollView 水平动态生成TextV
  3. Android RecyclerView基本使用
  4. [置顶] 入门Android开发--总目录
  5. 从零开始-android 4.2之旅
  6. android中使用PopupWindow实现弹出窗口菜
  7. LTP在Android上的移植
  8. android 百度地图api使用问题
  9. android(3) 启动动画的渐变
  10. android中sqlite数据库操作