Android之Activity 2

来源:互联网 发布:城市人口密度数据 编辑:程序博客网 时间:2024/04/30 05:13

android提倡的标准方法是使用intent来实现两个screen之间的跳转,说到screen和activity的区别,我也不是很清楚,目前我的理解就是screen = activity。因为要使用两个activity,所以首先要在AndroidManifest.xml声明有两个activity,如下:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package
="com.Android.HelloThree">
    
<application android:icon="@drawable/icon">
        
        
<activity android:name=".HelloThree" android:label="@string/app_name">
            
<intent-filter>
                
<action android:name="android.intent.action.MAIN" />
                
<category android:name="android.intent.category.LAUNCHER" />
            
</intent-filter>
        
</activity>
        
        
<activity android:name=".HelloThreeB" android:label="@string/app_name">
        
</activity>
        
    
</application>
</manifest> 

看到网上的同志们的xml是这样写的

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package
="com.Android.HelloThree">
    
<application android:icon="@drawable/icon">
        
        
<activity class=".HelloThree" android:label="@string/app_name">
            
<intent-filter>
                
<action android:name="android.intent.action.MAIN" />
                
<category android:name="android.intent.category.LAUNCHER" />
            
</intent-filter>
        
</activity>
        
        
<activity class=".HelloThreeB" android:label="@string/app_name">
        
</activity>
        
    
</application>
</manifest> 

与我的不相同是activity以class命名,我尝试了一下,但是总是报错,也不知道是什么原因......

之后的类定义如下:

public void onCreate(Bundle icicle){
        
super.onCreate(icicle);
        setTheme(android.R.style.Theme_Dark);
        setContentView(R.layout.main);
        setViewOneCommand();
    }

    
public void setViewOneCommand()
    
{
        Button btn 
= (Button)findViewById(R.id.go);
        btn.setOnClickListener(
new View.OnClickListener()
        
{
            
public void onClick(View v)
            
{
                Intent intent 
= new Intent();
                intent.setClass(HelloThree.
this, HelloThreeB.class);
                startActivity(intent);
                finish();            
            }

        }
);       
        Button btnExit
=(Button)findViewById(R.id.exit);
        btnExit.setOnClickListener(
new View.OnClickListener(){
            
public void onClick(View v){
                HelloThree.
this.finish();
            }

        }
);    
    }

可以看到在hellothree这个activity中有两个按钮,按go,它就会启动第二个activity,按exit,它就会退出。相应的,我们必须定义hellothreeb这个activity。如下:

public void onCreate(Bundle icicle){
        
super.onCreate(icicle);
        setTheme(android.R.style.Theme_Dark);
        setContentView(R.layout.second);
        setviewTwocommand();
    }
;
    
public void setviewTwocommand(){
        Button btn2 
= (Button)findViewById(R.id.go2);
        btn2.setOnClickListener(
new View.OnClickListener()
        
{
            
public void onClick(View v)
            
{
                Intent intent 
= new Intent();
                intent.setClass(HelloThreeB.
this, HelloThree.class);
                startActivity(intent);
                finish();            
            }

        }
);        
    }
;

我在这个activity中定义了一个按钮来实现回到前一个screen的功能,由此实现两个screen之间的跳转。但是此处仍然不能实现数据的传递。