当前位置:操作系统 > 安卓/Android >>

Android 学习笔记(三):Activity 之间跳转

新建一个Android项目,代码如下:

main.xml


[html] <?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
    android:orientation="vertical">" 
    <TextView android:id="@+id/textView" 
        android:text="点击按钮to Activity2" 
        android:layout_width="wrap_content"  
        android:layout_height="wrap_content"/> 
    <Button android:id="@+id/button" 
        android:layout_width="fill_parent"  
        android:layout_height="wrap_content" /> 
 
</LinearLayout> 
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical">"
    <TextView android:id="@+id/textView"
        android:text="点击按钮to Activity2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"/>
    <Button android:id="@+id/button"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content" />

</LinearLayout>HelloWorldActivity.java


[java] public class HelloWorldActivity extends Activity implements OnClickListener{ 
    private Button button; 
    /*继承Activity,必须实现onCreate方法 
     * 每一个Activity都要在AndroidManifest.xml中注册,
     * */ 
    @Override 
    public void onCreate(Bundle savedInstanceState) { 
        super.onCreate(savedInstanceState); 
        setContentView(R.layout.main); 
        button=(Button)findViewById(R.id.button); 
        button.setText("My Button"); 
        button.setOnClickListener(this); 
    } 
 
    public void onClick(View v) { 
        Intent intent=new Intent(); 
        intent.setClass(HelloWorldActivity.this, Activity2.class); 
        //启动Activity2  
        HelloWorldActivity.this.startActivity(intent); 
    } 

public class HelloWorldActivity extends Activity implements OnClickListener{
 private Button button;
    /*继承Activity,必须实现onCreate方法
     * 每一个Activity都要在AndroidManifest.xml中注册,
     * */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        button=(Button)findViewById(R.id.button);
        button.setText("My Button");
        button.setOnClickListener(this);
    }

 public void onClick(View v) {
  Intent intent=new Intent();
  intent.setClass(HelloWorldActivity.this, Activity2.class);
  //启动Activity2
  HelloWorldActivity.this.startActivity(intent);
 }
}
新建一个类Activity2:

Activity2.xml


[html] <?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/textView" 
        android:layout_width="wrap_content"  
        android:layout_height="wrap_content" 
        android:text="activy2"/> 
 
</LinearLayout> 
<?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/textView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="activy2"/>

</LinearLayout>Activity2.java


[java] public class Activity2 extends Activity{ 
    @Override 
    public void onCreate(Bundle savedInstanceState) { 
        super.onCreate(savedInstanceState); 
        setContentView(R.layout.activity2); 
    } 

public class Activity2 extends Activity{
    @Override
    public void onCreate(Bundle savedInstanceState) {
     super.onCreate(savedInstanceState);
     setContentView(R.layout.activity2);
    }
}
在AndroidManifest.xml中注册Acivity2:


[html] <activity android:name=".Activity2" 
            android:label="activity2" /> 
<activity android:name=".Activity2"
            android:label="activity2" />
运行效果如下,按返回又会回到HelloWorldActivity,  


 \


摘自  柒尐狐的专栏 
补充:移动开发 , Android ,
CopyRight © 2022 站长资源库 编程知识问答 zzzyk.com All Rights Reserved
部分文章来自网络,