android意图传參数(四)

一、依照向导创建一个project,layout的activity_main.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="match_parent"
        android:layout_height="wrap_content"
        android:text="意向传參数測试"
      />

</RelativeLayout>

1)android:layout_width="match_parent" :谷歌把fill_parent改成了与实际效果更符合的match_parent,表示塞满容器,塞的意思就是有多少空间,占用多少空间

2)android:layout_height="wrap_content" :设置为wrap_content将完整显示其内部的文本和图像。布局元素将依据内容更改大小。

设置一个视图的尺寸为wrap_content大体等同于设置Windows控件的Autosize属性为True。

3)android:text :设置button的显示值


二、在Main函数做意向传參跳转

	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		button = (Button)this.findViewById(R.id.button);
		button.setOnClickListener(new View.OnClickListener() {
			
			@Override
			public void onClick(View arg0) {
				// TODO Auto-generated method stub
			   <span style="color:#ff0000;">  Intent intent = new Intent(MainActivity.this,OtherActivity.class);
			     intent.putExtra("name", "Deng");
			     intent.putExtra("age", 23);
			     startActivity(intent);</span>
			}
		});
	}


通过startActivity方法启动


三、新建一个other.xml 和 OtherActivity.class

<?

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/msg" android:layout_width="match_parent" android:layout_height="wrap_content" /> </LinearLayout>


public class OtherActivity extends Activity {

	private TextView textview;
	public OtherActivity(){
		
	}
	protected void onCreate(Bundle savedInstanceState){
		super.onCreate(savedInstanceState);
		setContentView(R.layout.other);
		textview = (TextView)this.findViewById(R.id.msg);
		<span style="color:#ff0000;">Intent intent = getIntent();</span>
		<span style="color:#ff0000;">String name = intent.getStringExtra("name");
		int age = intent.getIntExtra("age", 0);</span>
		textview.setText("name"+name+";"+"age"+age);
	}
}

记得重写onCreate方法以及设置setContentView布局


四、最后在文件AndroidManifest.xml加入一个activity标签

 <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name="com.example.android_intent.MainActivity"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    <span style="color:#ff0000;">   <activity
           android:name = ".OtherActivity" 
           /></span>
    </application>

        


原文地址:https://www.cnblogs.com/brucemengbm/p/6855542.html