您的位置:首页 > 其它

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

2013-07-05 17:49 204 查看
四大组件:
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;
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: