clicklistener and longclicklistener on the same button?

故事扮演 提交于 2019-12-19 10:40:14

问题


i am creating a call/dial button, when i click on that call/dial button, a call will be made based on the input that is displayed in the edittext. I managed to do that part. can you guys advise me whether i can do a longer click on that same call/dial button, so that a toast can come out to ask user to choose something else??

I did some research on "setOnLongClickListener" but i am not sure if i can combine it in the same call/dial button? I have attached on the working dial function which i managed to do, wondering if the "setOnLongClickListener" can be combined together somehere in the code?

    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);

    }

this code is working. i hope a longer click can be made on the same call/dial button so the button can have a normal click to make a call, and longer click to pop out a toast. Thanks in advance.


回答1:


Note that returning "false" on the long click listener will have the UI responding to the long click as a short click too. Return "true" if you want to kill that off. "True" means "yes, I used this event" and "false" means "whether I used it or not, the environment is free to respond as well." (I know this because I just used AkashG's answer in my own app.)




回答2:


A GestureDetector with a SimpleOnGestureListener would help you differentiate between the different types of presses. A GestureDectector is a class which can read different types of touch events (for example, single taps and long presses), and sends them to a listener which handles each type differently. Here's the documentation on the Detector and Listener.

http://developer.android.com/reference/android/view/GestureDetector.html

http://developer.android.com/reference/android/view/GestureDetector.SimpleOnGestureListener.html

First, set up your SimpleOnGestureListener, the important methods for you to override will be onSingleTapUp and onLongPress. In your onCreate, create an instance of GestureDetector that references your listener. Then, attach an OnTouchListener to your button that sends the event to your detector. You'll want it to look something like this:

//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;
    }

There a a bunch of other types of events this class can interpret in case you want to get even more fancy. GestureDetector is a very good class to do a little research on, it can be very useful and isn't too complex. Hope this helps.




回答3:


Yes you can do this:

XML file:

    <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"/>

For button click event:

        Button button=(Button) findViewById(R.id.call);
        button.setOnLongClickListener(new OnLongClickListener() {

            public boolean onLongClick(View v) {
                Toast.makeText(getBaseContext(), "Long CLick", Toast.LENGTH_SHORT).show();
                return false;
            }
        });

        button.setOnClickListener(new OnClickListener() {
            public void onClick(View v) {


             if (display != null) {
                Intent callNumber = new Intent();
                callNumber
                        .setAction(android.content.Intent.ACTION_CALL);
                callNumber.setData(Uri.parse("tel:" + display.getText()));
                startActivity(callNumber);
            }
            }
        });

For imageButton:

        ImageButton imageButton=(ImageButton) findViewById(R.id.callBtn);

imageButton.setOnClickListener(new OnClickListener() {

            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;
            }
        });

Declare this at the top(golbally):

boolean check=false;


来源:https://stackoverflow.com/questions/11374444/clicklistener-and-longclicklistener-on-the-same-button

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!