android页面跳转之——activity

来源:互联网 发布:java设置日期格式 编辑:程序博客网 时间:2024/06/09 18:59

、、Activity的跳转

     显示启动:明确指定要启动的Activity的class或者 包名.activtiy类名

     隐式启动:设置启动过滤器,通过指定的action或者action和data属性,系统会查找符合条件的activity并启动他。

    显示启动:

       方式一:Class跳转

             Intent intent = new Intent(Activity1.this, Activtiy2.class);

            starActivtiy(intent);

      方式二:包名.类名跳转

            Intent intent = new Intent();  om.maizi.lesson.activtiylesson

            intent.setClassName(context, "c1");

            starActivity(intent);

      方式三:ComponentName跳转

           Intent intent = new Intent();

           intent.setComponent(new Component(content,Activtiy2.class))

           startActivity(intent);

//开始创建(模拟通过Intent在两个 Activtiy之间进行跳转)

  1.在原有项目的基础上创建  Activtity_second  文件

package com.example.administrator.activtiytest;import android.support.v7.app.AppCompatActivity;import android.os.Bundle;public class Activtiy_Second extends AppCompatActivity {    @Override    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);
//Activtiy显示第二个布局文件内容        setContentView(R.layout.activity_activtiy__second);    }}
2.对第二个Activtiy添加布局资源文件
<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"    xmlns:tools="http://schemas.android.com/tools"    android:layout_width="match_parent"    android:layout_height="match_parent"    android:paddingBottom="@dimen/activity_vertical_margin"    android:paddingLeft="@dimen/activity_horizontal_margin"    android:paddingRight="@dimen/activity_horizontal_margin"    android:paddingTop="@dimen/activity_vertical_margin"    tools:context="com.example.administrator.activtiytest.Activtiy_Second">    <TextView        android:layout_width="match_parent"        android:layout_height="match_parent"        android:text="这是第二个Activtiy"/></LinearLayout>
3.关联Activty和布局文件使得第二个Activtiy显示出“等同于Hello_world的效果。
4.在AndroidMinafast.xml文件中注册Activty。
<?xml version="1.0" encoding="utf-8"?><manifest xmlns:android="http://schemas.android.com/apk/res/android"    package="com.example.administrator.activtiytest">    <application        android:allowBackup="true"        android:icon="@mipmap/ic_launcher"        android:label="@string/app_name"        android:supportsRtl="true"        android:theme="@style/AppTheme">        <activity android:name=".MainActivity">            <intent-filter>                <action android:name="android.intent.action.MAIN" />                <category android:name="android.intent.category.LAUNCHER" />            </intent-filter>        </activity>
//注册第二个Activtiy文件。        <activity android:name=".Activtiy_Second"></activity>    </application></manifest>
5.添加按钮控件实现点击事件在两个Activity之间跳转。
、、使用 Class跳转
package com.example.administrator.activtiytest;import android.content.DialogInterface;import android.content.Intent;import android.support.v7.app.AppCompatActivity;import android.os.Bundle;import android.view.View;import android.widget.Button;public class MainActivity extends AppCompatActivity {    Button button_start;    @Override    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.activity_main);        button_start = (Button) findViewById(R.id.btn_strat_second);        button_start.setOnClickListener(new View.OnClickListener() {            @Override            public void onClick(View v) {                Intent intent = new Intent();                intent.setClass(MainActivity.this, Activtiy_Second.class);                startActivity(intent);            }        });    }}
、、包名.类名跳转
  

   


原创粉丝点击