0

我有一个包含 EditText 和 ImageView 的线性布局。我给 EditText 一个 @null 背景,并给 LinearLayout 一个背景:

?android:attr/editTextBackground

使它看起来像整个事情是一个小部件。当 EditText 获得焦点/被选中时,我想更新线性布局的背景可绘制对象以显示整个内容已被选中。

我的线性布局的布局 XML:

<LinearLayout
    android:id="@+id/search_plate"
    android:orientation="horizontal"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:background="?android:attr/editTextBackground">

    <EditText
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:id="@+id/edit_text"
        android:layout_weight="1"
        android:background="@null"
        android:height="36dp"/>

    <ImageView
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:id="@+id/image_view_close"
        android:src="@drawable/ic_clear"
        android:focusable="true"
        android:background="?android:attr/selectableItemBackground"/>
</LinearLayout>

这是我用来尝试在 EditText 聚焦时更改 LinearLayout 的背景状态的代码:

public class IconEditText extends LinearLayout implements View.OnFocusChangeListener {

    private static final String LOG_TAG = "IconEditText";

    private View mSearchPlate;            // Linear Layout
    private EditText mEditTextSearch;

    public IconEditText(Context context) {
        this(context, null);
    }

    public IconEditText(Context context, AttributeSet attrs) {
        super(context, attrs);

        LayoutInflater inflater = (LayoutInflater) context
                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        inflater.inflate(R.layout.icon_edit_text, this, true);

        mSearchPlate = findViewById(R.id.search_plate);
        mEditTextSearch = (EditText) findViewById(R.id.editText);

        mEditTextSearch.setOnFocusChangeListener(this);

    }

    @Override
    public void onFocusChange(View view, boolean focused) {
        mSearchPlate.getBackground().setState(focused ? FOCUSED_STATE_SET : EMPTY_STATE_SET);
    }
}

除了使用 FOCUSED_STATE_SET,我还尝试了以下方法:

ENABLED_FOCUSED_SELECTED_STATE_SET
FOCUSED_SELECTED_STATE_SET
ENABLED_SELECTED_STATE_SET
SELECTED_STATE_SET

以上似乎都没有将 LinearLayout 的背景更改为蓝色下划线。任何帮助,将不胜感激!

4

1 回答 1

0

Seems as though: ENABLED_FOCUSED_SELECTED_WINDOW_FOCUSED_STATE_SET works... if anyone could explain that, that would be great!

于 2014-03-10T00:54:47.120 回答