• 搜索页


    开发中经常需要为用户提供一个单独的搜索页面,这里做一下记录。

    先贴上最终的效果:

    1.输入框背景,通过定义drawable实现:

    <!--bg_et_search.xml-->
    <?xml version="1.0" encoding="utf-8"?>
    <shape xmlns:android="http://schemas.android.com/apk/res/android">
        <solid android:color="@color/white" />
        <corners android:radius="8dp" />
    </shape>

    2.布局文件:

    <!-- title_bar_search.xml-->
    <?xml version="1.0" encoding="utf-8"?>
    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:tools="http://schemas.android.com/tools"
        android:layout_width="match_parent"
        android:layout_height="?attr/actionBarSize"
        android:background="@color/colorPrimary">
    
        <EditText
            android:id="@+id/title_bar_search_et"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_marginBottom="8dp"
            android:layout_marginLeft="15dp"
            android:layout_marginRight="65dp"
            android:layout_marginTop="8dp"
            android:background="@drawable/bg_et_search"
            android:gravity="center_vertical"
            android:hint="请输入关键字"
            android:paddingLeft="10dp"
            android:singleLine="true"
            android:textSize="14sp" />
    
        <ImageView
            android:id="@+id/title_bar_iv_clear"
            android:layout_width="30dp"
            android:layout_height="30dp"
            android:layout_alignParentRight="true"
            android:layout_centerVertical="true"
            android:layout_marginRight="75dp"
            android:padding="5dp"
            android:src="@android:drawable/ic_delete"
            android:visibility="gone" />
    
        <TextView
            android:id="@+id/title_bar_right_text"
            android:layout_width="60dp"
            android:layout_height="match_parent"
            android:layout_alignParentRight="true"
            android:clickable="true"
            android:gravity="center"
            android:text="取消"
            android:textColor="@color/colorTitleText"
            android:textSize="16sp"
            android:visibility="visible" />
    
    </RelativeLayout>

    3.代码

    public class SearchActivity extends BaseActivity implements TextWatcher, TextView.OnEditorActionListener {
        private static final String TAG = SearchActivity.class.getSimpleName();
        private EditText mEditTextSearch;
        private TextView mTextViewDoSearch;
        private ImageView mImageViewClear;
    
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
    
            mEditTextSearch.addTextChangedListener(this);
            mEditTextSearch.setOnEditorActionListener(this);
            mTextViewDoSearch.setOnClickListener(this);
            mImageViewClear.setOnClickListener(this);
            mEditTextSearch.setInputType(InputType.TYPE_CLASS_NUMBER);
        }
    
        @Override
        public void initView() {
            setContentView(R.layout.activity_search_bed);
            mEditTextSearch = (EditText) findViewById(R.id.title_bar_search_et);
            mTextViewDoSearch = (TextView) findViewById(R.id.title_bar_right_text);
            mImageViewClear = (ImageView) findViewById(R.id.title_bar_iv_clear);
        }
    
    
        @Override
        public void beforeTextChanged(CharSequence s, int start, int count, int after) {
            Log.d(TAG, "beforeTextChanged()  s==" + s);
        }
    
        @Override
        public void onTextChanged(CharSequence s, int start, int before, int count) {
            Log.d(TAG, "onTextChanged() s==" + s);
        }
    
        @Override
        public void afterTextChanged(Editable s) {
            Log.d(TAG, "afterTextChanged() s==" + s);
            String content = s.toString().trim();
    
            if (TextUtils.isEmpty(content)) {
                mTextViewDoSearch.setText("取消");
                mImageViewClear.setVisibility(View.GONE);
            } else {
                mTextViewDoSearch.setText("搜索");
                mImageViewClear.setVisibility(View.VISIBLE);
            }
        }
    
        @Override
        public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
            Log.d(TAG, "onEditorAction() content==" + v.getText());
            return false;
        }
    
        @Override
        public void onClick(View v) {
            switch (v.getId()) {
                case R.id.title_bar_right_text:
                    if ("取消".equals(((TextView) v).getText().toString())) {
                        onBackPressed();
                    } else {
                        //搜索操作
                    }
                    break;
                case R.id.title_bar_iv_clear:
                    mEditTextSearch.setText("");
                    break;
            }
            super.onClick(v);
        }
    }
  • 相关阅读:
    PHP 数据库连接
    php函数
    php数组基础
    Jquery元素追加和删除
    jquery获取父,同,子级元素
    深入理解css中position属性及z-index属性
    php运算符
    MySQL主键与索引的区别和联系
    php 常量
    20150408--Sphinx+邮件激活-02
  • 原文地址:https://www.cnblogs.com/hsji/p/5218002.html
Copyright © 2020-2023  润新知