Activity:

        1:创建一个类继承Activity或者它的子类

                public class MainActivity extends Activity {


@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);

Button button=(Button)findViewById(R.id.button1);//获取按钮
button.setOnClickListener(new OnClickListener(){//进行监听


@Override
public void onClick(View v) {
// TODO Auto-generated method stub
Intent intent=new Intent(MainActivity.this,DetailActivity.class);//创建Intent对象
startActivity(intent);//启动Activity
}

});
}
2:设置布局文件

                <?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context=".DetailActivity" >


    <TextView
        android:id="@+id/textView2"
        android:layout_width="129dp"
        android:layout_height="53dp"
        android:text="@string/detail" />


    <Button
        android:id="@+id/button2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/bu" />


</LinearLayout>

      3:重写继承类的方法

                   protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.detail);//设置布局文件

Button button=(Button)findViewById(R.id.button2);//获取关闭按钮
button.setOnClickListener(new OnClickListener(){


@Override
public void onClick(View v) {
// TODO Auto-generated method stub
finish();//关闭当前的Activity
}


});
}


          4:在主活动中创建Intent对象并开启Activity

       setContentView(R.layout.main);

Button button=(Button)findViewById(R.id.button1);//获取按钮
button.setOnClickListener(new OnClickListener(){//进行监听


@Override
public void onClick(View v) {
// TODO Auto-generated method stub
Intent intent=new Intent(MainActivity.this,DetailActivity.class);//创建Intent对象
startActivity(intent);//启动Activity
}

});


5:在AndroidManifest.xml文件中配置你所写类的信息

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


    <uses-sdk
        android:minSdkVersion="8"
        android:targetSdkVersion="17" />


    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name="com.org.aui.MainActivity"
            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:icon="@drawable/p1"
            android:name=".DetailActivity"
            android:label="@string/lab"
            >
            
        </activity>
    </application>


</manifest>


 

Activity的创建和使用_ico