自定义EditText,带有搜索图片的EditText
来源:互联网 发布:卫生部 卫计委 知乎 编辑:程序博客网 时间:2024/05/21 10:42
效果如图,搜索框带有搜索的图片。
代码如下
/** * Created by 海峰 on 2016/10/18. */import android.content.Context;import android.content.res.TypedArray;import android.graphics.Canvas;import android.graphics.Paint;import android.graphics.drawable.Drawable;import android.support.v4.content.ContextCompat;import android.util.AttributeSet;import android.widget.EditText;import com.hoge.app4hrzki6lz7.R;public class SeachView extends EditText { private float searchSize = 0; private float textSize = 0; private int textColor = 0xFF000000; private Drawable mDrawable; private Paint paint; public SeachView(Context context, AttributeSet attrs) { super(context, attrs); InitResource(context, attrs); InitPaint(); } private void InitResource(Context context, AttributeSet attrs) { TypedArray mTypedArray = context.obtainStyledAttributes(attrs, R.styleable.searchedit); float density = context.getResources().getDisplayMetrics().density; searchSize = mTypedArray.getDimension(R.styleable.searchedit_imagewidth, 15 * density + 0.5F); textColor = mTypedArray.getColor(R.styleable.searchedit_textColor, 0xFF848484); textSize = mTypedArray.getDimension(R.styleable.searchedit_textSize, 14 * density + 0.5F); mTypedArray.recycle(); } private void InitPaint() { paint = new Paint(Paint.ANTI_ALIAS_FLAG); paint.setColor(textColor); paint.setTextSize(textSize); } @Override protected void onDraw(Canvas canvas) { super.onDraw(canvas); DrawSearchIcon(canvas); } // 在指定的位置画搜索图片,和文字。 private void DrawSearchIcon(Canvas canvas) { if (this.getText().toString().length() == 0) { float textWidth = paint.measureText("艺术/标题/新闻"); float textHeight = getFontLeading(paint); float dx = (getWidth() - searchSize - textWidth - 15) / 2; float dy = (getHeight() - searchSize) / 2; canvas.save(); canvas.translate(getScrollX() + dx, getScrollY() + dy); if (mDrawable != null) { mDrawable.draw(canvas); } canvas.drawText("艺术/标题/新闻", getScrollX() + searchSize + 15, getScrollY() + (getHeight() - (getHeight() - textHeight) / 2) - paint.getFontMetrics().bottom - dy, paint); canvas.restore(); } } //初始化搜索图片的大小 @Override protected void onAttachedToWindow() { super.onAttachedToWindow(); if (mDrawable == null) { try { mDrawable = ContextCompat.getDrawable(getContext(),R.mipmap.seach); mDrawable.setBounds(0, 0, (int) searchSize, (int) searchSize); } catch (Exception e) { } } } @Override protected void onDetachedFromWindow() { if (mDrawable != null) { mDrawable.setCallback(null); mDrawable = null; } super.onDetachedFromWindow(); } public float getFontLeading(Paint paint) { Paint.FontMetrics fm = paint.getFontMetrics(); return fm.bottom - fm.top; }}用法如下<com.wbkj.artmss.Weight.SeachView android:id="@+id/et_seach" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="1" android:background="@drawable/edit_common_yuanjiao" android:imeOptions="actionSearch" android:paddingBottom="5dp" android:paddingLeft="10dp" android:paddingTop="5dp" android:textColorHint="@color/hint" />然后在activity里设置输入框默认搜索功能seach.setImeOptions(EditorInfo.IME_ACTION_SEARCH); seach.setInputType(EditorInfo.TYPE_CLASS_TEXT); seach.setSingleLine(true); seach.setOnEditorActionListener(new TextView.OnEditorActionListener() { @Override public boolean onEditorAction(TextView v, int actionId, KeyEvent event) { if (actionId == EditorInfo.IME_ACTION_SEARCH) { if (!getSeachString().equals("")){ page=0; getResultData(getSeachString()); //调服务器 InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE); //隐藏键盘 imm.hideSoftInputFromWindow(v.getWindowToken(), 0); }else{ Toast.makeText(SearchActivity.this,"搜索内容不能为空",Toast.LENGTH_SHORT).show(); } } return false; } });
0 0
- 自定义EditText,带有搜索图片的EditText
- 自定义控件--带有删除按钮的EditText
- Android 自定义带有清除按钮的Edittext
- 自定义带有删除功能的EditText
- 自定义带有删除标签的edittext
- 自定义的带有删除功能EditText
- Android 实现自定义自带删除按钮的EditText(带有搜索接口)
- 带有EditText的AlertDialog
- 带有×的EditText
- 自定义搜索框EditText
- Android自定义View带有删除按钮的EditText
- Android 自定义带有EditText键盘遇到的坑
- Android自定义控件实现带有清除按钮的EditText
- 自定义带有删除功能的edittext实例,在登录,搜索等地方经常用到可以直接用
- 自定义带有删除功能的edittext实例,在登录,搜索等地方经常用到可以直接用
- 带有中间线条的EditText
- 带有一键清空功能的EditText
- 带有一键清空功能的EditText
- 理解MySQL——索引与优化
- error: 'XCTest.framework’ file not found
- 【NOIP2016普及组】 first 水资源 pencil
- wemall app商城源码中android按钮的三种响应事件
- MYSQL——为现有字段添加自增属性
- 自定义EditText,带有搜索图片的EditText
- SQL Case when 的使用方法
- redis3.0.0 集群安装详细步骤
- canvas学习笔记
- 1049. 数列的片段和(20)/yhf/2016.11.22
- 日志收集之rsyslog to kafka
- Halcon学习(二十四)总结(一)
- .NET Framework-C# 7.0新特性
- IDEA Properties代码高亮问题