/**
*@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代码相同,所以省之。