你的位置:首页 > 操作系统

[操作系统]android从一个Activity转到另一个Activity的方式:


MainActivity:点击一个Button会跳转到另一个NextActivity

package com.example;import java.util.ArrayList;import android.os.Bundle;import android.app.Activity;import android.content.Intent;import android.view.Menu;import android.view.View;import android.view.View.OnClickListener;import android.widget.Button;public class MainActivity extends Activity {  @Override  protected void onCreate(Bundle savedInstanceState) {    super.onCreate(savedInstanceState);    setContentView(R.layout.activity_main);    Button button = (Button) findViewById(R.id.but1);    button.setOnClickListener(new OnClickListener() {      @Override      public void onClick(View v) {        // TODO Auto-generated method stub        Intent intent = new Intent(MainActivity.this,            NextActivity.class);        /*         * 第二种         * Intent intent2=new Intent();         * intent2.setClass(MainActivity.this, NextActivity.class);         *第三种:         * Intent intent3 =new Intent("com.example.NextActivity");         *注意:此方法一定要在Manifest清单文件中配置intent-filter标签中配置action属性         *         * 等价于:         * Intent intent3 =new Intent("com.example.NextActivity");         * intent3.setAction(com.example.NextActivity");         *         * 最后不要忘记有个:startActivity(intent)方法哦!         */        intent.putExtra("name", "zhangsan");        intent.putExtra("age", 20);        ArrayList<String> arrayList = new ArrayList<String>();        arrayList.add("1");        arrayList.add("2");        arrayList.add("4");        intent.putStringArrayListExtra("unknow", arrayList);        startActivity(intent);      }    });  }  @Override  public boolean onCreateOptionsMenu(Menu menu) {    // Inflate the menu; this adds items to the action bar if it is present.    getMenuInflater().inflate(R.menu.main, menu);    return true;  }}

AndroidManifest文件:

<?package="com.example"  android:versionCode="1"  android:versionName="1.0" >  <uses-sdk    android:minSdkVersion="8"    android:targetSdkVersion="17" />  <application    android:allowBackup="true"    android:icon="@drawable/ic_launcher"    android:label="@string/app_name"    android:theme="@style/AppTheme" >    <activity      android:name="com.example.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 android:name=".NextActivity">      <intent-filter >        <action android: name="com.example.NextActivity"/>        <category android:name="android.intent.category.DEFAULT" />      </intent-filter>          </activity>  </application></manifest>


在NextActivity中得到由MainActivity传过来的数据:

package com.example;

import java.util.ArrayList;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;

public class NextActivity extends Activity {
    private final String TAG = "NextActivity";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_next);
        Intent intent = getIntent();
        String name = intent.getStringExtra("name");
        Log.i(TAG, "--name-->>" + name);
        int age = intent.getIntExtra("age", 0);
        Log.i(TAG, "--age-->>" + age);
        ArrayList<String> arrayList = intent.getStringArrayListExtra("unknow");
        for (String s : arrayList) {
            Log.i(TAG, s);

        }

    }

}