现在的位置: 首页 > 综合 > 正文

多个activities应用编程

2019年07月24日 ⁄ 综合 ⁄ 共 5353字 ⁄ 字号 评论关闭

基于篇幅,这里我只用两个独立却又关联的activities来作演示:

main activitiy:

 

package mth.android;

import android.app.Activity;
import android.content.Intent;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

public class AndroidActivity extends Activity implements View.OnClickListener {
    // Define a media player
 private MediaPlayer mediaPlayer = null;
 private int curPos;
 
 /** Called when the activity is first created.
  *  We can treat it like a constructor. Truly it's not the constructor,
  *  it's called when this activity is created!
  *  Constructor is only used when an instance is created!
  * */ 
 @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
       
        // Initialize local variables
        curPos = 0;
        mediaPlayer = new MediaPlayer();
       
        /**
         * Initialize View components
         * */
        Button startActivityButton = (Button)this.findViewById(R.id.startAndroidActivity);
        startActivityButton.setOnClickListener(this);
       
        Button stopActivityButton = (Button)this.findViewById(R.id.stopAndroidActivity);
        stopActivityButton.setOnClickListener(this);
       
        Button suspendActivityButton = (Button)this.findViewById(R.id.suspendAndroidActivity);
        suspendActivityButton.setOnClickListener(this);
       
        Button nextActivityButton = (Button)this.findViewById(R.id.nextPlay);
        nextActivityButton.setOnClickListener(this);
       
        Button aboutAndroidButton = (Button)this.findViewById(R.id.aboutAndroidButton);
        aboutAndroidButton.setOnClickListener(this);

    }
 
 public void replay(MediaPlayer mp) {
  // TODO Auto-generated method stub
  mp.stop();
  mp.reset();
  mp = MediaPlayer.create(this, R.raw.apologize);
  mp.seekTo(0);
  mp.start();
 }
 
 public void display(TextView tv, String str)
    {
     tv.setText("Hello world");
     tv.setBackgroundColor(0xFFFFFF00);
    }
   
    public void onClick(View v) {
  switch (v.getId())
  {
  case R.id.startAndroidActivity:
   // TODO Auto-generated method stub
   final TextView textDisplayer = (TextView) this.findViewById(R.id.displayText);
   textDisplayer.setText("Hello world");
   textDisplayer.setBackgroundColor(BIND_AUTO_CREATE);
   // Start media player
   mediaPlayer = MediaPlayer.create(this, R.raw.just_one_last_dance);
         mediaPlayer.seekTo(0);
         mediaPlayer.start();
   break;
  case R.id.stopAndroidActivity:
   mediaPlayer.stop();
   break;
  case R.id.suspendAndroidActivity:
   if(true == mediaPlayer.isPlaying())
   {
    curPos = mediaPlayer.getCurrentPosition();
    mediaPlayer.pause();
   }
   else
   {
    mediaPlayer.seekTo(curPos);
    mediaPlayer.start();
   }
   break;
  case R.id.nextPlay:
   mediaPlayer.stop();
   // uninitialize media player
   mediaPlayer.reset();
   // Reinitialize media player
   mediaPlayer = MediaPlayer.create(this, R.raw.apologize);
   mediaPlayer.seekTo(0);
   mediaPlayer.start();
   break;
   
  case R.id.aboutAndroidButton:
   /*
    * Display new manifest about
    * */
   Intent intent = new Intent();
         intent.setAction("mth.android.ABOUT_ANDROID");
         startActivity(intent);
   break;
  default:
   break;
  }
 }
}

 

About activity:

 

package mth.android;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

public class AboutActivity extends Activity {
 
 @Override
 public void onCreate(Bundle savedInstanceState)
 {
  super.onCreate(savedInstanceState);
  setContentView(R.layout.about_android);
  
  /*
   * Initialize the view components
   * */
  TextView aboutTextView = (TextView)this.findViewById(R.id.aboutAndroidTextView);
  aboutTextView.bringToFront();
  
  Button exitAboutMenuButton = (Button)this.findViewById(R.id.exitAboutMenu);
  exitAboutMenuButton.setOnClickListener(new Button.OnClickListener(){

   public void onClick(View arg0) {
    // TODO Auto-generated method stub
    finishActivity(0);
   }
   
  });
 }
}

接下来要创建一个xml,专门用于About Activity

about_android.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">
<TextView android:layout_height="wrap_content"
    android:layout_width="fill_parent"
    android:id="@+id/aboutAndroidTextView"
    android:text="Welcome to use Android SDK :-)">
</TextView>
<Button   android:text="Exit About"
    android:id="@+id/exitAboutMenu"
    android:layout_height="wrap_content"
    android:layout_width="fill_parent">
</Button>

</LinearLayout>

接下来要在android manifest里面注册。manifest用于管理所有的activities等资源,并合理标识。

AndroidManifest.xml:

 

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
      package="mth.android"
      android:versionCode="1"
      android:versionName="1.0">
    <application android:icon="@drawable/icon" android:label="@string/app_name">
        <activity android:name=".AndroidActivity"
                  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:name=".AboutActivity"
            android:label="@string/app_name">
         <intent-filter>
          <action android:name="mth.android.ABOUT_ANDROID" />
          <category android:name="android.intent.category.DEFAULT" />
         </intent-filter>   
        </activity>

    </application>

</manifest>

 

程序可运行:-)

抱歉!评论已关闭.