如何在视图组中导航

时间:2017-12-07 20:45:43

标签: java android android-viewgroup

在HTML中,您可以使用以下内容在页面内部导航:

<a href="#someElement">Go To Element</a>

Android应用程序中的等效内容是什么?

我有一个Activity,其中有很多文字和标题分隔文字。我希望用户能够点击某处,然后导航到特定的标题,这样他们就不必自己滚动太多。

我怎样才能做到这一点?

2 个答案:

答案 0 :(得分:0)

您可以尝试使用

来自ScrollView的

scrollTo()方法

您可以使用View.getLocationOnScreen()getLocationInWindow()

获取元素的坐标 像这样:

int[] theCoorndiates = new int[2];

yourView.getLocationOnScreen(theCoordinates);

scrollView.scrollTo(theCoordinates[0], theCoordinates[1]);

只需将它们调整为您的代码

即可

的引用: https://developer.android.com/reference/android/widget/ScrollView.html#scrollTo%28int,%20int%29 https://developer.android.com/reference/android/view/View.html#getLocationOnScreen(int[])

答案 1 :(得分:0)

使用

view.clearFocus();
 view.requestFocus();

如果您使用的是EdiText并且id是mEditText而不是必须使用mEditText.clearFocus(); mEditText.requestFocus();

相关问题