首页 > 代码库 > 自定义超简单SearchView搜索框
自定义超简单SearchView搜索框
先看效果图
Java代码:
import android.os.Bundle; import android.text.Editable; import android.text.TextWatcher; import android.view.KeyEvent; import android.view.View; import android.view.View.OnClickListener; import android.view.View.OnKeyListener; import android.widget.Button; import android.widget.EditText; import android.widget.LinearLayout; import android.widget.Toast; import android.app.Activity; public class MainActivity extends Activity { private EditText mEtSearch = null;// 输入搜索内容 private Button mBtnClearSearchText = null;// 清空搜索信息的按钮 private LinearLayout mLayoutClearSearchText = null; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); mEtSearch = (EditText) findViewById(R.id.et_search); mBtnClearSearchText = (Button) findViewById(R.id.btn_clear_search_text); mLayoutClearSearchText = (LinearLayout) findViewById(R.id.layout_clear_search_text); mEtSearch.addTextChangedListener(new TextWatcher() { @Override public void onTextChanged(CharSequence s, int start, int before, int count) { } @Override public void beforeTextChanged(CharSequence s, int start, int count, int after) { } @Override public void afterTextChanged(Editable s) { int textLength = mEtSearch.getText().length(); if (textLength > 0) { mLayoutClearSearchText.setVisibility(View.VISIBLE); } else { mLayoutClearSearchText.setVisibility(View.GONE); } } }); mBtnClearSearchText.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { mEtSearch.setText(""); mLayoutClearSearchText.setVisibility(View.GONE); } }); mEtSearch.setOnKeyListener(new OnKeyListener() { @Override public boolean onKey(View arg0, int keyCode, KeyEvent event) { if (keyCode == KeyEvent.KEYCODE_ENTER) { Toast.makeText(MainActivity.this, mEtSearch.getText().toString().trim(), Toast.LENGTH_LONG).show(); } return false; } }); } }
布局文件
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="vertical"> <LinearLayout android:layout_width="fill_parent" android:layout_height="50dp" android:layout_marginTop="5dp" android:layout_marginLeft="20dp" android:layout_marginRight="12dp" android:background="@drawable/shape_search_app_bg_yj" android:orientation="horizontal"> <!-- 输入的搜索信息 --> <EditText android:id="@+id/et_search" android:layout_weight="4" android:layout_width="0dp" android:layout_height="fill_parent" android:imeOptions="actionSearch" android:layout_marginLeft="10dp" android:textColor="#0e0e0e" android:textSize="17sp" android:singleLine="true" android:hint="搜索" android:textColorHint="#b0c6ce" android:gravity="center_vertical" android:drawableLeft="@drawable/ic_search_app_left" android:background="@null"/> <!-- 清空搜索文字按钮 --> <LinearLayout android:id="@+id/layout_clear_search_text" android:layout_weight="0.5" android:layout_width="0dp" android:layout_height="fill_parent" android:visibility="gone" android:orientation="horizontal"> <Button android:id="@+id/btn_clear_search_text" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="right|center_vertical" android:background="@drawable/selector_btn_search_clear_text_right" /> </LinearLayout> </LinearLayout> </LinearLayout>
源码下载
自定义超简单SearchView搜索框
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。