android意圖傳參數(四)

來源:互聯網
上載者:User

標籤: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意圖傳參數(四)

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.