Android Launcher T9搜索APP实现 

https://github.com/xufeifandj/TosLauncherT9Search


1、自定义T9网格 ViewGroup实现虚拟键盘

package t9.launcher.tos.com.toslaunchert9search;import android.content.Context;import android.text.Editable;import android.text.TextUtils;import android.text.TextWatcher;import android.util.AttributeSet;import android.view.View;import android.view.ViewGroup;import android.widget.ImageView;import android.widget.TextView;/** * T9拨号盘 3*4 T9拨号盘 * Created by ferris on 2016/7/15. */public class AppSearchT9View extends ViewGroup implements        View.OnClickListener {    private int col=3;    private int row=4;    private TextView mT9InputEt;    private ImageView mDialDeleteBtn;    public void setTextInput(TextView mT9InputEt){        this.mT9InputEt=mT9InputEt;        mT9InputEt.addTextChangedListener(new TextWatcher() {            @Override            public void onTextChanged(CharSequence s, int start, int before,                                      int count) {                // TODO Auto-generated method stub            }            @Override            public void beforeTextChanged(CharSequence s, int start, int count,                                          int after) {                // TODO Auto-generated method stub            }            @Override            public void afterTextChanged(Editable s) {                if (null != mOnT9TelephoneDialpadView) {                    String inputStr=s.toString();                    mOnT9TelephoneDialpadView.onDialInputTextChanged(inputStr);                }            }        });    }    private OnT9TelephoneDialpadView mOnT9TelephoneDialpadView = null;    public OnT9TelephoneDialpadView getOnT9TelephoneDialpadView() {        return mOnT9TelephoneDialpadView;    }    public void setOnT9TelephoneDialpadView(            OnT9TelephoneDialpadView onT9TelephoneDialpadView) {        mOnT9TelephoneDialpadView = onT9TelephoneDialpadView;    }    public AppSearchT9View(Context context) {        super(context);        init(context,null);    }    public AppSearchT9View(Context context, AttributeSet attrs) {        super(context, attrs);        init(context,attrs);    }    public  void init(Context context,AttributeSet attrs){        setPadding(25,25,25,25);    }    @Override    protected void onFinishInflate() {        super.onFinishInflate();        for (int i = 0; i < getChildCount(); i++) {            View v =getChildAt(i);            if(v.getId()==R.id.dial_delete_btn){                mDialDeleteBtn = (ImageView) v;            }            v.setOnClickListener(this);        }    }    /**     * Interface definition for a callback to be invoked when a     * T9TelephoneDialpadView is operated.     */    public interface OnT9TelephoneDialpadView {        void onAddDialCharacter(String addCharacter);        void onDeleteDialCharacter(String deleteCharacter);        void onDialInputTextChanged(String curCharacter);        void onHideT9TelephoneDialpadView();    }    @Override    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {        //measure每个图标的大小        int widthMode = View.MeasureSpec.getMode(widthMeasureSpec);        int heightMode = View.MeasureSpec.getMode(heightMeasureSpec);        int width = View.MeasureSpec.getSize(widthMeasureSpec);        int height = View.MeasureSpec.getSize(heightMeasureSpec);        int paddingLeft=getPaddingLeft();        int paddingRight=getPaddingRight();        int padingTop=getPaddingTop();        int padingBottom=getPaddingBottom();        int gridWidth=(width-paddingLeft-paddingRight)/col;        int gridHeight=(height-padingTop-padingBottom)/row;        int childCount = getChildCount();        for (int i = 0; i < childCount; i++) {            View v = getChildAt(i);            if (v != null) {                int childWidthSpec = View.MeasureSpec.makeMeasureSpec(gridWidth, widthMode);                int childHeightSpec = View.MeasureSpec.makeMeasureSpec(gridHeight, heightMode);                v.measure(childWidthSpec, childHeightSpec);            }        }        setMeasuredDimension(width, height);    }    @Override    protected void onLayout(boolean changed, int l, int t, int r, int b) {        // layout child        int paddingLeft=getPaddingLeft();        int paddingRight=getPaddingRight();        int padingTop=getPaddingTop();        int padingBottom=getPaddingBottom();        int releaseWidth=(getMeasuredWidth()-paddingLeft-paddingRight)/col;        int releaseHeiht=(getMeasuredHeight()-padingTop-padingBottom)/row;        int iconOffsetX=paddingLeft;        int iconOffsetY=padingTop;        int left;        int top;        int childCount = getChildCount();        for (int i = 0; i < childCount; i++) {            View v = getChildAt(i);            //行            int currentRow=i/col;            //列  row=1 col=4            int currentCol=i%col;            left=currentCol*releaseWidth+iconOffsetX;            top=currentRow*releaseHeiht+iconOffsetY;            if (v != null) {                v.layout(left, top, left+releaseWidth, top+releaseHeiht);            }        }    }    @Override    public void onClick(View v) {        switch (v.getId()) {            case R.id.dial_delete_btn:                deleteSingleDialCharacter();                break;            case R.id.dialNum0:            case R.id.dialNum1:            case R.id.dialNum2:            case R.id.dialNum3:            case R.id.dialNum4:            case R.id.dialNum5:            case R.id.dialNum6:            case R.id.dialNum7:            case R.id.dialNum8:            case R.id.dialNum9:                addSingleDialCharacter(v.getTag().toString());                break;            default:                break;        }    }    public void deleteSingleDialCharacter() {        String curInputStr = mT9InputEt.getText().toString();        if (curInputStr.length() > 0) {            String deleteCharacter = curInputStr.substring(                    curInputStr.length() - 1, curInputStr.length());            if (null != mOnT9TelephoneDialpadView) {                mOnT9TelephoneDialpadView                        .onDeleteDialCharacter(deleteCharacter);            }            String newCurInputStr=curInputStr.substring(0,curInputStr.length() - 1);            mT9InputEt.setText(newCurInputStr);            //mT9InputEt.setSelection(newCurInputStr.length());//            if(TextUtils.isEmpty(newCurInputStr)){//                ViewUtil.hideView(mDialDeleteBtn);//            }else{//                ViewUtil.showView(mDialDeleteBtn);//            }        }    }    private void addSingleDialCharacter(String addCharacter) {        String preInputStr = mT9InputEt.getText().toString();        if (!TextUtils.isEmpty(addCharacter)) {            mT9InputEt.setText(preInputStr + addCharacter);        //    mT9InputEt.setSelection(mT9InputEt.getText().length());            if (null != mOnT9TelephoneDialpadView) {                mOnT9TelephoneDialpadView.onAddDialCharacter(addCharacter);            }         //   ViewUtil.showView(mDialDeleteBtn);        }    }}


2、自定义带底线TextView


package t9.launcher.tos.com.toslaunchert9search;import android.content.Context;import android.graphics.Canvas;import android.text.TextUtils;import android.util.AttributeSet;import android.widget.TextView;/** * 带下划线的textView * Created by ferris on 2016/7/15. */public class LineTextView extends TextView {//    Paint mPaint=new Paint();    public LineTextView(Context context) {        super(context);        init();    }    public LineTextView(Context context, AttributeSet attrs) {        super(context, attrs);        init();    }    public LineTextView(Context context, AttributeSet attrs, int defStyleAttr) {        super(context, attrs, defStyleAttr);        init();    }    public void init(){//        mPaint.setStyle(Paint.Style.STROKE);//设置非填充//        mPaint.setStrokeWidth(1);//笔宽5像素//        mPaint.setColor(Color.BLACK);//设置为红笔//        mPaint.setAntiAlias(true);//锯齿不显示    }    @Override    protected void onDraw(Canvas canvas) {        if(TextUtils.isEmpty(getText())){            super.onDraw(canvas);            return;        }        canvas.save();        super.onDraw(canvas);        canvas.drawLine(0,getMeasuredHeight(),getMeasuredWidth(),getMeasuredHeight(),getPaint());        canvas.restore();    }}



更多相关文章

  1. Android实现动态显示或隐藏密码输入框的内容
  2. Android学习记录使用Gallery实现炫丽的拖动效果
  3. android 状态栏占位_Android(安卓)Studio 全屏沉浸式透明状态栏
  4. Android(安卓)实现滑动的六种方式
  5. Android(安卓)来电翻转静音实现源码
  6. Android中使用HttpURLConnection和HttpClient实现GET和POST请求
  7. Android(安卓)图文混排spannableStringBuilder简单使用
  8. Android调用C++实现共享内存(Native层)
  9. 浅谈Java中Collections.sort对List排序的两种方法

随机推荐

  1. SQL语句的并集UNION 交集JOIN(内连接,外连
  2. SQL Server中的SQL语句优化与效率问题
  3. sql 聚集索引和非聚集索引(详细整理)
  4. SQL SERVER数据库重建索引的方法
  5. SQL Server 压缩日志与减少SQL Server 文
  6. 针对Sqlserver大数据量插入速度慢或丢失
  7. ms sql server中实现的unix时间戳函数(含
  8. sql 判断函数、存储过程是否存在的代码整
  9. SQL SERVER 2000通讯管道后复用劫持
  10. SQL Server 2000安全配置详解