標籤:android intent
一、按照嚮導建立一個工程,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" :Google把fill_parent改成了與實際效果更符合的match_parent,表示塞滿容器,塞的意思就是有多少空間,佔用多少空間
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() {@Overridepublic 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>
android意圖傳參數(四)