android relativelayout 点击事件,android - RelativeLayout onClick onTouch not working - Stack Overflow...

I am having trouble attaching onTouch/onClick events for a RelativeLayout. I have searched a lot on internet, and could find the possible solution as using dispatchTouchEvent(MotionEvent). But I don't want to use this way, as I am having some trouble implementing that as well. Code is attached below.

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;

}

};

}

I have tried setEnabled(true) setFocusable(true) setFocusableInTouchMode(true) setClickable(true) setLongClickable(true) and none of them seems to work.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值