初识Activity

来源:互联网 发布:mmd口型数据 编辑:程序博客网 时间:2024/04/27 02:13

根据文档的解释,ActivityAndroid开发中非常重要的一个基础类。我把它想像成J2ME中的Display类,或者是Win32平台上的Form类,也许不准确,但是它的重要性我觉得应该是一样的(当然,如果我们写的是一个没有界面的应用,例如后台运行的服务之类的,可以不用Display的)。

1.     在一个Activity中使用多个View

如果把Activity看作MVC中的Control?它负责管理UI和接受事件(包括用户的输入),虽然说一个Activity通常对应一个屏幕,但事实上,我们是可以只用一个Activity管理多个不同的View来实现简单的逻辑。
首先,我们增加一个新的资源描述layout/second.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation
="vertical"
    android:layout_width
="fill_parent"
    android:layout_height
="fill_parent"
    
>
<TextView id="@+id/txt"  
    android:layout_width
="fill_parent"
 
    android:layout_height
="wrap_content"
 
    android:text
="Hello 中国"

    
/>
 
<Button id="@+id/go2"
        android:layout_width
="wrap_content" android:layout_height="wrap_content" 
        android:text
="back">

        
<requestFocus />
    
</Button>   
</LinearLayout>

除了一个“Hello中国以外,增加一个按钮可以返回前一个界面。然后,在代码中我们要为helloTwo增加两个方法,setViewOneCommandsetViewTwoCommand,分别处理一下在不同界面时,从资源里加载组件并为组件绑定一个事件处理器。

  public void setViewOneCommand()
    ...{
        Button btn = (Button)findViewById(R.id.go);
        btn.setOnClickListener(
new
 View.OnClickListener()
        ...{
            
public void
 onClick(View v)
            ...{
                helloTwo.
this
.setContentView(R.layout.second);
                helloTwo.
this
.setViewTwoCommand();                
            }
        });       
        Button btnExit=(Button)findViewById(R.id.exit);
        btnExit.setOnClickListener(
new
 View.OnClickListener()...{
            
public void
 onClick(View v)...{
                helloTwo.
this
.finish();
            }
        });    
    }
    
public void
 setViewTwoCommand()
    ...{
        Button btnBack=(Button)findViewById(R.id.go2);
        btnBack.setOnClickListener(
new
 View.OnClickListener()...{
            
public void
 onClick(View v)...{
                helloTwo.
this
.setContentView(R.layout.main);
                helloTwo.
this
.setViewOneCommand();
            }                          
        });
    }

最后,我们需要在onCreate的时候,也就是启动后的main界面上设置一下按钮事件处理器。新的onCreate方法如下:

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

编译,运行,OK

2.     还是回到正道上,多个Activity之间的跳转

Android中提供一个叫Intent的类来实现屏幕之间的跳转,按文档的说法,似乎他们也建议采用这种方法,Intent的用法比较复杂,现在我先看看它最简单的用法。

先在应用中增加两个Activity,这需要修改AndroidManifest.xml文件了,如下:

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

很简单,就是加一个标签而已,新标签的class.HelloThreeB显示的应用标题与前一个Activity一样而已,然后第二步就是修改一个HelloThree类的实现,在onCreate方法中绑定按钮的事件处理器:

    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();
            }
        });    
    } 

这里的跳转功能Intent来操作,它的最简单用法就是用函数setClass()设置跳转前后两个Activity类的实例,然后调用Activity自己的startActivity(intent)即可。最后一句finish()表示将当前Activity关掉(如果不关掉会如何?你可以自己试一下看效果,事实上有时我们是不需要关掉当前Activity的)。

然后,我们同样弄一个ActivityHelloThreeB,代码与前面的差不多,只是将setClass的两个参数反一下,这样就可以简单地实现在两个Activity界面中来回切换的功能了。

3.     如果我想在两个Activity之间进行数据交换,怎么办?

前例中的startActivity()只有一个参数,如果需要向新打开的Activity传递参数,我们得换一个函数了, Android提供了startSubActivity(Intent,int)这个函数来实现这个功能。

函数原型为: public void startSubActivity(Intent intent, int requestCode)

这里的requestCode用来标识某一个调用,一般由我们定义一个常量。

如何把参数传过去呢?Intent类在提供setClass()函数的同时也提供了一个setData()函数。

函数原型为public Intent setData(ContentURI data)

参数类型是ContentURI,它的详细内容下回再分析,现在就把它当成一个String类型来用吧。

参数带到新的Activity后,同样用Activity.getIntent()函数可以得到当前过来的Intent对象,然后用getData()就取到参数了。

把参数带回来的方法是Activity.setResult(),它有几个形式,现在先看最简单的一个吧。

函数原型是public final void setResult(int resultCode, String data)

resultCode是返回代码,同样用来标识一个返回类型,而data则是它要返回的参数。

在原来的Activity中的事件处理回调函数onActivityResult,会被系统调用,从它的参数里可以得到返回值。

函数原型为protected void onActivityResult(int requestCode, int resultCode,String data, Bundle extras)

这里的requestCode就是前面启动新Activity时的带过去的requestCode,而resultCode则关联上了setResult中的resultCodedata是参数,extras也是一个很重要的东西,后面再研究一下它的作用。

下面,我们来看一下代码吧,先看看HelloThree中的代码:

    public void setViewOneCommand()
    ...{
        Button btn = (Button)findViewById(R.id.go);
        btn.setOnClickListener(
new
 View.OnClickListener()
        ...{
            
public void
 onClick(View v)
            ...{
                
try

                ...{                    
                    Intent intent = 
new Intent();
                    intent.setClass(HelloThree.
this, HelloThreeB.class
);
                    
intent.setData(new ContentURI("One"));
                   
                    
startSubActivity(intent,REQUEST_TYPE_A);

                }
                
catch
(Exception ex)...{}
            }
        });       
        Button btnExit=(Button)findViewById(R.id.exit);
        btnExit.setOnClickListener(
new
 View.OnClickListener()...{
            
public void
 onClick(View v)...{
                HelloThree.
this
.finish();
            }
        });    
    } 
    
protected void onActivityResult(int requestCode, int
 resultCode,
            String data, Bundle extras)
    ...{
        
if
 (requestCode == REQUEST_TYPE_A) ...{
            
if
 (resultCode == RESULT_OK) ...{
                Log.v(TAG,data);
                TextView txt = (TextView)findViewById(R.id.txt);
                txt.setText(data);                
            }
        }
    }

这里的REQUEST_TYPE_A是我们定义的一个常量。在onActivityResult中用它与RESULT_OK一起作为条件判断如何处理返回值,这里只是简单将TextView显示值换成传来的字串。

再来看看另一个HelloThreeB类的实现代码:

    private Intent i;
    
protected void
 onCreate(Bundle icicle) ...{
        super
.onCreate(icicle);
        setContentView(R.layout.second);    
        
        i = getIntent();
        
        android.util.Log.v(TAG,"onCreate");
        Button btn = (Button)findViewById(R.id.go);
        btn.setOnClickListener(
new
 View.OnClickListener()...{
            
public void onClick(View v)...{

                String result=HelloThreeB.this.i.getData().toString()+" And Two";
                
HelloThreeB.this.setResult(RESULT_OK,result);
                finish();
            }            
        });
        
        TextView v = (TextView)findViewById(R.id.txt);
        v.setText("Param is "+i.getData().toString());
        
    }

在按钮处理事件中,从Intent取出参数,处理一下再用setResult返回给前一个Activity即可。

编译运行即可。

来源:http://www.sf.org.cn/Android/lumen/20977.html