zoukankan      html  css  js  c++  java
  • Activity 利用Intent 转换到另一个Activity

    在Android 中,

    要从一个Activity 中呼叫另一个Activity,

    就必须使用 Intent 物件,

    请参考以下范例(这里就不列出Layout XML 档案内容了) :

    来源Activity 程式码

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    
    public class helloWorld extends Activity {
       public void onCreate(Bundle savedInstanceState) {
          super.onCreate(savedInstanceState);
     
          // 设定Layout 为main.xml
          setContentView( R.layout.main );
     
          // 按钮物件
          Button b1 = (Button)findViewById( R.id.button1 );
     
          // Button 的OnClick Trigger
          b1.setOnClickListener( new OnClickListener(){
             public void onClick(View v) {
     
                // 指定要呼叫的Activity Class
                Intent newAct = new Intent() ;
                newAct. setClass ( helloWorld .this , helloWorld2 .class );
     
                // 呼叫新的Activity Class
                startActivity ( newAct );
     
                // 结束原先的Activity Class
                helloWorld .this.finish() ;
            }
          });  
       }
    }

    目的Activity 程式码

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    
     public void onCreate(Bundle savedInstanceState) {
          super.onCreate(savedInstanceState);
     
          // 设定Layout 为main2.xml
          setContentView( R.layout.main2 );
     
          // 按钮物件
          Button b1 = (Button)findViewById( R.id.button1 );
     
          // Button 的OnClick Trigger
          b1.setOnClickListener( new OnClickListener(){
             public void onClick(View v) {
     
                // 指定要呼叫的Activity Class
                Intent newAct = new Intent() ;
                newAct. setClass ( helloWorld2 .this , helloWorld .class );
     
                // 呼叫新的Activity Class
                startActivity ( newAct );
     
                // 结束原先的Activity Class
                helloWorld2 .this.finish() ;
             }
          });
       }
    }

    AndroidManifest.xml 要加入第二个Activity,不然呼叫时会有问题:



    奋斗改变命运,梦想让我与众不同,追求技术的巅峰
  • 相关阅读:
    left join 和 inner join 区别和优化
    认识位移操作符
    動態修改 XML 欄位
    (轉載)sql server xml字段的操作
    (轉)CSS 单行溢出文本显示省略号...的方法(兼容IE FF)
    (轉)Equal height boxes with CSS
    獲得瀏覽器顯示標簽的真實的長寬高
    轉:Jquery绑定img的click事件
    SqlLocalDB 的一些常用命令行
    转:css实现强制不换行/自动换行/强制换行
  • 原文地址:https://www.cnblogs.com/ligo/p/2383854.html
Copyright © 2011-2022 走看看