Android -- 编辑框更改样式
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了Android -- 编辑框更改样式,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含3266字,纯文字阅读大概需要5分钟。
内容图文
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 protectedvoid 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 publicboolean onCreateOptionsMenu(Menu menu) { // Inflate the menu; this adds items to the action bar if it is present. getMenuInflater().inflate(R.menu.main, menu); returntrue; } /** * 判断输入框中是否有数据,然后显示相应的图标文件 */private TextWatcher tbxSearch_TextChanged = new TextWatcher() { // 缓存上一次文本框内是否为空privateboolean isnull = true; @Override publicvoid 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 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 Toast toast = Toast.makeText(MainActivity.this, "你好啊", Toast.LENGTH_SHORT); toast.show(); // typereturntrue;// consume touch even } break; } returnfalse; } }; }
3. 使用的图片
4.说明
在农民伯伯的博客中看到,因为找不到网址 ,特此说明一下
原文:http://www.cnblogs.com/SM-t/p/3980611.html
内容总结
以上是互联网集市为您收集整理的Android -- 编辑框更改样式全部内容,希望文章能够帮你解决Android -- 编辑框更改样式所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。