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

本文详细讲解了如何在Android应用中使用Intent来携带参数,并跳转到指定Activity,同时展示如何从Intent中取出数据并显示在UI界面上,涵盖了Intent的基本用法和数据类型转换。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

<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"));
	}
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值