IT虾米网

Android之Intent

itxm 2022年11月07日 手机开发 90 0

在android平台中,Activity组件之间的交互通过意向(Intent)来实现。

Intent是android的组件之一(激活组件,用于激活其他组件),Intent对象在android系统中代表一种意图,Intent中最重要的内容是action与data

使用Intent对象传递数据的步骤:

1)在Activity之间可以使用Intent对象传递数据

2)使用putExtra()系列方法向Intent对象当中储存数据

3)使用getXXXExtra()系列方法从Intent对象当中取出数据

下面是代码示例:

主Activity:

package com.tangbc.s02e04_intent; 
 
import android.app.Activity; 
import android.content.Intent; 
import android.os.Bundle; 
import android.view.Menu; 
import android.view.View; 
import android.view.View.OnClickListener; 
import android.widget.Button; 
 
public class MainActivity extends Activity { 
	private Button button; 
 
	@Override 
	protected void onCreate(Bundle savedInstanceState) { 
		super.onCreate(savedInstanceState); 
		setContentView(R.layout.activity_main); 
		 
		button = (Button)findViewById(R.id.button); 
		button.setOnClickListener(new ButtonListener()); 
	} 
	 
	class ButtonListener implements OnClickListener{ 
		@Override 
		public void onClick(View arg0) { 
			//生成一个Intent对象 
			Intent intent = new Intent(); 
			//调用Intent对象的setClass()方法 
			//setClass(Context packageContext, Class<?> cls) 
			intent.setClass(MainActivity.this, Other.class); 
			//在该Intent对象中存入数据 
			intent.putExtra("com.tangbc.s02e04_intent.Age", 20); 
			intent.putExtra("com.tangbc.s02e04_intent.Name","tang"); 
			//启动第二个Activity 
			startActivity(intent); 
		} 
	} 
} 

第二个Activity:

package com.tangbc.s02e04_intent; 
 
import android.app.Activity; 
import android.content.Intent; 
import android.os.Bundle; 
import android.widget.TextView; 
 
public class Other extends Activity{ 
	private TextView textView; 
 
	@Override 
	protected void onCreate(Bundle savedInstanceState) { 
		super.onCreate(savedInstanceState); 
		setContentView(R.layout.other); 
		//将返回启动这个Activity的Intent对象赋给intent 
		Intent intent = getIntent(); 
		//将intent中的数据取出并赋值 
		//括号中第二个数是默认值,若没有对应的值,就取默认值 
		int age = intent.getIntExtra("com.tangbc.s02e04_intent.Age", 10); 
		int age1 = intent.getIntExtra("com.tangbc.s02e04_intent.Age1", 10); 
		String name = intent.getStringExtra("com.tangbc.s02e04_intent.Name"); 
		 
		textView = (TextView)findViewById(R.id.textView); 
		textView.setText("name= " + name + ",age= " + age + ",age1=" + age1); 
	} 
} 

主Activity的XML代码:

<RelativeLayout 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=".MainActivity" > 
 
    <Button  
        android:id="@+id/button" 
        android:layout_width="wrap_content" 
        android:layout_height="wrap_content" 
        android:text="touch me" 
        /> 
 
</RelativeLayout> 

副Activity的XML代码:

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:orientation="vertical" > 
     
    <TextView  
        android:id="@+id/textView" 
        android:layout_width="wrap_content" 
        android:layout_height="wrap_content" /> 
     
</LinearLayout> 

最后的结果图:





本文参考链接:https://blog.csdn.net/tt75281920/article/details/25830663
评论关闭
IT虾米网

微信公众号号:IT虾米 (左侧二维码扫一扫)欢迎添加!

Java多线程中Lock锁如何使用