我有一个场景,当某个动作发生时我想让某些字段可见。这样可以正常工作,但是当我在包含一些其他字段(如TextEdit)的LinearLayout上调用setVisible(View.VISIBLE)时,焦点会转移到TextEdit(或至少屏幕滚动到它)。
调用setVisibility(View.VISIBLE)时有没有办法不改变焦点?
LinearLayout的XML布局我可以调用setVisible:
<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);
}
谢谢!
更新
感谢下面的朋友,以下代码有效。它基本上使LinearLayout的所有后代都无法获得焦点,这意味着当显示时,屏幕不会滚动到它们,因为它们没有获得焦点。到目前为止,我发现只能发生这种情况......
((ViewGroup) fieldView).setDescendantFocusability(ViewGroup.FOCUS_BLOCK_DESCENDANTS);
fieldView.setVisibility(View.VISIBLE);
((ViewGroup) fieldView).setDescendantFocusability(ViewGroup.FOCUS_AFTER_DESCENDANTS);
答案 0 :(得分:5)
您可以尝试使用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.
答案 1 :(得分:0)
我发现将 View.INVISIBLE 更改为 View.VISIBLE 不会导致焦点发生变化。