abdroid实习程序5——切换过程、Ball

四大组件:
Service
(提供数据,访问接口)
intent

context的子类
list
group
tape

第一种Service:
继承Service,并调用OnBound()

1、页面切换的方法顺序
2、Service的生命周期
3、小球点击出现
===================================
1、页面间的切换
.mainActivity.java
package com.tarena.Day04;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;

public class Day05_01Activity extends Activity {

/** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        System.out.print("firstActivity onCreate()");
        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(Day05_01Activity.this , SecActivity.class);
startActivity(intent);
finish();
}    
        });    
    }
    
    @Override
    protected void onDestroy() {
    // TODO Auto-generated method stub
    super.onDestroy();
    System.out.print("firstActivity onDestroy()");
    }

    @Override
    protected void onPause() {
    // TODO Auto-generated method stub
    super.onPause();
    System.out.print("firstActivity onPause()");
    }

    @Override
    protected void onRestart() {
    // TODO Auto-generated method stub
    super.onRestart();
    System.out.print("firstActivity onRestart()");
    }

    @Override
    protected void onResume() {
    // TODO Auto-generated method stub
    super.onResume();
    System.out.print("firstActivity onResume()");
    }

    @Override
    protected void onStart() {
    // TODO Auto-generated method stub
    super.onStart();
    System.out.print("firstActivity onStart()");
    }

    @Override
    protected void onStop() {
    // TODO Auto-generated method stub
    super.onStop();
    System.out.print("firstActivity onStop()");
   
}

.secActivity.java
package com.tarena.Day04;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;

public class SecActivity extends Activity{

@Override
protected void onStart() {
// TODO Auto-generated method stub
super.onStart();
System.out.print("secondActivity onStart()");
}

 @Override
protected void onDestroy() {
// TODO Auto-generated method stub
super.onDestroy();
System.out.print("firstActivity onDestroy()");
}

@Override
protected void onPause() {
// TODO Auto-generated method stub
super.onPause();
System.out.print("secondActivity onPause()");
}

@Override
protected void onRestart() {
// TODO Auto-generated method stub
super.onRestart();
System.out.print("secondActivity onRestart()");
}

@Override
protected void onResume() {
// TODO Auto-generated method stub
super.onResume();
System.out.print("secondActivity onResume()");
}

@Override
protected void onStop() {
// TODO Auto-generated method stub
super.onStop();
System.out.print("secondActivity onStop()");
}
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.main2);
 Button button =(Button) findViewById(R.id.Tothird);
        button.setOnClickListener(new OnClickListener(){

@Override
public void onClick(View v) {
// TODO Auto-generated method stub
Intent   intent  = new  Intent(SecActivity.this , ThirdActivity.class);
/*Intent   intent  = new  Intent();
intent.setClass(Day05_01Activity.this,SecActivity.class);
startActivity(intent);*/
startActivity(intent);
finish();
}                 
        });
}
}

thirdActivity
package com.tarena.Day04;

import android.app.Activity;
import android.os.Bundle;

public class ThirdActivity  extends Activity{

@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.main3);
}
}

main.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="@string/first" />

</LinearLayout>

main2.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/main2Text" />

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

</LinearLayout>

main3.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/third" />

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

</LinearLayout>

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

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

    <application
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name" >
        <activity
            android:name=".Day05_01Activity"
            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=".SecActivity"
            android:label="@string/app_name" 
            android:theme="@android:style/Theme.Dialog"
            >           
        </activity>
        
         <activity
            android:name=".ThirdActivity"
            android:label="@string/app_name" >          
        </activity>
         
    </application>

</manifest>
===================================
2、Service的生命周期
。java
package com.tarena;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;

public class Day05_02Activity extends Activity {
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        
        Button start =(Button) findViewById(R.id.start);
        Button stop =(Button) findViewById(R.id.stop);
        
        final Intent intent =new Intent(this, MyService.class);
        
        start.setOnClickListener(new OnClickListener(){      
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
startService(intent);
}        
        });
        
        stop.setOnClickListener(new OnClickListener(){

@Override
public void onClick(View v) {
// TODO Auto-generated method stub
stopService(intent);
}
        });       
    }
}

Myservice.java
package com.tarena;

