3、Android中Activity的跳轉

來源:互聯網
上載者:User

標籤:src   this   拖拽   java   rac   save   size   code   and   

1、建立project        file->new->android application 依次填入應用程式名稱、project名、包名在project檔案夾下找到src/com.example.helloworld MainActivity.java2、加入代碼
package com.example.helloworld;import android.app.Activity;import android.content.Intent;import android.os.Bundle;import android.view.View;import android.view.View.OnClickListener;import android.widget.Button;public class MainActivity extends Activity {    private Button button;@Overrideprotected void onCreate(Bundle savedInstanceState){super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);   this.button = (Button) this.findViewById(R.id.button1);        this.button.setOnClickListener(new OnClickListener() {            @Override            public void onClick(View v) {                Intent intent = new Intent();                intent.setClass(MainActivity.this, IntentActivity.class);                intent.putExtra("name", "Hello World");                startActivity(intent);            }        });}}

在加入代碼之前,先在res/layout/activity_main.xml中,拖拽一個Button控制項上去。此時會自己主動在activity_main.xml中產生Button的資訊。注意id值,假設使用此Button,實用3、加入跳轉Activity1、右擊src/com.example.helloworld,new->class。在name處填寫IntentActivity,不用加java就可以2、加入layout。在res/layout處右擊。new->android XML file,在此xml上拖拽一個TextView上去。此時在R.java中自己主動加入了TextView的地址3、在AndroidManifest.xml中加入例如以下代碼:完畢Activity的注冊
<activity android:name="com.example.helloworld.IntentActivity"> </activity>
4、實現IntentActivity.java
package com.example.helloworld;import android.app.Activity;import android.os.Bundle;import android.widget.TextView;import android.content.Intent;public class IntentActivity extends Activity{@Overrideprotected void onCreate(Bundle savedInstanceState){super.onCreate(savedInstanceState);setContentView(R.layout.activity_intent);Intent intent = getIntent();String string = intent.getStringExtra("name");TextView textView = new TextView(this);textView.setTextSize(40);textView.setText(string);setContentView(textView);}}

4、執行        開啟模擬器或者手機,執行。

3、Android中Activity的跳轉

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.