bestie

导航

 

  总结Activity的常识之前,先来说说什么是Activity。简单的讲,Activity就是一个屏幕,在屏幕上我们可以放置任何被允许放置的东西,图片,文字,android内置的各类控件等;它的标准定义如下(摘录自android文档):
An activity is a single, focused thing that the user can do. Almost all activities interact with the user, so the Activity class takes care of creating a window for you in which you can place your UI with setContentView(View).
  上面这句话说出了两个关键点,一是Activity做什么用:interact with the user,二是Activity怎么使用:place UI with setContentView(View)。Activity就是承载View以提供用户交互界面的这么一个东西。如果你一定要问Activity到底是什么?Activity就是Activity,用某事物来解释它自身最合适不过,而我们更容易了解的是事物呈现出来的某种规律。世间万物都各有命数,休养生息,轮回不止。从一个Activity被创建,到它的消亡一般可以经历以下过程(Activity的生命周期):

                            图引自android-developers

  刚才说过,Activity是一个屏幕,只是相当于一个画框,画中的内容呢,还需要android的各种控件或开发人员自己来绘制。下面写个demo。

public class AndroidWidgetSampleActivity extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.widget_sample_layout);
}
}
widget_sample_layout.xml
<?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:layout_width="fill_parent"
  android:layout_height
="wrap_content"
  android:text
="@string/hello" />

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

<CheckBox
  android:id="@+id/checkBox1"
  android:layout_width
="wrap_content"
  android:layout_height
="wrap_content"
  android:text
="CheckBox" />

<RadioGroup
  android:id="@+id/radioGroup1"
  android:layout_width
="wrap_content"
  android:layout_height
="wrap_content" >

<RadioButton
  android:id="@+id/radio0"
  android:layout_width
="wrap_content"
  android:layout_height
="wrap_content"
  android:checked
="true"
  android:text
="RadioButton" />

<RadioButton
  android:id="@+id/radio1"
  android:layout_width
="wrap_content"
  android:layout_height
="wrap_content"
  android:text
="RadioButton" />
</RadioGroup>

<ProgressBar
  android:id="@+id/progressBar1"
  android:layout_width
="wrap_content"
  android:layout_height
="wrap_content" />

<AnalogClock
  android:id="@+id/AnalogClock01"
  android:layout_width
="74dp"
  android:layout_height
="76dp" />

<ZoomControls
  android:id="@+id/zoomControls1"
  android:layout_width
="wrap_content"
  android:layout_height
="wrap_content" />

</LinearLayout>

  系统内置的控件主要在android.widget.*下,直接调用相应的api就可以完成界面布局,控件设置,事件监听等需要的功能,而View中提供的onDraw方法提供了更多的自由和灵活性,能让开发人员做出创造性的界面设计。
  再来说一说Activity的启动,跳转和返回的过程,之前讲过AndroidManifest.xml文件的作用是方便系统管理各个应用程序,看一下这个程序的AndroidManifest.xml文件:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
  package
="com.demo"
  android:versionCode
="1"
  android:versionName
="1.0" >

<uses-sdk android:minSdkVersion="14" />

<application
  android:icon="@drawable/ic_launcher"
  android:label
="@string/app_name" >
<activity
  android:name=".AndroidWidgetSampleActivity"
  android:label
="@string/app_name" >
  <intent-filter>
    <action android:name="android.intent.action.MAIN" />
    <category android:name="android.intent.category.LAUNCHER" />
  </intent-filter>
</activity>
</application>

</manifest>

<intent-filter>用来响应Intent请求,Intent是android的重要组件之一,主要负责不同组件之间的消息传递,Activity之间的跳转和传参都需要用到它。我们在上面的程序中再添加一些内容:

public class AndroidWidgetSampleActivity extends Activity {
  /** Called when the activity is first created. */
  @Override
  public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    Button bt = (Button)findViewById(R.id.button1);
    bt.setOnClickListener(new OnClickListener(){
      public void onClick(View v) {
        startAnotherActivity();
      }
    });
}
  public void startAnotherActivity(){
    Intent intent = new Intent(this, AnotherActivity.class);
    Bundle bundle = new Bundle();
    bundle.putString("name", "James Blunt");
    intent.putExtras(bundle); // 或者直接用intent.putExtra("key", "value");
    this.startActivity(intent);
}
}

添加AnotherActivity.java类,AndroidManifest.xml注册该类:

public class AnotherActivity extends Activity {
  Bundle bundle;
  TextView textView;
public void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  if((bundle = this.getIntent().getExtras()) != null) {
    textView = new TextView(this);
    textView.setText(bundle.getString("name"));
  }
  setContentView(textView);
  }
}

 

<activity android:name=".AnotherActivity">

  在AndroidWidgetSampleActivity界面中点击Button,跳转到AnotherActivity界面,并用简单的方式传递了一个参数。

   

  AndroidWidgetSampleActivity界面              AnotherActivity界面

  对Intent的正确使用,也是android应用开发中的一个重点。同时,因为android的“task and back stack”机制,每次启动新的Activity,都会把先前的Activity压入Activity stack暂存,等待下次返回时快速复原。如果使用startActivityForResult方法启动AnotherActivity,还可以在AnotherActivity返回时传一定的信息回AndroidWidgetSampleActivity,这就很像通信的半双工。Activity还有一些子类,如PreferenceActivity,ListActivity和TabActivity等,对于特定的实现,是一种很好的选择。

  关于Activity的内容有很多,这里只是做一个大概的分析,各种细节的部分只有在实际的开发和应用中不断的积累和深入,才能掌握和熟练,而熟能生巧,就是技术养成的一般途径。

posted on 2011-12-20 14:24  bestie  阅读(228)  评论(0)    收藏  举报