【问题标题】:ImageButton with clickable = "false" is still clickable带有 clickable = "false" 的 ImageButton 仍然可以点击
【发布时间】:2020-08-02 07:18:26
【问题描述】:

我使用 ImageButton 拨打电话,但在我点击 showPhoneNumberBtn ImageButton 之前它不应该是可点击的。

ImageButton showPhoneNumberBtn = root.findViewById(R.id.showPhoneNumberBtn);
showPhoneNumberBtn.setOnClickListener(new View.OnClickListener() {
    @SuppressLint("ResourceAsColor")
    @Override
    public void onClick(View v) {
        TextView phoneNumber = root.findViewById(R.id.phoneNumberText);
        phoneNumber.setText(currentUser.getPhoneNumber());
        ImageButton makeCallBtn = root.findViewById(R.id.makeCallBtn);
        makeCallBtn.setClickable(true);
        Drawable img = getContext().getDrawable(R.drawable.ic_call_black_24dp);
        img.setTint(Color.parseColor("#3CB371"));
        makeCallBtn.setBackground(img);
    }
});

ImageButton makeCallBtn = root.findViewById(R.id.makeCallBtn);
makeCallBtn.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {
        ActivityCompat.requestPermissions(getActivity(), new String[]{Manifest.permission.CALL_PHONE}, 1);
        if (ActivityCompat.checkSelfPermission(getContext(), Manifest.permission.CALL_PHONE) == PackageManager.PERMISSION_GRANTED) {
            Intent callIntent = new Intent(Intent.ACTION_CALL);
            callIntent.setData(Uri.parse("tel:" + phoneNumber));
            startActivity(callIntent);
        } else {
            Toast.makeText(getContext(), "You don't assign permission.", Toast.LENGTH_SHORT).show();
        }
    }
});

【问题讨论】:

    标签: android onclicklistener android-phone-call


    【解决方案1】:

    使用setOnClickListener 会将可点击状态重置为真。您需要在之后将其设置为 false。

    来自源代码:

    public void setOnClickListener(OnClickListener l) {
        if (!isClickable()) {
            setClickable(true);
        }
        getListenerInfo().mOnClickListener = l;
    }
    

    详细解释可以看here

    【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-03-08
    • 1970-01-01
    • 2015-09-06
    相关资源
    最近更新 更多