80,471
社区成员




<RelativeLayout
android:id="@+id/layout_et"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#FF5000"
android:focusable="true"
android:focusableInTouchMode="true" >
<LinearLayout
android:id="@+id/layout_default"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:orientation="horizontal" >
<ImageView
android:id="@+id/iv_default"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/search_icon" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="搜索" />
</LinearLayout>
<EditText
android:id="@+id/et_search"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</RelativeLayout>
Java代码:
mEtSearch = (EditText) findViewById(R.id.et_search);
mLayoutDefaultText = (LinearLayout) findViewById(R.id.layout_default);
// editText 离开监听
mEtSearch.setOnFocusChangeListener(new OnFocusChangeListener() {
@Override
public void onFocusChange(View v, boolean hasFocus) {
// hasFocus 为false时表示点击了别的控件,离开当前editText控件
if (!hasFocus) {
if ("".equals(mEtSearch.getText().toString())) {
mLayoutDefaultText.setVisibility(View.VISIBLE);
}
}
else {
mLayoutDefaultText.setVisibility(View.GONE);
}
}
});
[/quot
验证可用<RelativeLayout
android:id="@+id/layout_et"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#FF5000"
android:focusable="true"
android:focusableInTouchMode="true" >
<LinearLayout
android:id="@+id/layout_default"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:orientation="horizontal" >
<ImageView
android:id="@+id/iv_default"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/search_icon" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="搜索" />
</LinearLayout>
<EditText
android:id="@+id/et_search"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</RelativeLayout>
Java代码:
mEtSearch = (EditText) findViewById(R.id.et_search);
mLayoutDefaultText = (LinearLayout) findViewById(R.id.layout_default);
// editText 离开监听
mEtSearch.setOnFocusChangeListener(new OnFocusChangeListener() {
@Override
public void onFocusChange(View v, boolean hasFocus) {
// hasFocus 为false时表示点击了别的控件,离开当前editText控件
if (!hasFocus) {
if ("".equals(mEtSearch.getText().toString())) {
mLayoutDefaultText.setVisibility(View.VISIBLE);
}
}
else {
mLayoutDefaultText.setVisibility(View.GONE);
}
}
});