Chinaunix首页 | 论坛 | 博客
  • 博客访问: 9152046
  • 博文数量: 1727
  • 博客积分: 12961
  • 博客等级: 上将
  • 技术积分: 19860
  • 用 户 组: 普通用户
  • 注册时间: 2009-01-09 11:25
个人简介

偷得浮生半桶水(半日闲), 好记性不如抄下来(烂笔头). 信息爆炸的时代, 学习是一项持续的工作.

文章分类

全部博文(1727)

文章存档

2024年(3)

2023年(26)

2022年(112)

2021年(217)

2020年(157)

2019年(192)

2018年(81)

2017年(78)

2016年(70)

2015年(52)

2014年(40)

2013年(51)

2012年(85)

2011年(45)

2010年(231)

2009年(287)

分类:

2009-04-30 11:20:15

根据文档的解释,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=""
    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=""
    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即可。

编译运行即可。

来源:
阅读(1265) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~