Android搜索框效果
生活随笔
收集整理的這篇文章主要介紹了
Android搜索框效果
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
轉載:http://blog.csdn.net/walker02/article/details/7917392
需求:項目中的有關搜索的地方,加上清空文字的功能,目的是為了增加用戶體驗,使用戶刪除文本更加快捷
解決過程:開始的時候感覺這個東西不太好實現,主要就是布局的問題,可能是開始顧慮的太多了,再加上當時產品催的不太緊,而且這個功能也不是必須實現的。但是今天不一樣了,這個是老大讓加上的,說別的很多應用中都有這個功能,沒辦法那就加上唄,試著去使用了相對布局去實現,把一個刪除按鍵放在編輯框的右上方,當文字的時候就把刪除按鍵給顯示出來,當編輯框為空的時候就把刪除按鍵給隱藏掉。布局代碼
<?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"android:orientation="vertical"android:paddingBottom="50dp"android:layout_width="fill_parent"android:layout_height="fill_parent"><RelativeLayout android:id="@+id/top"android:layout_width="fill_parent"android:layout_alignParentTop="true"android:paddingLeft="10dp"android:paddingRight="10dp"android:background="@drawable/top_background"android:layout_height="wrap_content"><Button android:id="@+id/btnSearch"android:layout_width="wrap_content"android:layout_height="wrap_content"android:gravity="center"android:layout_centerVertical="true"android:layout_alignParentRight="true"android:textSize="12sp"android:textStyle="bold"android:background="@drawable/search_btn_background"android:text="搜索"/><RelativeLayout android:id="@+id/rlSearchFrameDelete"android:layout_width="fill_parent"android:layout_height="wrap_content"android:layout_centerVertical="true"android:gravity="center_vertical"android:layout_toLeftOf="@id/btnSearch"><EditText android:id="@+id/etSearch"android:layout_width="fill_parent"android:layout_height="wrap_content"android:singleLine="true"android:background="@drawable/search_frame"android:layout_marginRight="10dp"android:paddingLeft="32dp"android:textSize="12sp"android:hint="請輸入文字..."/><ImageView android:id="@+id/ivDeleteText"android:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_alignParentRight="true"android:src="@drawable/delete"android:layout_centerInParent="true"android:paddingRight="20dp"android:visibility="gone"/></RelativeLayout></RelativeLayout></RelativeLayout>這代碼是直接從項目那截取過來的,里面用到了一些小技巧,開發的時候用到的布局寫法,其中以一種背景平鋪,這個在以前的文章里講述過。在主程序里主要是使用了EditText監聽輸入的功能,這個以前的文章也寫過,這次在使用又復習了一遍。代碼如下
public void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);ivDeleteText = (ImageView) findViewById(R.id.ivDeleteText);etSearch = (EditText) findViewById(R.id.etSearch);ivDeleteText.setOnClickListener(new OnClickListener() {public void onClick(View v) {etSearch.setText("");}});etSearch.addTextChangedListener(new TextWatcher() {public void onTextChanged(CharSequence s, int start, int before, int count) {// TODO Auto-generated method stub }public void beforeTextChanged(CharSequence s, int start, int count,int after) {// TODO Auto-generated method stub }public void afterTextChanged(Editable s) {if (s.length() == 0) {ivDeleteText.setVisibility(View.GONE);} else {ivDeleteText.setVisibility(View.VISIBLE);}}});現在就可以實現開始描述的要求了。這里面還用到了一張背景圖是.9.png的,能大能小哦?
Demo代碼:http://pan.baidu.com/s/1pJOiXab
轉載于:https://www.cnblogs.com/AndroidJotting/p/4769437.html
總結
以上是生活随笔為你收集整理的Android搜索框效果的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: HTML 5 全局属性
- 下一篇: Android Fragment 生命周