clicklistener and longclicklistener on the same button?

后端 未结 3 664
清歌不尽
清歌不尽 2021-01-15 02:38

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.

3条回答
  •  春和景丽
    2021-01-15 03:23

    Yes you can do this:

    XML file:

        

    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;
    

提交回复
热议问题