Android -- 編輯框更改樣式

來源:互聯網
上載者:User

標籤:android   style   blog   http   color   io   os   使用   java   

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.說明 

   在農民伯伯的部落格中看到,因為找不到網址 ,特此說明一下

 

Android -- 編輯框更改樣式

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.