首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 移动开发 > Android >

Android中Widget的应用示例

2012-09-22 
Android中Widget的使用示例Android中Widget的使用示例 之前已经写过通过重绘整个背景的方式,实现物体移动。

Android中Widget的使用示例
Android中Widget的使用示例

之前已经写过通过重绘整个背景的方式,实现物体移动。今天要讲的是,
如何通过移动widget,view等控件,实现部分重绘。结合之前发Droiddraw工具,使用AbsoluteLayout,可以随意放置控件在任何位置。
package com.sky;

import android.app.Activity;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AbsoluteLayout;
import android.widget.Button;

@SuppressWarnings("deprecation")
public class AbosoluteMove extends Activity implements OnClickListener
{
??? /** Called when the activity is first created. */
public Button button1;
public Button button2;
public Button button3;
public Button button4;
Button tmp;//临时保存,选择的BUTTON
int x;
int y;
int flag;//用于标志选择哪个BUTTON
??? @Override
??? public void onCreate(Bundle savedInstanceState)
??? {
??????? super.onCreate(savedInstanceState);
??????? setContentView(R.layout.main);
??????? button1=(Button) findViewById(R.id.widget27);
??????? button2=(Button) findViewById(R.id.widget28);
??????? button3=(Button) findViewById(R.id.widget29);
??????? button4=(Button) findViewById(R.id.widget30);
??????? button1.setOnClickListener(this);
??????? button2.setOnClickListener(this);
??????? button3.setOnClickListener(this);
??????? button4.setOnClickListener(this);
??? }
@Override
public void onClick(View v)
{
// TODO Auto-generated method stub
switch(v.getId())
{
case R.id.widget27:
flag=R.id.widget27;
setTitle("button1");
break;
case R.id.widget28:
flag=R.id.widget28;
setTitle("button2");
break;
case R.id.widget29:
flag=R.id.widget29;
setTitle("button3");
break;
case R.id.widget30:
flag=R.id.widget30;
setTitle("button4");
break;
}
}


@Override
public boolean onKeyDown(int keyCode, KeyEvent event)
{
// TODO Auto-generated method stub
return super.onKeyDown(keyCode, event);
}
@Override
public boolean onTouchEvent(MotionEvent event)
{
// TODO Auto-generated method stub
x = (int) event.getX();
??????? y = (int) event.getY();
??????? tmp=(Button) findViewById(flag);//获取所选中的BUTTON
??????? AbsoluteLayout.LayoutParams params1=new AbsoluteLayout.LayoutParams(50,50,x-25,y-50);
??????? tmp.setLayoutParams(params1);//设置BUTTON的新位置
??????? switch(event.getAction())
??????? {??
??????????? case MotionEvent.ACTION_DOWN:???
??????????? tmp.invalidate();?
??????????? tmp.setText("选中down");
??????????????? break;??
??????????? case MotionEvent.ACTION_UP:???
??????????? tmp.invalidate();?
??????????? tmp.setText("选中UP");
??????????????? break;??
??????????? case MotionEvent.ACTION_MOVE:??
??????????? tmp.invalidate();??
??????????? tmp.setText("选中move");
??????????????? break;
??????? }
??????? return super.onTouchEvent(event);
}
}

热点排行