Android中Activity的跳轉

來源:互聯網
上載者:User

Android中Activity的跳轉
目錄(?)[+]

  1. 建立工程添加代碼 添加跳轉Activity運行1、建立工程file->new->android application 依次填入應用程式名稱、工程名、包名 在工程目錄下找到src/com.example.helloworld MainActivity.java2、添加代碼[java] 
    1. package com.example.helloworld;
    2. import android.app.Activity; import android.content.Intent;
    3. import android.os.Bundle; import android.view.View;
    4. import android.view.View.OnClickListener; import android.widget.Button;
    5.  
    6. public class MainActivity extends Activity {
    7. private Button button; @Override
    8. protected void onCreate(Bundle savedInstanceState) {
    9. super.onCreate(savedInstanceState); setContentView(R.layout.activity_main);
    10. this.button = (Button) this.findViewById(R.id.button1);
    11. this.button.setOnClickListener(new OnClickListener() { @Override
    12. public void onClick(View v) { Intent intent = new Intent();
    13. intent.setClass(MainActivity.this, IntentActivity.class); intent.putExtra(name, Hello World);
    14. startActivity(intent); }
    15. }); }
    16. }
      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的註冊 [java] 
      1.  
        4、實現IntentActivity.java [java] view plaincopyprint?
        1. package com.example.helloworld;
        2. import android.app.Activity; import android.os.Bundle;
        3. import android.widget.TextView; import android.content.Intent;
        4. public class IntentActivity extends Activity {
        5. @Override protected void onCreate(Bundle savedInstanceState)
        6. { super.onCreate(savedInstanceState);
        7. setContentView(R.layout.activity_intent); Intent intent = getIntent();
        8. String string = intent.getStringExtra(name);
        9. TextView textView = new TextView(this); textView.setTextSize(40);
        10. textView.setText(string);
        11. setContentView(textView); }
        12. }
          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、運行開啟模擬器或者手機,運行。

聯繫我們

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