简体   繁体   English

如何检查特定的视图当前是否集中在Android中

[英]How to check if a particular view is currently focussed in Android

In my android app I need to check whether a particular view is focussed. 在我的android应用中,我需要检查特定视图是否聚焦。 Now I found the getCurrentFocus() function in Activity class but this returns a View Object. 现在,我在Activity类中找到了getCurrentFocus()函数,但这返回了一个View Object。

How can I compare and check whether this returned View is same as the one in question. 如何比较并检查此返回的View是否与所讨论的View相同。 I mean there is no getName() function here. 我的意思是这里没有getName()函数。 So after getting the View object, how can I compare to check which View class is this ? 因此,在获取View对象之后,如何比较这是哪个View类?

The View.isFocused() method tells whether the view in question is focused or not. View.isFocused()方法告知所关注的视图是否已聚焦。

if (myView.isFocused()) {
    // your code
}

If you still want to use the getCurrentFocus() method, you can simply check: 如果仍然要使用getCurrentFocus()方法,则可以简单地检查:

View focusView = getCurrentFocus();
if (myView == focusView) {
    // your code
}

Or else, you can compare your views by id. 否则,您可以按ID比较您的观看次数。

View focusView = getCurrentFocus();
if (focusView != null && myView.getId() == focusView.getId()) {
    // your code
}

You could use the getId() method I guess? 我猜你可以使用getId()方法吗?

eg 例如

getCurrentFocus() == R.id.myView;

No need for getName(). 不需要getName()。 Just use the == operator. 只需使用==运算符。 See: 看到:

View myView = findViewById(...);

if (activitygetCurrentFocus() == myView)
    // ha focus

}

Another option, one I usually prefer, is to set a focus listener for the views you are interested in monitoring: 我通常更喜欢的另一种选择是为您想要监视的视图设置焦点侦听器:

myView.setOnFocusChangeListener(new OnFocusChangeListener() {
    @Override
    public void onFocusChange(View v, boolean hasFocus) {
          if (hasFocus) {
               // got focus logic
          }
    }
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何为我的自定义onClick方法提供当前聚焦的视图页面视图? - How to give a currently focussed view of the view pager to my custom onClick method? 可访问性(TalkBack):如何在加载完整片段后自动使特定视图聚焦? - Accessibility (TalkBack) : How to make a particular view focussed automatically, just after the complete fragment loads? 如何更改焦点视图的背景颜色 - How change the background color of focussed view Android:如何避免对话框中的蓝色(聚焦)按钮? - Android: How to avoid blue (focussed) buttons in dialogs? 如何在Android中查看imageview当前是否已缩放? - How to check if imageview is currently zoomed or not in android? 如何在android中检查当前的互联网连接是否可用 - How to check currently internet connection is available or not in android Android如何在按下或聚焦时使按钮文本加粗 - Android how to make button text bold when pressed or focussed Android-如何创建不同状态(默认,已按下和已聚焦)的图像按钮 - Android - How to create image button of different states, default , pressed and focussed Android如何在按下或聚焦时使TextView文本变为粗体 - Android how to make TextView text bold when pressed or focussed 如何更新当前不在屏幕上的回收者视图的特定位置的视图? - How to update the view of a particular position of a recycler view which is not currently in focus on the screen?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM