文字框實現搜尋和清空效果
前言
本文實現的效果:文字框輸入為空時顯示輸入的圖示;不為空時顯示清空的圖示,此時點選清空圖示能清空文字框內輸入文字。
正文
一、實現效果
二、實現程式碼
監聽輸入
/*** 動態搜尋
*/private TextWatcher tbxSearch_TextChanged =new TextWatcher() {
//快取上一次文字框內是否為空privateboolean isnull =true;
@Override
publicvoid afterTextChanged(Editable s) {
if (!isnull) {
mSearchView.setCompoundDrawablesWithIntrinsicBounds(null,
null, mIconSearchDefault, null);
isnull =true;
}
} else {
if (isnull) {
mSearchView.setCompoundDrawablesWithIntrinsicBounds(
null, mIconSearchClear, null);
isnull =false;
}
}
}
@Override
publicvoid beforeTextChanged(CharSequence s, int start, int count,
int after) {
}
/**
* 隨著文字框內容改變動態改變列表內容
@Override
publicvoid onTextChanged(CharSequence s, int start, int before,
int count) {
}
};
觸控事件
private OnTouchListener txtSearch_OnTouch =new OnTouchListener() {@Override
publicboolean 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 typereturntrue;// consume touch even }
break;
}
returnfalse;
}
};
繫結事件
private Drawable mIconSearchDefault; // 搜尋文字框預設圖示private Drawable mIconSearchClear; // 搜尋文字框清除文字內容圖示@Override
protectedvoid onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.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);
}
程式碼說明:
1. 為輸入框繫結觸控事件(模擬點選事件捕捉)。通過監聽點選區域判斷是否點選清空圖片,如果在該區域並且文字框不為空,則清空文字框。
2. 為輸入框繫結文字改變事件監聽,根據內容改變動態設定圖示顯示。
3. 維持清空操作後軟鍵盤狀態。
三、參考
四、小圖示下載
(右鍵另存為即可。)
相關推薦
android_文字框實現搜尋和清空效果
前言 本文實現的效果:文字框輸入為空時顯示輸入的圖示;不為空時顯示清空的圖示,此時點選清空圖示能清空文字框內輸入文字。 正文 一、實現效果 二、實現程式碼 監聽輸入 /** * 動態搜尋
Android EditText 文字框實現搜尋和清空效果
前言 本文實現的效果:文字框輸入為空時顯示輸入的圖示;不為空時顯示清空的圖示,此時點選清空圖示能清空文字框內輸入文字。 宣告 歡迎轉載,但請保留文章原始出處:) 部落格園:http://www.cnblogs.com 農民伯伯: http://over140.cnblogs.
文字框實現搜尋和清空效果
前言 本文實現的效果:文字框輸入為空時顯示輸入的圖示;不為空時顯示清空的圖示,此時點選清空圖示能清空文字框內輸入文字。 正文 一、實現效果 二、實現程式碼 監聽輸入 /** * 動態搜尋 */private TextWatcher t
ueditor文字框初始化如何顯示預設內容,以及文字框獲取焦點後清空預設內容
<div> <form id="myForm" method="post" action="<?=Url::toRoute(['save'])?>">
android自定義文字框,後面帶清空按鈕
class MyEditText extends EditText { private final String TAG = "MyEditText"; private Drawable dRight; private Rect rBounds; // 構造器 public MyEditText(Conte
jQuery 複選框全選和清空
html程式碼: <div class="checkbox-custom" style="height:32px;line-height:32px;"> <input id="Appleid" name="Apple" class="checkboxstyle" type="
VB.Net文字框提示及一次性清空
【前言】 第二次基礎機房收費系統,在很多系統優化方面有了更多的認識,對於窗體中文字框內容是否為空的提示以及一次性清空其內容,這些在第一次的機房收費系統中進行了嘗試,在這次的機房收費系統個人重構的過程中,再次產生了這一方面的需求,如何減少程式碼的重複率,於是對該方面
EditText文字框清空效果
/** * 動態搜尋 */ private TextWatcher tbxSearch_TextChanged = new TextWatcher() { // 快取上一次文字框內是否為空 private boolean isnull = true; @Overr
layui文字框實現選擇起始時間和結束時間
首先要呼叫layui時間外掛必須下載layui.js檔案,這個可以在官網去下載的(https://www.layui.com/),我這個功能是可以選擇當前時間的前一天,或者後一小時的時間,這個根據具體需求可以修改的: 如下圖所示: html程式碼: <!DOCTY
移動端實現彈出框漸顯和漸隱效果
在移動端想實現一個彈出框漸漸出現和消失的效果。由於用的是vue.js,所以寫法有些獨特,用變數控制是否顯示類名。但是核心解決方法就是CSS3的animation屬性應用還有CSS中的z-index應用,必須初始化定義讓彈出框在最底部。還有就是opacity屬性的應用,這樣才有漸健彈出和漸漸消失的效果。給彈出框
中文輸入法觸發input事件 js繫結事件:addEventListener和on的區別 select下拉框用empty()方法 清空option
中文輸入法觸發input事件 compositionstart 當瀏覽器有非直接的文字輸入時, compositionstart事件觸發. compositionend 當瀏覽器是直接的文字輸入時, compositionend事件觸發 解決方法: 這裡還有一個問題: 在有些瀏
jquery實現文字框的禁用和啟用
1,開啟disabled,設定input不可編輯 程式碼示例: $("#id").attr("disabled","disabled"); 2,關閉disabled 程式碼示例: $("#id").removeAttr("disabled"); 普通js中是這樣寫的
RDLC 表格和文字框實現多資料,單備註(簡單)
RDLC 中表格和文字框實現單備註,多資料的表現格式 將文字框放在表的旁邊,但是這個文字框只是對錶資料的一個備註 由於備註內容部分不能放在表的重複單元格中(因為這樣會讓備註內容對每一條資料都重複一次),故而只能用文字框,而文字框設定邊框後,又不能達到對所有重複資料進行單備註
如何使用Cookie來簡單實現商城購物車的新增和清空
這個程式主要是有三個java程式和1個html來完成的(後面還有一個web.xml檔案),每個檔案中都有詳細的註解,所以就不再詳細介紹具體的步驟了,接下來看一看我的程式碼吧! package com.nick.web; import java.io.IOException
ajax和清空div下的輸入框
var params=new Object(); var channel = $('#modal-manage input:radio:checked').val(); params['mfid']= userCode;
轉 navicat for mysql 的 刪減表 和清空表
epo popu rep jce 包含 mod pla aid 5% 刪除操作在執行的語句就是delete語句 也就是可以按照後邊的where條件進行刪除指定行,當然也可以不加where條件,把所有的數據刪除 清空所執行的語句是truncate,也就是把這個表裏所有
android刪除表和清空表
pla tle tab ble ews from ron ocl sqlite 刪除某一表: [java] view plain copy //刪除某一個表 public void dropTable(SQLiteDatabase db){ db.exe
Python3 tkinter基礎 Entry insert delete 點擊按鈕 向輸入框賦值 或 清空
pytho 源碼 stringvar 4.5 content htm 文檔 rate lock ? python : 3.7.0 OS : Ubuntu 18.04.1 LTS
判斷input文字框獲取焦點和失去焦點
<!doctype html> <html lang="en"> <head> <meta charset="UTF-8"> <title>判斷input文字框
Bootstrap Ace模板實現選單和Tab頁效果
Bootstrap Ace模板實現選單和Tab頁效果 這篇文章主要為大家分享了基於Bootstrap Ace模板實現選單和Tab頁效果,感興趣的小夥伴們可以參考一下 本文分享了專案中使用Ace模板的選單樣式和基於iframe的Tab頁效果。 一、效果展示 1、折騰了好久,