標籤: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 -- 編輯框更改樣式