android button按键按下和抬起 长按一直发送


1.实键

Log.v("TAG","onKeyDown event.getRepeatCount() "+event.getRepeatCount());


	@Override
	public boolean onKeyDown(int keyCode, KeyEvent event) {
		Log.d("TAG","onKeyDown event.getRepeatCount() "+event.getRepeatCount());
		if (keyCode == KeyEvent.KEYCODE_BACK) {
			
			moveTaskToBack(false);
			return true;
		}
		return super.onKeyDown(keyCode, event);
	}

2.触屏键

public class AppMain extends Activity{
	
	private Button mButton;
	
    /** Called when the activity is first created. */
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        
        ButtonListener b = new ButtonListener();       
        mButton = (Button)findViewById(R.id.button1);
        mButton.setOnClickListener(b);
        mButton.setOnTouchListener(b);
        mButton.setBackgroundResource(R.drawable.green);
        
    }
    
    class ButtonListener implements OnClickListener, OnTouchListener{

		public void onClick(View v) {
			if(v.getId() == R.id.button1){
				Log.d("test", "cansal button ---> click");
			}
		}

		public boolean onTouch(View v, MotionEvent event) {
			if(v.getId() == R.id.button1){
				if(event.getAction() == MotionEvent.ACTION_UP){
					Log.d("test", "cansal button ---> cancel");
					mButton.setBackgroundResource(R.drawable.green);
				} 
				if(event.getAction() == MotionEvent.ACTION_DOWN){
					Log.d("test", "cansal button ---> down");
					mButton.setBackgroundResource(R.drawable.yellow);
				}
			}
			return false;
		}
		
    }
}



3.长按不放一直有效

示例:

android文本框左右加减按钮长按一直加减

 1、最主要的问题:长按事件并不是我们想要的
 
 findViewById(R.id.test).setOnLongClickListener(
  new OnLongClickListener() {
   @Override
   public boolean onLongClick(View v) {
    Log.e("", "onLongClick");
    return true;
   }
  });

长按着test按钮,你会发现,控制台并不会一直打印出onLongClick,这也没什么奇怪的OnLongClickListener只是长按后出发的一个事件,这个事件一直到松开手,在按下,长按,松手这个
 过程只会触发OnLongClickListener一次,你想啊,用过智能机的都知道,长按文件夹移动,文件夹会跟着你跑,如果会触发很多次,那不是会弹出很多个文件夹来吗。。。
 
   所以,要实现长按让它连续加减值,OnLongClickListener事件对我们是没用的,在android有个onTouch(View v, MotionEvent event)事件,它有两个参数View v, MotionEvent event,相信大家都知道这是什么意思,所以。。。。  有想法了没,哈哈
 
   在这里贴上主要代码:

package com.example.longclick;


import android.support.v7.app.ActionBarActivity;
import android.support.v4.app.Fragment;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnTouchListener;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.TextView;


public class MainActivity extends ActionBarActivity {
public boolean isOnLongClick=false;
boolean miusEnable=false;

MiusThread miusThread;
PlusThread plusThread;
int i=0;
TextView tv;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
CompentOnTouch b = new CompentOnTouch();       
Button mButton = (Button)findViewById(R.id.button1);
        mButton.setOnTouchListener(b);
   
Button mButton2 = (Button)findViewById(R.id.button2);
        mButton2.setOnTouchListener(b);
        
        tv=(TextView)findViewById(R.id.textView1);
        
if (savedInstanceState == null) {
getSupportFragmentManager().beginTransaction()
.add(R.id.container, new PlaceholderFragment()).commit();


}
}


// Touch事件
class CompentOnTouch implements OnTouchListener {
@Override
public boolean onTouch(View v, MotionEvent event) {
switch (v.getId()) {
// 这是btnMius下的一个层,为了增强易点击性
case R.id.button1:
onTouchChange("mius", event.getAction());
break;
// 这里也写,是为了增强易点击性
case R.id.button2:
onTouchChange("plus", event.getAction());
break;
}
return true;
}
}


private void onTouchChange(String methodName, int eventAction) {
// 按下松开分别对应启动停止减线程方法
if ("mius".equals(methodName)) {
if (eventAction == MotionEvent.ACTION_DOWN) {
miusThread = new MiusThread();
isOnLongClick = true;
miusThread.start();
} else if (eventAction == MotionEvent.ACTION_UP) {
if (miusThread != null) {
isOnLongClick = false;
}
} else if (eventAction == MotionEvent.ACTION_MOVE) {
if (miusThread != null) {
isOnLongClick = true;
}
}
}
// 按下松开分别对应启动停止加线程方法
else if ("plus".equals(methodName)) {
if (eventAction == MotionEvent.ACTION_DOWN) {
plusThread = new PlusThread();
isOnLongClick = true;
plusThread.start();
} else if (eventAction == MotionEvent.ACTION_UP) {
if (plusThread != null) {
isOnLongClick = false;
}
} else if (eventAction == MotionEvent.ACTION_MOVE) {
if (plusThread != null) {
isOnLongClick = true;
}
}
}
}


// 减操作
class MiusThread extends Thread {
@Override
public void run() {
while (isOnLongClick) {
try {
Thread.sleep(200);
myHandler.sendEmptyMessage(1);
} catch (InterruptedException e) {
e.printStackTrace();
}
super.run();
}
}
}


// 加操作
class PlusThread extends Thread {
@Override
public void run() {
while (isOnLongClick) {
try {
Thread.sleep(200);
myHandler.sendEmptyMessage(2);
} catch (InterruptedException e) {
e.printStackTrace();
}
super.run();
}
}
}


// 更新文本框的值
Handler myHandler = new Handler() {
public void handleMessage(Message msg) {
switch (msg.what) {
case 1:
Log.d("TAG","send:"+i++);
tv.setText("send:"+i++);
break;
case 2:
Log.d("TAG","send:"+i--);
tv.setText("send:"+i--);
break;
}
};
};





@Override
public boolean onCreateOptionsMenu(Menu menu) {


// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}


@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}


/**
* A placeholder fragment containing a simple view.
*/
public static class PlaceholderFragment extends Fragment {


public PlaceholderFragment() {
}


@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_main, container,
false);
return rootView;
}
}


}


  • 1
    点赞
  • 14
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 2
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

小黄人软件

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值