我有一种情况,当发生某个 Action 时,我想使某些字段可见。效果很好,但是当我在包含其他几个字段(例如TextEdit)的LinearLayout上调用setVisible(View.VISIBLE)时,焦点将移至TextEdit(或至少屏幕滚动至该文本)。
调用setVisibility(View.VISIBLE)时,有没有办法改变焦点?
我可以在以下位置调用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>
初始化它的代码:
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);
}
以及调用setVisibility的代码:
if(sectionFieldView != null && sectionFieldView.getVisibility() != View.VISIBLE)
{
sectionFieldView.setVisibility(View.VISIBLE);
}
谢谢!
更新
感谢以下我的 friend ,以下代码可以正常工作。基本上,这会使LinearLayout的所有后代无法获得焦点,这意味着在可见时,屏幕不会滚动到它们,因为它们未获得焦点。迄今为止,我发现只有这样才能避免这种情况的发生...
((ViewGroup) fieldView).setDescendantFocusability(ViewGroup.FOCUS_BLOCK_DESCENDANTS);
fieldView.setVisibility(View.VISIBLE);
((ViewGroup) fieldView).setDescendantFocusability(ViewGroup.FOCUS_AFTER_DESCENDANTS);
最佳答案
您可以在LinearLayout上尝试使用带有android:descendantFocusability
参数的beforeDescendants
吗?
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.
关于android - 调用setVisibility(View.VISIBLE)时不改变焦点,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/33029332/