Android之Intent携带参数跳转到指定的Activity并取出值显示在UI页面

<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.test23.MainActivity" >

   <Button 
       android:layout_width="wrap_content"
       android:layout_height="wrap_content"
       android:text="传值跳转到第二个页面"
       android:onClick="btnOnclick"
       />

</LinearLayout>

<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:orientation="vertical" >

    <TextView
        android:id="@+id/tv_textViewOne" 
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="hello"
        />

</LinearLayout>

package com.example.test23;

import android.support.v7.app.ActionBarActivity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;

public class MainActivity extends ActionBarActivity {
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
	}
	
	public void btnOnclick(View view){
		Intent intent=new Intent();
		
		
		Bundle bundle=new Bundle();
		bundle.putString("hello", "你好!");
		bundle.putInt("test", 10);
		intent.putExtras(bundle);
		//Log.i("TEST", intent.getStringExtra("hello").toString()+"---"+intent.getIntExtra("test", 0));
		//Log.i("TEST", intent.getIntExtra("test", 0));
		intent.setClass(this, SecondActivity.class);
		startActivity(intent);
	}
}

package com.example.test23;

import android.support.v7.app.ActionBarActivity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.TextView;

public class SecondActivity extends ActionBarActivity {
	private TextView textView;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_second);
		//Log.i("TEST", "1");
		textView=(TextView) findViewById(R.id.tv_textViewOne);
		
		Intent intent=getIntent();
		Bundle bundle=intent.getExtras();
		textView.setText(bundle.getString("hello").toString()+"---"+bundle.getInt("test"));
	}
}


猜你喜欢

转载自blog.csdn.net/qq_38922435/article/details/80844886
今日推荐