RelativeLayout onClick onTouch не работает

У меня проблемы с прикреплением событий onTouch/onClick для RelativeLayout. Я много искал в интернете и мог найти возможное решение, используя dispatchTouchEvent(MotionEvent), Но я не хочу использовать этот способ, поскольку у меня тоже есть проблемы с его реализацией. Код прилагается ниже.

public class ConnectMeDigit extends RelativeLayout implements AddressAware {



    public ConnectMeDigit(Context context, AttributeSet attrs) {
        super(context, attrs);
        View view = LayoutInflater.from(context).inflate(R.layout.numpad_digit, this);

        Typeface face=Typeface.createFromAsset(context.getAssets(), "helveticathin.ttf");//"helveticaultralight.ttf");

        String xmlProvidedSize = attrs.getAttributeValue("http://schemas.android.com/apk/res/android", "tag");
        TextView keypadnumber = (TextView) view.findViewById(R.id.keypadnumber);
        TextView keypadalphabets = (TextView) view.findViewById(R.id.keypadalphabets);
        TextView keypadplussubscript = (TextView) view.findViewById(R.id.keypadplussubscript);

        keypadnumber.setTypeface(face);
        keypadalphabets.setTypeface(face);
        keypadplussubscript.setTypeface(face);

        keypadplussubscript.setVisibility(View.GONE);

        keypadnumber.setText(xmlProvidedSize);

        if(xmlProvidedSize.equals("1")) {
            keypadalphabets.setVisibility(View.GONE);
        }       
        else if(xmlProvidedSize.equals("2")) {
            keypadalphabets.setText("ABC");     
        }       
        else if(xmlProvidedSize.equals("3")) {
            keypadalphabets.setText("DEF");             
        }       
        else if(xmlProvidedSize.equals("4")) {
            keypadalphabets.setText("GHI");             
        }       
        else if(xmlProvidedSize.equals("5")) {
            keypadalphabets.setText("JKL");             
        }       
        else if(xmlProvidedSize.equals("6")) {
            keypadalphabets.setText("MNO");             
        }
        else if(xmlProvidedSize.equals("7")) {
            keypadalphabets.setText("PQRS");                
        }
        else if(xmlProvidedSize.equals("8")) {
            keypadalphabets.setText("TUV");             
        }
        else if(xmlProvidedSize.equals("9")) {
            keypadalphabets.setText("WXYZ");                
        }
        else if(xmlProvidedSize.equals("0")) {
            keypadplussubscript.setVisibility(View.VISIBLE);
            keypadalphabets.setVisibility(View.GONE);           
        }
        else if(xmlProvidedSize.equals("*")) {
            keypadalphabets.setVisibility(View.GONE);
        }
        else if(xmlProvidedSize.equals("#")) {
            keypadalphabets.setVisibility(View.GONE);
        }

        System.out.println("ConnectMeDigit.this.getTag(): " + ConnectMeDigit.this.getTag());
        System.out.println("xmlProvidedSize: " + xmlProvidedSize);
        setEnabled(true);
        setFocusable(true);
        setFocusableInTouchMode(true);//setFocusable(true);
        setClickable(true);
        setLongClickable(true);


        DialKeyListener lListener = new DialKeyListener(xmlProvidedSize);


        setOnClickListener(lListener);
        setOnTouchListener(lListener);

        if (xmlProvidedSize.equals("0")) {//("0+".equals(text)) {
            setOnLongClickListener(lListener);
        }


    }


    private AddressText mAddress;
    public void setAddressWidget(AddressText address) {
        mAddress = address;
    }

    private boolean mPlayDtmf;
    public void setPlayDtmf(boolean play) {
        mPlayDtmf = play;
    }    


    private class DialKeyListener implements OnClickListener, OnTouchListener, OnLongClickListener {
        final char mKeyCode;
        boolean mIsDtmfStarted;

        /*DialKeyListener() {
            mKeyCode = ConnectMeDigit.this.getText().subSequence(0, 1).charAt(0);
        }*/
        DialKeyListener(String character) {
            mKeyCode = character.charAt(0); //ConnectMeDigit.this.getText().subSequence(0, 1).charAt(0);
            System.out.println("mKeyCode: " + mKeyCode);
        }

        private boolean linphoneServiceReady() {
            if (!LinphoneService.isReady()) {
                Log.w("Service is not ready while pressing digit");
                Toast.makeText(getContext(), getContext().getString(R.string.skipable_error_service_not_ready), Toast.LENGTH_SHORT).show();
                return false;
            }
            return true;
        }

        public void onClick(View v) {
            if (mPlayDtmf) {
                if (!linphoneServiceReady()) return;
                LinphoneCore lc = LinphoneManager.getLc();
                lc.stopDtmf();
                mIsDtmfStarted =false;
                if (lc.isIncall()) {
                    lc.sendDtmf(mKeyCode);
                }
            }

            if (mAddress != null) {
                int lBegin = mAddress.getSelectionStart();
                if (lBegin == -1) {
                    lBegin = mAddress.length();
                }
                if (lBegin >= 0) {
                    mAddress.getEditableText().insert(lBegin,String.valueOf(mKeyCode));
                }
            }
        }

        public boolean onTouch(View v, MotionEvent event) {
            if (!mPlayDtmf) return false;
            if (!linphoneServiceReady()) return true;

            if (InCallActivity.isInstanciated()) {
                InCallActivity.instance().resetControlsHidingCallBack();
            }

            LinphoneCore lc = LinphoneManager.getLc();
            if (event.getAction() == MotionEvent.ACTION_DOWN && !mIsDtmfStarted) {
                LinphoneManager.getInstance().playDtmf(getContext().getContentResolver(), mKeyCode);
                mIsDtmfStarted = true;
            } else {
                if (event.getAction() == MotionEvent.ACTION_UP) {
                    lc.stopDtmf();
                    mIsDtmfStarted = false;
                }
            }
            return false;
        }

        public boolean onLongClick(View v) {
            if (mPlayDtmf) {
                if (!linphoneServiceReady()) return true;
                // Called if "0+" dtmf
                LinphoneCore lc = LinphoneManager.getLc();
                lc.stopDtmf();
            }

            if (mAddress == null) return true;

            int lBegin = mAddress.getSelectionStart();
            if (lBegin == -1) {
                lBegin = mAddress.getEditableText().length();
            }
            if (lBegin >= 0) {
            mAddress.getEditableText().insert(lBegin,"+");
            }
            return true;
        }
    };


}

я пытался setEnabled(true) setFocusable(true) setFocusableInTouchMode(true) setClickable(true) setLongClickable(true) и ни один из них, кажется, не работает.

1 ответ

Попробуйте использовать

view.setOnClickListener(lListener);
view.setOnTouchListener(lListener);

вместо

setOnClickListener(lListener);
setOnTouchListener(lListener);
Другие вопросы по тегам