一、按照向导创建一个工程,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>2)android:layout_height="wrap_content" :设置为wrap_content将完整显示其内部的文本和图像。布局元素将根据内容更改大小。设置一个视图的尺寸为wrap_content大体等同于设置Windows控件的Autosize属性为True。
3)android:text :设置按钮的显示值
二、在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方法启动
<?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布局
<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>
原文地址:http://blog.csdn.net/loveheronly/article/details/44223437