import android.app.Service;
import android.content.Intent;
import android.os.IBinder;
/*新建一个Service的步骤
 * 创建一个类继承Service
 * 在清单文件中注册该Service
 * 调用startService()启动service
 * 调用stopService()停止service
 * 
 * 
 * 
 * */
import android.widget.TextView.SavedState;

public class MyService extends Service{

@Override
public void onCreate() {
// TODO Auto-generated method stub
      System.out.println("Oncreate");
super.onCreate();
}

@Override
public IBinder onBind(Intent arg0) {
// TODO Auto-generated method stub
return null;
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
// TODO Auto-generated method stub
System.out.print("onStartCommend()");
return super.onStartCommand(intent, flags, startId);
}

@Override
public void onDestroy() {
// TODO Auto-generated method stub
System.out.print("onDestory()");
super.onDestroy();
}
}

main.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/start"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="@string/start" />

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

</LinearLayout>

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

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

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

</manifest>
=============================================================
3、小球点击出现
mainActivity

package com.tarena.day02_02;

import android.app.Activity;
import android.graphics.Color;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.View.OnTouchListener;
import android.widget.Button;
import android.widget.LinearLayout;

public class Day02_02Activity extends Activity {
MyView m;
int a=5;
int b=5;
//
int[] colors  = {Color.BLUE,Color.CYAN,Color.DKGRAY,Color.MAGENTA,Color.RED,Color.GREEN};
/** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        //获取布局
        LinearLayout layout = (LinearLayout)findViewById(R.id.root);
       m = new MyView(this);
           layout.addView(m);  
           //
       Button start = (Button)findViewById(R.id.button1);
       start.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
  int a=(int)(Math.random()*colors.length);//0--1
int color = colors[a];
Ball ball = new Ball(m,color);
m.addBall(ball);
MyThread t = new MyThread(ball);
//启动线程
t.start();
}
});
    }
    //QQ:573435795
   
    Handler h = new Handler(){
      public void handleMessage(Message msg) {
      if(msg.what==0x1122){
      //重绘面板
      m.invalidate();
      }
      }
    };
    //创建一个线程控制小球移动
    class MyThread extends Thread{
      Ball ball;
      public MyThread(Ball ball){
      this.ball = ball;
      }
      public void run(){
      while(true){
      ball.move();
      try {
Thread.sleep(100);
} catch (InterruptedException e) {
e.printStackTrace();
}
      Message m = new Message();
      m.what=0x1122;
      h.sendMessage(m);
      }
      }
    }
}


MyView.java
package com.tarena.day02_02;

import java.util.ArrayList;
import java.util.List;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.os.Bundle;
import android.view.View;

//自定义组件
public class MyView extends View{
List<Ball> bs = new ArrayList<Ball>();
public void addBall (Ball ball){
bs.add(ball);
}
public MyView(Context context) {
super(context);
// TODO Auto-generated constructor stub
}
protected void onDraw(Canvas canvas)
{
//画圆,创建画笔对象
Paint paint = new Paint();
//设置画笔所画为 小球及其颜色
for(int i=0; i<bs.size(); i++){
Ball ball = bs.get(i);
paint.setColor(ball.getColor());
canvas.drawCircle(ball.getX(), ball.getY(), 15, paint);
}
}
}


Ball.java
package com.tarena.day02_02;

import android.graphics.Color;

public class Ball {
float x=30,y=30;
int color;
MyView myview;
int a=10, b=10;
int [] colors={Color.BLACK,Color.DKGRAY,Color.GREEN,Color.RED,Color.YELLOW,Color.GRAY,Color.WHITE};
public Ball(MyView m, int color){
this.myview=m;
this.color= color;
}
public void move(){   
  if(x>=myview.getWidth()-15){
  a=-a;
  }
  if(x<=15){
  a=-a;
  }
  if(y>=myview.getHeight()-15){
  b=-b;
  }
  if(y<=15){
  b=-b;
  }
  this.x=x+a;
  this.y=y+b;
  
}
public float getX() {
return x;
}

public void setX(float x) {
this.x = x;
}

public float getY() {
return y;
}

public void setY(float y) {
this.y = y;
}
public int getColor() {
return color;
}

public void setColor(int color) {
this.color = color;
}
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值