*问题描述:对于一个按钮,如果同时需要处理单击以及长按的连续处理,怎样实现。
*解决方案:
1.分别使用onClick或onTouch和onLongClick
存在的问题:对于点击onClick以及onTouch,都可以处理,但是对于长按的连续处理,onLongClick只能办到一次,无法做到连续处理。
2.使用onTouch开启线程处理,点击时开启线程,拿起后终止线程。
好处:可以连续处理长按。将长按与点击做相同处理。
实现栗子主要完成的功能:
1.自定义View在中心画了一条直线。
2.双击MyView,出现++和–按钮。
3.单击或长按++向右移动,–向左移动。
4.移动边界为MyView的边界。
代码缺陷:
1.两个Button按钮点击一次后背景大小变了。
2.代码有些不够简洁。
3.MyView中moveLine中的type应该使用全局定义变量,用Application改进。
实现代码如下:
MainActivity.java
package com.example.testdoubleclick;
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.OnLongClickListener;
import android.view.View.OnTouchListener;
import android.widget.Button;
import android.widget.Toast;
public class MainActivity extends Activity implements OnTouchListener{
private long firClick = 0,secClick = 0;
private MyView myView;
private int count = 0;
private Button bt_add,bt_delete;
private boolean bl_exist = false, bl_run = false;
private int EVENTADD = 1;
private int EVENTDELETE = 2;
private int eventType = 0,step = 5;
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
bt_add = (Button)findViewById(R.id.add);
bt_delete = (Button)findViewById(R.id.delete);
myView = (MyView)findViewById(R.id.MyView);
myView.setOnTouchListener(mButtonTouchHandler);
bt_add.setOnTouchListener(mButtonTouchHandler);
bt_delete.setOnTouchListener(mButtonTouchHandler);
}
public Button.OnTouchListener mButtonTouchHandler = new Button.OnTouchListener()
{
@Override
public boolean onTouch(View v, MotionEvent event)
{
int id = v.getId();
switch(id)
{
case R.id.MyView:
if(MotionEvent.ACTION_DOWN == event.getAction())//按下去
{
count++;
if(count == 1)
{
firClick = System.currentTimeMillis();
//双击事件
} else if (count == 2)
{
if(!bl_exist)
{
secClick = System.currentTimeMillis();
if(secClick - firClick < 2000)
{
bt_add.setVisibility(Button.VISIBLE);
bt_delete.setVisibility(Button.VISIBLE);
}
bl_exist = true;
}
else
{
bt_add.setVisibility(Button.INVISIBLE);
bt_delete.setVisibility(Button.INVISIBLE);
bl_exist = false;
}
count = 0;
firClick = 0;
secClick = 0;
}
}
break;
case R.id.add:
eventType = EVENTADD;
progressTouchMove(v,event);
break;
case R.id.delete:
eventType = EVENTDELETE;
progressTouchMove(v,event);
break;
default:
break;
}
return true;
}
};
//处理直线的整体移动
public void progressTouchMove(View v,MotionEvent event)
{
showToast("progressTouchMove!");
int id = v.getId();
if(MotionEvent.ACTION_DOWN == event.getAction())
{
bl_run = true;
if( id == R.id.add )
{
bt_add.setBackgroundColor(Color.GREEN);
}
else if( id == R.id.delete )
{
bt_delete.setBackgroundColor(Color.GREEN);
}
new Thread(addRunnable).start();
}
else if(MotionEvent.ACTION_UP == event.getAction())
{
bl_run = false;
if( id == R.id.add )
{
bt_add.setBackgroundColor(Color.LTGRAY);
}
else if( id == R.id.delete )
{
bt_delete.setBackgroundColor(Color.LTGRAY);
}
}
}
private void showToast(String text)
{
Toast.makeText(this, text, Toast.LENGTH_SHORT).show();
}
private Runnable addRunnable = new Runnable() {
@Override
public void run() {
while(bl_run){
uiHandler.sendEmptyMessage(0);
try {
Thread.sleep(200l);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
};
private Handler uiHandler = new Handler(){
@Override
public void handleMessage(Message msg) {
super.handleMessage(msg);
myView.moveLine(eventType, step);
myView.invalidate();
}
};
}
自定义视图类,主要画了一条直线
MyView.java
package com.example.testdoubleclick;
import android.R.integer;
import android.app.Activity;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.util.AttributeSet;
import android.util.Log;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
import android.view.View;
public class MyView extends View
{
private Paint pLinePaint = new Paint();
private Paint paintTwo = new Paint();
private int i_wid = 0;
private int i_height = 0;
private String TAG = "MyView";
private int count = 0;
private float xStart = 0, yStart = 0;
private float xStop = 0, yStop = 0;
public MyView(Context context, AttributeSet attrs)
{
super(context,attrs);
Log.i(TAG, "myView construct");
}
//绘图
public void onDraw(Canvas canvas)
{
Log.i(TAG, "myView onDraw");
super.onDraw(canvas);
if( 0 == this.i_wid && 0 == this.i_height )
{
this.i_wid = this.getWidth();
this.i_height = this.getHeight();
xStart = i_wid/2;
xStop = xStart;
yStart = 0;
yStop = i_height;
}
else;
pLinePaint.setStrokeWidth(5);
canvas.drawLine(xStart, yStart, xStop, yStop, pLinePaint);
}
//向右移动
public void moveLine(int type,int step)
{
if(type == 1)
{
if(xStart + step < i_wid)
{
xStart += step;
xStop += step;
}
else
xStart = i_wid;
}
else if(type == 2)
{
if(xStart - step > 0)
{
xStart -= step;
xStop -= step;
}
else
xStart = 0;
}
invalidate();
}
}
MainActivity.xml
<RelativeLayout 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:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="com.example.testdoubleclick.MainActivity" >
<com.example.testdoubleclick.MyView
android:id="@+id/MyView"
android:layout_width="fill_parent"
android:layout_height="fill_parent" />
<Button
android:id="@+id/add"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:text="++"
android:textSize="20sp"
android:visibility="invisible" />
<Button
android:id="@+id/delete"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="--"
android:textSize="20sp"
android:visibility="invisible"
android:layout_below="@id/add"
/>
RelativeLayout>