android 跳转并传递参数
?
package com.sun.hello;
import android.app.Activity;
import android.content.Intent;
import android.location.GpsStatus.Listener;
import android.net.Uri;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.Toast;
?
public class HelloActivity extends Activity {
private Button button = null;
?
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
button = (Button) findViewById(R.id.but1);
button.setOnClickListener(listener);
}
private OnClickListener listener = new OnClickListener() {
public void onClick(View v) {
// TODO Auto-generated method stub
Intent intent = new Intent();
intent.setClass(HelloActivity.this, SecondActivity.class);//
intent.putExtra("str","Intent demo");
startActivity(intent);
?
}
};
}
?
SecondActivity。java
package com.sun.hello;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.widget.Button;
import android.widget.TextView;
?
public class SecondActivity extends Activity {
private Button button;
private TextView secondTxt;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.secondmain);
Intent intent=getIntent();
Bundle bundle=intent.getExtras();
String str=bundle.getString("str");
secondTxt=(TextView)findViewById(R.id.secondTxt);
secondTxt.setText(str);
}
}
main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
? ? android:layout_width="fill_parent"
? ? android:layout_height="fill_parent"
? ? android:orientation="vertical" >
? ? <TextView
? ? ? ? android:layout_width="fill_parent"
? ? ? ? android:layout_height="wrap_content"
? ? ? ? android:text="@string/hello" />
<Button?
? ?android:id="@+id/but1"
? ?android:layout_width="wrap_content"
? ?android:layout_height="wrap_content"
? ?android:text="Call"
? ?/>
? ??
</LinearLayout>
secondmain.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
? ? android:layout_width="fill_parent"
? ? android:layout_height="fill_parent"
? ? android:orientation="vertical" >
? ? <TextView
? ? ? ? android:id="@+id/secondTxt"
? ? ? ? android:layout_width="fill_parent"
? ? ? ? android:layout_height="wrap_content"
? ? ? ? android:text="second" />
<Button?
? ?android:id="@+id/secondbut1"
? ?android:layout_width="wrap_content"
? ?android:layout_height="wrap_content"
? ?android:text="SecondText"
? ?/>
? ??
</LinearLayout>
AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
? ? package="com.sun.hello"
? ? android:versionCode="1"
? ? android:versionName="1.0" >
? ? <uses-sdk android:minSdkVersion="15" />
? ? <application
? ? ? ? android:icon="@drawable/ic_launcher"
? ? ? ? android:label="@string/app_name" >
? ? ? ? <activity
? ? ? ? ? ? android:name=".HelloActivity"
? ? ? ? ? ? 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=".SecondActivity"
? ? ? ? ? ? android:label="@string/app_name" >
? ? ? ? ? ? <intent-filter>
? ? ? ? ? ? ? ? <action android:name="android.intent.action.MAIN" />
? ? ? ? ? ? ? ? <category android:name="android.intent.category.LAUNCHER" />
? ? ? ? ? ? </intent-filter>
? ? ? ? </activity>
? ? </application>
<uses-permission android:name="android.permission.CALL_PHONE" />
<uses-permission android:name="android.permission.SEND_SMS"/>
</manifest>