TextView 的onClick attribute 为何要增加clickable的设置

本文探讨了Android中TextView点击事件的问题,通过源码分析解释了TextView默认不可点击的原因,并提供了使TextView具备点击功能的方法。

http://smartcloudblog.blogspot.com/2011/09/android-onclicklisteners-vs.html

提到了四种onclick的实现方法,作者喜欢第四种,也即是在XML文件中配置,简单的大家都喜欢。我于是就做个尝试一个是TextView、Button来做一个简单的Demo

发现在Button的时候是好用的,而在用作TextView的时候就不行了。而在用TextView.setOnclickLinstener的时候就可以?

源码是最好的导师--这个是发哥告诉我的,于是就用 F3、F4进行源码的探索,发现在View的一段代码

**
     * Register a callback to be invoked when this view is clicked. If this view is not
     * clickable, it becomes clickable.
     *
     * @param l The callback that will run
     *
     * @see #setClickable(boolean)
     */
    public void setOnClickListener(OnClickListener l) {
        if (!isClickable()) {
            setClickable(true);
        }
        mOnClickListener = l;
    }

发现setOnClickListener 一共做了两件事情

1、让这个View变得是可以点击。

2、注册处理事件

于是就测试一下是不是正确,一下几行代码,验证TextView的默认clickable值以及其改变

        boolean clickable = this.findViewById(R.id.mTextView).isClickable();
        Log.d("Before setOnClickListener", String.valueOf(clickable));
        this.findViewById(R.id.mTextView).setOnClickListener(new View.OnClickListener() {
			
			@Override
			public void onClick(View v) {
				Toast.makeText(TestAndroidXMLOnclickActivity.this, "from onclick inner class", Toast.LENGTH_SHORT).show();
				
			}
		});
        clickable = this.findViewById(R.id.mTextView).isClickable();
        Log.d("After setOnClickListener", String.valueOf(clickable));


结果如下


猜想正确。

有了以上的实验,就找到问题的原因了:

TextView的默认是不支持点击的,要是在xml中配置onclick aattribute的话,必须得陪clickable并是其为true。

/* * Copyright (C) 2018 The Android Open Source Project * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ package androidx.databinding; import java.lang.annotation.ElementType; import java.lang.annotation.Target; /** * BindingAdapter is applied to methods that are used to manipulate how values with expressions * are set to views. The simplest example is to have a public static method that takes the view * and the value to set: * <p><pre> *<code>@BindingAdapter("android:bufferType") * public static void setBufferType(TextView view, TextView.BufferType bufferType) { * view.setText(view.getText(), bufferType); * }</code></pre> * In the above example, when android:bufferType is used on a TextView, the method * setBufferType is called. * <p> * It is also possible to take previously set values, if the old values are listed first: * <p><pre> *<code>@BindingAdapter("android:onLayoutChange") * public static void setOnLayoutChangeListener(View view, View.OnLayoutChangeListener oldValue, * View.OnLayoutChangeListener newValue) { * if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) { * if (oldValue != null) { * view.removeOnLayoutChangeListener(oldValue); * } * if (newValue != null) { * view.addOnLayoutChangeListener(newValue); * } * } * }</code></pre> * When a binding adapter may also take multiple attributes, it will only be called when all * attributes associated with the binding adapter have binding expressions associated with them. * This is useful when there are unusual interactions between attributes. For example: * <p><pre> *<code>@BindingAdapter({"android:onClick", "android:clickable"}) * public static void setOnClick(View view, View.OnClickListener clickListener, * boolean clickable) { * view.setOnClickListener(clickListener); * view.setClickable(clickable); * }</code></pre> * The order of the parameters must match the order of the attributes in values in the * BindingAdapter. * <p> * A binding adapter may optionally take a class extending DataBindingComponent as the first * parameter as well. If it does, it will be passed the value passed in during binding, either * directly in the inflate method or indirectly, using the value from * {@link DataBindingUtil#getDefaultComponent()}. * <p> * If a binding adapter is an instance method, the generated DataBindingComponent will have * a getter to retrieve an instance of the BindingAdapter's class to use to call the method. */ @Target(ElementType.METHOD) public @interface BindingAdapter { /** * @return The attributes associated with this binding adapter. */ String[] value(); /** * Whether every attribute must be assigned a binding expression or if some * can be absent. When this is false, the BindingAdapter will be called * when at least one associated attribute has a binding expression. The attributes * for which there was no binding expression (even a normal XML value) will * cause the associated parameter receive the Java default value. Care must be * taken to ensure that a default value is not confused with a valid XML value. * * @return whether or not every attribute must be assigned a binding expression. The default * value is true. */ boolean requireAll() default true; } 翻译并注释
最新发布
09-25
评论 3
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值