在我的 Android 应用程序中,我想在 Android Studio 中使用 Java 对 textView 中的文本进行首次搜索时使用 .requestFocus()
。这是我经历过的代码 fragment ...
String mytext = "Menu1";
mytext.requestFocus();
但是这里我遇到了错误..
最佳答案
请求焦点仅适用于 View ,不适用于原始数据类型。 为了让 Textview 通过使用 XML 或以编程方式保持焦点,
// Parent view
LinearLayout ll = …
final int childCount = ll.getChildCount();
for (int i = 0; i < childCount; i++) {
View v = ll.getChildAt(i);
// Check is v is instanceof TextView.
}
Xml 属性,
// Based on the requirement
android:focusable="true"
android:focusableInTouchMode = "true"
以编程方式,
TextView text = view.findViewById(R.id.textView);
text.requestFocus();
关于java - 如何使用 Java 为 Android Studio 中的布局中的文本首次搜索提供 .requestFocus() ?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58497365/