Android -- 编辑框更改样式

1. 效果图

 

2. 实现代码 

    activity_main.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >

    <EditText 
        android:id="@+id/txtSearch"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:hint="请输入"/>

</LinearLayout>

   MainActivity.java

 

import android.app.Activity;
import android.content.res.Resources;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.text.Editable;
import android.text.InputType;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.view.Menu;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnTouchListener;
import android.widget.EditText;
import android.widget.Toast;

public class MainActivity extends Activity {

    private Drawable mIconSearchDefault; // 搜索文本框默认图标
    private Drawable mIconSearchClear; // 搜索文本框清除文本内容图标
    private EditText mSearchView = null;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        // 得到资源里面的图标文件
        final Resources res = getResources();
        // 默认的图标
        mIconSearchDefault = res.getDrawable(R.drawable.txt_search_default);
        // 清除图标
        mIconSearchClear = res.getDrawable(R.drawable.txt_search_clear);

        mSearchView = (EditText) findViewById(R.id.txtSearch);
        mSearchView.addTextChangedListener(tbxSearch_TextChanged);
        mSearchView.setOnTouchListener(txtSearch_OnTouch);

    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }

    /**
     * 判断输入框中是否有数据,然后显示相应的图标文件
     */
    private TextWatcher tbxSearch_TextChanged = new TextWatcher() {

        // 缓存上一次文本框内是否为空
        private boolean isnull = true;

        @Override
        public void afterTextChanged(Editable s) {
            if (TextUtils.isEmpty(s)) {
                if (!isnull) {
                    mSearchView.setCompoundDrawablesWithIntrinsicBounds(null,
                            null, mIconSearchDefault, null);
                    isnull = true;
                }
            } else {
                if (isnull) {
                    mSearchView.setCompoundDrawablesWithIntrinsicBounds(null,
                            null, mIconSearchClear, null);
                    isnull = false;
                }
            }
        }

        @Override
        public void beforeTextChanged(CharSequence s, int start, int count,
                int after) {
        }

        /**
         * 随着文本框内容改变动态改变列表内容
         */
        @Override
        public void onTextChanged(CharSequence s, int start, int before,
                int count) {

        }
    };

    
    // 当清除图标被点击的时候的处理事件
    private OnTouchListener txtSearch_OnTouch = new OnTouchListener() {
        @Override
        public boolean onTouch(View v, MotionEvent event) {
            switch (event.getAction()) {
            case MotionEvent.ACTION_UP:
                int curX = (int) event.getX();
                if (curX > v.getWidth() - 38
                        && !TextUtils.isEmpty(mSearchView.getText())) {
                    mSearchView.setText("");
                    int cacheInputType = mSearchView.getInputType();// backup
                                                                    // the input
                                                                    // type
                    mSearchView.setInputType(InputType.TYPE_NULL);// disable
                                                                    // soft
                                                                    // input
                    mSearchView.onTouchEvent(event);// call native handler
                    mSearchView.setInputType(cacheInputType);// restore input
                    Toast toast = Toast.makeText(MainActivity.this, "你好啊", Toast.LENGTH_SHORT);
                    toast.show();
                    
                                                                // type
                    return true;// consume touch even

                }
                break;
            }
            return false;
        }
    };

}

3. 使用的图片

  

4.说明 

   在农民伯伯的博客中看到,因为找不到网址 ,特此说明一下

 

posted @ 2014-09-19 08:33  落寞回头不如华丽转身  阅读(1342)  评论(0编辑  收藏  举报