标签:
1.跳转:意图的2种形式 class和action(intent-filter要加意图过滤),开启Activity即可
2.传值:意图put/get值
3.日志记录:Log.i i---info信息记录
Manifest.xml:清单文件
<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.manusas.activitydemo"> <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" android:label="@string/app_name"> <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> <!--几个Activity几个配置--> <activity android:name=".NextActivity" android:label="@string/app_name"> <intent-filter> <action android:name="nextaction" /> <category android:name="android.intent.category.DEFAULT" /> </intent-filter> </activity> </application> </manifest>
MainActivity.java
package com.manusas.activitydemo; import android.app.Activity; 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 Activity { private Button button; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); button = (Button) this.findViewById(R.id.button); button.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { //1跳转 // 方法一二 根据意图Class //方法一 最常用 Intent i = new Intent(MainActivity.this, NextActivity.class); //方法二 // Intent i = new Intent(); // i.setClass(MainActivity.this, NextActivity.class); // 方法三四 根据意图Action 在清单文件里面activity里面<intent-filter>一定要定义action名 //方法三 //Intent i = new Intent("nextaction"); //方法四 // Intent i = new Intent(); // i.setAction("nextaction"); //2传值 i.putExtra("name","liang"); i.putExtra("age",40); Bundle bundle=new Bundle(); bundle.putString("nickname","manusas"); i.putExtra("bundle",bundle); startActivity(i); } }); } }
NextActivity.java
package com.manusas.activitydemo; import android.app.Activity; import android.content.Intent; import android.os.Bundle; import android.util.Log; import android.view.View; import android.widget.Button; public class NextActivity extends Activity { public static final String TAG = "tag"; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_next); //1取值 Intent i = getIntent(); String name = i.getStringExtra("name"); int age = i.getIntExtra("age", 0); Bundle bundle = i.getBundleExtra("bundle"); String nickname = bundle.getString("nickname"); Log.i(TAG, name + nickname + age); } }
activity_main.xml:主页
<?xml version="1.0" encoding="utf-8"?> <RelativeLayout 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.manusas.activitydemo.MainActivity"> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Next" android:id="@+id/button" android:layout_alignParentTop="true" android:layout_alignParentStart="true" /> </RelativeLayout>
activity_next.xml:跳转页
<?xml version="1.0" encoding="utf-8"?> <RelativeLayout 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.manusas.activitydemo.MainActivity"> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Ok" android:id="@+id/textView" android:layout_alignParentTop="true" android:layout_alignParentStart="true" /> </RelativeLayout>
标签:
原文地址:http://www.cnblogs.com/manusas/p/5428132.html