如何在 android 中使用长按和录音
How to use Long press and record sound in android
在我的应用程序中,我需要使用单击并长按按钮
点击某些东西(调用 Itemclick())并长按录制声音
并使用此代码:
send.setOnTouchListener(new OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
switch(event.getAction()){
case MotionEvent.ACTION_DOWN:
{
recording =false;
t=new Timer();
t.scheduleAtFixedRate(new TimerTask() {
@Override
public void run() {
runOnUiThread(new Runnable() {
@Override
public void run() {
if(count>=2)
t.cancel();
count++;
}
});
}
}, 1, 1000);
break;
}
case MotionEvent.ACTION_MOVE:
{
if(count >=2&&!recording)
{
recording=true;
count=0;
Rec=new Recorder();
Rec.startRecord();
}
break;
}
case MotionEvent.ACTION_UP:
{
t.cancel();
if(!recording)
{
count=0;
Itemclick();
break;
}
recording=false;
Rec.StopRecord();
db.open();
db.insert_offline(Rec.getName(), true, false);
listItems.add(Rec.getName());
type.add("v");
adapter.notifyDataSetChanged();
db.close();
Rec=null;
break;
}
}
return false;
}
});
在此代码中,我使用计时器来查找长按,但我想要一种更简单、更快速的方法来执行此操作。
您有什么想法或更好的解决方案吗?
您或许可以替换计时器并使用 android 的内置事件侦听器进行点击和按住事件:
View.OnLongClickListener
View.OnClickListener
按照这条路线,您的代码将如下所示。
send.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
//code for click event
}
});
send.setOnLongClickListener(new View.OnLongClickListener() {
@Override
public boolean onLongClick(View v) {
//code for hold event... which sounds like you want to begin recording here
return true; //indicate we're done listening to this touch listener
}
});
send.setOnTouchListener(new OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
switch(event.getAction()){
case MotionEvent.ACTION_UP:
{
//stop recording voice if a long hold was detected and a recording started
return true; //indicate we're done listening to this touch listener
}
}
return false;
}
});
在我的应用程序中,我需要使用单击并长按按钮
点击某些东西(调用 Itemclick())并长按录制声音
并使用此代码:
send.setOnTouchListener(new OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
switch(event.getAction()){
case MotionEvent.ACTION_DOWN:
{
recording =false;
t=new Timer();
t.scheduleAtFixedRate(new TimerTask() {
@Override
public void run() {
runOnUiThread(new Runnable() {
@Override
public void run() {
if(count>=2)
t.cancel();
count++;
}
});
}
}, 1, 1000);
break;
}
case MotionEvent.ACTION_MOVE:
{
if(count >=2&&!recording)
{
recording=true;
count=0;
Rec=new Recorder();
Rec.startRecord();
}
break;
}
case MotionEvent.ACTION_UP:
{
t.cancel();
if(!recording)
{
count=0;
Itemclick();
break;
}
recording=false;
Rec.StopRecord();
db.open();
db.insert_offline(Rec.getName(), true, false);
listItems.add(Rec.getName());
type.add("v");
adapter.notifyDataSetChanged();
db.close();
Rec=null;
break;
}
}
return false;
}
});
在此代码中,我使用计时器来查找长按,但我想要一种更简单、更快速的方法来执行此操作。
您有什么想法或更好的解决方案吗?
您或许可以替换计时器并使用 android 的内置事件侦听器进行点击和按住事件:
View.OnLongClickListener
View.OnClickListener
按照这条路线,您的代码将如下所示。
send.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
//code for click event
}
});
send.setOnLongClickListener(new View.OnLongClickListener() {
@Override
public boolean onLongClick(View v) {
//code for hold event... which sounds like you want to begin recording here
return true; //indicate we're done listening to this touch listener
}
});
send.setOnTouchListener(new OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
switch(event.getAction()){
case MotionEvent.ACTION_UP:
{
//stop recording voice if a long hold was detected and a recording started
return true; //indicate we're done listening to this touch listener
}
}
return false;
}
});