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

来源:互联网 发布:淘宝上同款价格差好多 编辑:程序博客网 时间:2024/06/10 01:41


1.实键

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


@Overridepublic 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;
}
}


}


0 0
原创粉丝点击