[英]Not changing focus when calling setVisibility(View.VISIBLE)
I have a scenario where when an action occurs I want to make certain fields visible.我有一个场景,当一个动作发生时,我想让某些字段可见。 This works fine, however when I call setVisible(View.VISIBLE) on a LinearLayout that contains a few other fields, such as a TextEdit, the focus shifts to the TextEdit (or at least the screen scrolls to it).
这工作正常,但是当我在包含一些其他字段(例如 TextEdit)的 LinearLayout 上调用 setVisible(View.VISIBLE) 时,焦点会转移到 TextEdit(或至少屏幕滚动到它)。
Is there a way to not have the focus change when calling setVisibility(View.VISIBLE)?有没有办法在调用 setVisibility(View.VISIBLE) 时不改变焦点?
The XML layout of the LinearLayout I can calling setVisible on:我可以调用 setVisible 的 LinearLayout 的 XML 布局:
<LinearLayout
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:focusable="true"
android:focusableInTouchMode="true">
<TextView
android:id="@+id/name"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:textSize="18sp"
android:textStyle="normal"
android:paddingLeft="5dp"
android:paddingRight="5dp"
android:paddingBottom="5dp"
android:text="" />
<EditText android:id="@+id/entry"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:inputType="textMultiLine"
android:gravity="right" />
<View
android:layout_marginTop="8dp"
android:layout_width="match_parent"
android:layout_height="1dp"
android:background="@color/grey"
android:visibility="gone" />
</LinearLayout>
The code that initializes it:初始化它的代码:
private TextView mName;
private EditText mEntry;
... ...
private void initialize() {
LayoutInflater inflater = (LayoutInflater)mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
inflater.inflate(R.layout.field_multiline_entry, this);
// setup fields
mName = (TextView)findViewById(R.id.name);
mEntry = (EditText)findViewById(R.id.entry);
mEntry.addTextChangedListener(textWatcher);
}
And the code that is calling the setVisibility:以及调用 setVisibility 的代码:
if(sectionFieldView != null && sectionFieldView.getVisibility() != View.VISIBLE)
{
sectionFieldView.setVisibility(View.VISIBLE);
}
Thanks!谢谢!
UPDATE更新
Thanks to my friend below, the following code works.感谢我下面的朋友,以下代码有效。 It basically makes all the descendants of the LinearLayout unable to gain focus, which means when made visible the screen does not scroll to them as they do not gain focus.
它基本上使 LinearLayout 的所有后代无法获得焦点,这意味着当它们可见时,屏幕不会滚动到它们,因为它们没有获得焦点。 Only way to date I have found to keep this from happening...
迄今为止,我发现唯一可以防止这种情况发生的方法......
((ViewGroup) fieldView).setDescendantFocusability(ViewGroup.FOCUS_BLOCK_DESCENDANTS);
fieldView.setVisibility(View.VISIBLE);
((ViewGroup) fieldView).setDescendantFocusability(ViewGroup.FOCUS_AFTER_DESCENDANTS);
You might try the android:descendantFocusability
with beforeDescendants
parameter on your LinearLayout?您可以在 LinearLayout 上尝试使用
beforeDescendants
参数的android:descendantFocusability
吗?
Constant Value Description
beforeDescendants 0 The ViewGroup will get focus before any of its descendants.
afterDescendants 1 The ViewGroup will get focus only if none of its descendants want it.
blocksDescendants 2 The ViewGroup will block its descendants from receiving focus.
我发现将 View.INVISIBLE 更改为 View.VISIBLE 不会导致焦点发生变化。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.