View控件获得焦点,TextView获得焦点(focusable),自定义TextView使得其获得焦点,View的onFocusChange()

-- TextView的点击事件如何去除- https://yq.aliyun.com/wenzhang/show_17352
 TextView.setEnabled(true);
 TextView.setEnabled(true);
 TextView.setClickable(true);
 TextView.setClickable(true);

-- Android View获取焦点
 mRlCard1.setFocusable(true);
 mRlCard1.setFocusableInTouchMode(true);
 mRlCard1.requestFocus();
 mRlCard1.clearFocus();
View focusView = rootview.findFocus();

-- LinearLayout设置获取焦点
android:focusable="true" //获得聚焦,不会立即响应
android:focusableInTouchMode="true" //获得聚焦,会立即响应

-- android:focusable和android:focusableInTouchMode的区别- https://blog.csdn.net/csdn_susan/article/details/46651557
 1.android:descendantFocusability="blocksDescendants",
beforeDescendants:viewgroup会优先其子类控件而获取到焦点   
afterDescendants:viewgroup只有当其子类控件不需要获取焦点时才获取焦点   
blocksDescendants:viewgroup会覆盖子类控件而直接获得焦点   
然后,在不想抢走焦点的控件上面写好这两句就okay:
android:clickable="true"
android:focusable="false"

linearlayout.setEnable(false);
linearlayout.setClickable(false);
setFocusable(true);//允许此控件有获得焦点的能力
requestFocus();//让控件得到焦点
setFocusableInTouchMode(false);//失去焦点

EditText et = (EditText) findViewById(R.id.et);
et.clearFocus();
et.setFocusable(false);

> TextView获得焦点

 方法一:.在原来布局文件中的TextView加入两个属性:
android:focusable ="true"
android:focusableInTouchMode="true"
 
 方法二:自定义TextView使得其获得焦点
mport android.content.Context;
import android.util.AttributeSet;
import android.view.ViewDebug.ExportedProperty;
import android.widget.TextView;
 
public class FocuedTextView1 extends TextView {
 
    public FocuedTextView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }
 
    public FocuedTextView(Context context, AttributeSet attrs) {
        super(context, attrs);
    }
 
    public FocuedTextView(Context context) {
        super(context);
 
    }
        
    @Override
    public boolean isFocused() {
        return true;   //返回一个ture就可以获取焦点
    }
}

<com.example.test.view.FocuedTextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginBottom="5dp"
        android:layout_marginTop="5dp"
        android:ellipsize="marquee"
        android:singleLine="true"
        android:text="这是一个滚动字幕条,这是一个从左往右的滚动字幕条,啦啦啦啦啦啦啦!!!!"
        android:textColor="@android:color/black"
        android:textSize="18sp" />

> View的onFocusChange()

  Android中的View类中专门提供了一个接口——View.OnFocusChangeListener,用于监听焦点改变(得到焦点和失去焦点)事件,而所有的组件上又存在都存在这监听焦点变化的方法:
public void setOnFocusChangeListener(View view, Boolean hasFocus){};
    view:发生变化的视图
hasFocus:用来判断视图是否获得了焦点。true——获得了焦点;flash——没有获得焦点。

  类似于文本框里面hint文字在初始化的时候显示或者隐藏的操作,就要用到setOnFocusChangeListener的。Edittext是没有抢夺焦点这一说的。
在onFocusChange方法中判断是否有焦点,有焦点时setHint(null),没有焦点时setHint(string);(string为editText1.getHint());
  EditText的 焦点事件 setOnFocusChangeListener
//光标处在EditText时其内容消失
        mInfo = (EditText)findViewById(R.id.old_password);
        //setOnFocusChangeListener  焦点事件
        mInfo.setOnFocusChangeListener(new View.OnFocusChangeListener() {
            //v 发生变化的视图    hasFocus:用来判断视图是否获得了焦点
            public void onFocusChange(View v,boolean hasFocus) {
                EditText _v = (EditText)v;
                if(!hasFocus) {
                    _v.setHint(_v.getTag().toString());
                } else {
                    String hint = _v.getHint().toString();
                    _v.setTag(hint);
                    _v.setHint("");
                }
            }
        });

OnFocusChangeListener接口简介以及案例分析- https://blog.csdn.net/qq_41405257/article/details/81711842

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值