1、Activity的主要作用Activity是Android一个非常重要的用户/应用程序交互的接口,相当于JPanel(容器),上面可以放置N多控件,如按钮,文本,广告……控件
2、创建一个Activity的方法一、Activity就是一个类,并且这个类要继承Activity二、并重写断onCreate方法三、每一个Activity都需要在AndroidManifest.xml文件当中注册才能使用四、在Activity当中添加控件(按钮,文本,广告,下拉,单/复选按钮……)
示例代码:
Activity代码:
package com.zhd.hw; import android.app.Activity; import android.os.Bundle; import android.view.View; import android.view.View.OnClickListener; import android.widget.Button; import android.widget.TextView; public class HelloWorldActivity extends Activity { /** Called when the activity is first created. */ TextView myText = null; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); Button myButton = (Button)findViewById(R.id.myButton); myButton.setText("点击I"); myText = (TextView)findViewById(R.id.myText); myText.setText("显示文本"); myButton.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { myText.setText("我变"); } }); } }
这里和Swing有点不一样的是,添加事件不是add开头,而是set开头,晕死了…… 布局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 android:id="@+id/myText" android:layout_width="fill_parent" android:layout_height="wrap_content" /> <Button android:id="@+id/myButton" android:layout_width="fill_parent" android:layout_height="wrap_content" /> </LinearLayout> 需要注意的是,这里给每个控件都添加了一个id属性,请看下面的R.java文件的变量
R.java
/* AUTO-GENERATED FILE. DO NOT MODIFY. * * This class was automatically generated by the * aapt tool from the resource data it found. It * should not be modified by hand. */ package com.zhd.hw; public final class R { public static final class attr { } public static final class drawable { public static final int icon=0x7f020000; } public static final class id { public static final int myButton=0x7f050001; public static final int myText=0x7f050000; } public static final class layout { public static final int main=0x7f030000; } public static final class string { public static final int app_name=0x7f040001; public static final int hello=0x7f040000; } }
可以看到多了一个内部类id,里面的静态变量即我们配置的id,然后我们就可以通过id来引用控件了,即上面Button myButton = (Button)findViewById(R.id.myButton); 来获取对象,再来操作对象。 最后看一下全局的Activity的注册的XML配置文件 <?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.zhd.hw" android:versionCode="1" android:versionName="1.0"> <uses-sdk android:minSdkVersion="4" /> <application android:icon="@drawable/icon" android:label="@string/app_name"> <activity android:name=".HelloWorldActivity" 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> 这里没有什么变化,需要注意的是activity的android:name属性的配置,前面多了一个点,此属性和manifest中的package属性的值加起来,正好是类的包名.类名。 OK,一个简单的程序出来了。