Android clicklistener和longclicklistener在同一个按钮上?
我正在创建一个呼叫/拨号按钮,当我单击该呼叫/拨号按钮时,将根据edittext中显示的输入进行呼叫。我设法做到了这一点。你们能不能告诉我,我是否可以在同一个呼叫/拨号按钮上再点击一次,这样就可以出来祝酒,请用户选择其他东西 我对“setOnLongClickListener”做了一些研究,但我不确定是否可以将其组合到同一个呼叫/拨号按钮中?我已经附上了我设法实现的工作拨号功能,想知道“setOnLongClickListener”是否可以在代码中的某个地方组合在一起Android clicklistener和longclicklistener在同一个按钮上?,android,onclick,actionlistener,phone-call,long-click,Android,Onclick,Actionlistener,Phone Call,Long Click,我正在创建一个呼叫/拨号按钮,当我单击该呼叫/拨号按钮时,将根据edittext中显示的输入进行呼叫。我设法做到了这一点。你们能不能告诉我,我是否可以在同一个呼叫/拨号按钮上再点击一次,这样就可以出来祝酒,请用户选择其他东西 我对“setOnLongClickListener”做了一些研究,但我不确定是否可以将其组合到同一个呼叫/拨号按钮中?我已经附上了我设法实现的工作拨号功能,想知道“setOnLongClickListener”是否可以在代码中的某个地方组合在一起 private v
private void dialANumber() {
try {
buttonCall = (ImageButton) findViewById(R.id.imageButton2);
buttonCall.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
// TODO Auto-generated method stub
if (display != null) {
Intent callNumber = new Intent();
callNumber
.setAction(android.content.Intent.ACTION_CALL);
callNumber.setData(Uri.parse("tel:" + display.getText()));
startActivity(callNumber);
}
}
});
} catch (ActivityNotFoundException anfe) {
Log.e("DialANumber", "Dialing the number failed", anfe);
}
此代码正在运行。我希望可以在同一个呼叫/拨号按钮上长时间单击,这样按钮可以正常单击以拨打电话,长时间单击以弹出祝酒词。提前感谢。是的,您可以这样做:
XML文件:
<Button
android:id="@+id/call"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="CALL"/>
<ImageButton
android:id="@+id/callBtn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@drawable/ic_launcher"/>
对于imageButton:
ImageButton imageButton=(ImageButton) findViewById(R.id.callBtn);
setOnClickListener(新的OnClickListener(){
在顶部声明(天哪):
带有SimpleOnGestureListener的GestureDetector可以帮助您区分不同类型的按键。GestureDetector是一个可以读取不同类型的触摸事件(例如,单次点击和长时间按下)的类,并将它们发送到一个侦听器,该侦听器以不同的方式处理每种类型 首先,设置SimpleOnGestureListener,您要覆盖的重要方法将是
onSingleTapUp
和onLongPress
。在onCreate
中,创建一个引用您的侦听器的GestureDetector实例。然后,在按钮上附加一个OnTouchListener
,将事件发送到您的dete你会希望它看起来像这样:
//Set up the Detector
GestureDetector.SimpleOnGestureListener myGestureListener = new GestureDetector.SimpleOnGestureListener()
{
@Override
public boolean onSingleTapUp(MotionEvent e)
{
//Your onClick code
return false;
}
@Override
public void onLongPress(MotionEvent e)
{
//Your LongPress code
super.onLongPress(e);
}
};
//And make a variable for your GestureDetector
GestureDetector myGestureDetector;
...
@Override
onCreate(Bundle b)
{
...
myGestureDetector = new GestureDetector(myActivity.this, myGestureListener);
...
}
...
//And finally, wherever you are setting up your button
button.setOnTouchListener(new View.OnTouchListener(){
@Override
public boolean onTouch(View view, MotionEvent motionEvent)
{
myGestureDetector.onTouchEvent(motionEvent);
return false;
}
这个类还可以解释许多其他类型的事件,以防你想变得更有趣。GestureDetector是一个非常好的类,可以做一些研究,它非常有用,也不太复杂。希望这能有所帮助。注意返回“false”在长点击监听器上,用户界面也会以短点击的形式响应长点击。如果你想取消长点击,请返回“true”。“true”表示“是的,我使用了此事件”,而“false”表示“无论我使用与否,环境都可以自由响应。”(我知道这一点,因为我只是在自己的应用程序中使用了AkashG的答案。)您好,谢谢..我已经尝试过了,但是setOnLongClickListener提示需要3个修复:更改为setOnClickListener(..)、更改为setOnKeyListener(..)和更改为setOnTouchListener(..)“…我认为您必须在xml文件中声明imageButton。将其更改为only button,它不会要求您进行修复。我实现了它,并且工作正常。我将button=(button)findViewById(R.id.call);更改为buttonCall=(imageButton)findViewById(R.id.imageButton2);正如我做的那样,我的呼叫按钮是imageButton,因为我把它变成了一个图像..setOnLongclickListener不能在imageButton上工作吗?setOnLongclickListener也可以在imageButton上工作。在回复你的评论后,我尝试用imageButton做了一件事,并成功地完成了。如果你愿意,我也可以发布。嗨,伙计,我长时间点击b后,吐司会弹出但紧接着,一个调用被调用…我想在setOnLongClickListener函数完成后,setOnClickListener完成了它的工作,这是我不想要的..是的,我做了..我将button=(button)findViewById(R.id.call);改为buttonCall=(ImageButton)findViewById(R.id.imageButton2);你好,谢谢,我很快就会尝试真的。。我的笔记本电脑几天前刚刚死机。:(需要注意的是“注意,在长时间单击的侦听器上返回“false”时,用户界面也会以短时间单击的方式响应长时间单击)。已经挣扎了2个小时。
public void onClick(View v) {
if(check==false){
Toast.makeText(getBaseContext(), "CLick", Toast.LENGTH_SHORT).show();
}
imageButton.setOnLongClickListener(new OnLongClickListener() {
public boolean onLongClick(View v) {
check=true;
if(check){
Log.d("bool", check+"");
Toast.makeText(getBaseContext(), "Long CLick", Toast.LENGTH_SHORT).show();
check=false;
}
return false;
}
});
boolean check=false;
//Set up the Detector
GestureDetector.SimpleOnGestureListener myGestureListener = new GestureDetector.SimpleOnGestureListener()
{
@Override
public boolean onSingleTapUp(MotionEvent e)
{
//Your onClick code
return false;
}
@Override
public void onLongPress(MotionEvent e)
{
//Your LongPress code
super.onLongPress(e);
}
};
//And make a variable for your GestureDetector
GestureDetector myGestureDetector;
...
@Override
onCreate(Bundle b)
{
...
myGestureDetector = new GestureDetector(myActivity.this, myGestureListener);
...
}
...
//And finally, wherever you are setting up your button
button.setOnTouchListener(new View.OnTouchListener(){
@Override
public boolean onTouch(View view, MotionEvent motionEvent)
{
myGestureDetector.onTouchEvent(motionEvent);
return false;
}