Android学习笔记之Intent(意图)
2016-06-05 22:26
411 查看
/** *@author StormMaybin *@Date 2016-06-05 22:25 */
Intent简介
Intent中文名字意图的意思,之一的功能可以启动不同的Activity。Intent分别为隐式Intent和显式Intent显式Intent:
先在布局文件中定义一个BUtton,然后对Button实行监听事件。然后重写onClick方法来启动另外一个Activity<Button android:id="@+id/button1" android:layout_width="match_parent" android:layout_height="wrap_content" android:background="#FFFFFFFF" android:txet="@string/button_Name"/> <!--其中button_Name="启动第二个Activity"-->
除此之外,还要新建一个Activity,用来作为按下Button键之后启动的活动
public class SActivity extends Activity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.sActivity); }
然后对应的在layout文件下新建一个布局文件用作第二个Activity的布局,然后接下来在主活动中添加Button的监听器来启动第二个活动,除此之外我们创建的SActivity进行注册,在AndroidManifest.xml文件中进行注册(注册细节省略)
······ private Button button1; //然后再onCreat方法中进行初始化 button1 = (Button)findViewById(R.id.button1); button1.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { //创建Intent对象 Intent = intent = new Intent(Main_Activity.this, SActivity); //调用startActivity方法参数是Intent对象 //这样就当Button被点击就会启动第二个Activity startActivity(intent); } });
隐式Intent
隐式Intent和显式Intent不太一样隐式Intent用法,先要在AndroidManifest.xml文件中注册SActivity。
<activity android:name=".SActivity" > <intent-filter> <action android:name="com.example.actvitytest.ACTION_START" /> <category android:name="android.intent.category.DEFAULT" /> </activity>
然后在onCreat方法中添加以下代码:
button1.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { Intent intent = new Intent("com.example.activitytest.ACTION_START"); startActivity(intent); } });
上面这个代码就是使用隐式Intent实现页面跳转,部分代码和显式Intent代码相同,所以省之。
相关文章推荐
- 使用C++实现JNI接口需要注意的事项
- Android IPC进程间通讯机制
- Android Manifest 用法
- [转载]Activity中ConfigChanges属性的用法
- Android之获取手机上的图片和视频缩略图thumbnails
- Android之使用Http协议实现文件上传功能
- Android学习笔记(二九):嵌入浏览器
- android string.xml文件中的整型和string型代替
- i-jetty环境搭配与编译
- android之定时器AlarmManager
- android wifi 无线调试
- Android Native 绘图方法
- Android java 与 javascript互访(相互调用)的方法例子
- android 代码实现控件之间的间距
- android FragmentPagerAdapter的“标准”配置
- Android"解决"onTouch和onClick的冲突问题
- android:installLocation简析
- android searchView的关闭事件
- SourceProvider.getJniDirectories