Android在Activity之间传数据之Intent


Android在Activity之间传数据之Intent

FirstActivity.java:

package com.android.activity;


import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;


public class FirstActivity extends Activity {
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        Button bt = (Button) findViewById(R.id.bt);
        bt.setOnClickListener(new MyBt_OnClickListener());
    }
    class MyBt_OnClickListener implements OnClickListener{


@Override
public void onClick(View v) {
// TODO Auto-generated method stub
Intent intent = new Intent();
intent.putExtra("intentKey", "intentValue");
intent.setClass(FirstActivity.this, SecondActivity.class);
FirstActivity.this.startActivity(intent);
}
   
    }
}


对应的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:text="@string/first"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:id="@+id/bt"/>

</LinearLayout>

 

 

---SecondActivity.java\

package com.android.activity;


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


public class SecondActivity extends Activity{
@Override
public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.second);
        Intent intent = getIntent();
        TextView tv = (TextView) findViewById(R.id.second_tv);
        Log.e("intentKey", intent.getStringExtra("intentKey"));
        tv.setText(intent.getStringExtra("intentKey"));
}


}


对应的xml:second.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/second"
        android:id="@+id/second_tv"/>

</LinearLayout>

  • 1
  • 2
  • 下一页

相关内容