android editText

一些属性

http://www.cnblogs.com/tianguook/archive/2012/03/02/2376465.html


输入框变为搜索图标


  
  
  1. EditText editText = new EditText(this);
  2. editText.setImeOptions(EditorInfo.IME_ACTION_SEARCH);
  3. editText.setInputType(EditorInfo.TYPE_CLASS_TEXT);

xml配置文件:
EditText属性设置:

   
 
 
  1. android:imeOptions="actionSearch"
  2. android:inputType="text"

//设置action响应

    editText.setOnEditorActionListener(new TextView.OnEditorActionListener() {

            @Override
            public boolean onEditorAction(TextView v, int actionId,
                    KeyEvent event) {
                if (actionId == EditorInfo.IME_ACTION_SEARCH) {
                    // 隐藏软键盘
                    InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
                    imm.hideSoftInputFromWindow(editText.getWindowToken(), 0);
                    System.out.println("login....");
                    return true;
                }

                return false;
            }
        });


监听输入:


/**
     * 动态搜索
     */
    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  type
                    return true;// consume touch even
                }
                break;
            }
            return false;
        }
    };


private Drawable mIconSearchDefault; // 搜索文本框默认图标
    private Drawable mIconSearchClear; // 搜索文本框清除文本内容图标

    @Override
    protected void 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. 维持清空操作后软键盘状态。


EditText的background属性设置为@null就搞定了:android:background="@null"


改变边框颜色背景;

http://blog.csdn.net/djcken/article/details/7801966



public final void setHint(CharSequence hint) {

    mHint = TextUtils.stringOrSpannedString(hint);
 
    if (mLayout != null) {
        checkForRelayout();
    }
 
    if (mText.length() == 0) {
        invalidate();
    }
 
    // Invalidate display list if hint is currently used
    if (mEditor != null && mText.length() == 0 && mHint != null) {
        mEditor.invalidateTextDisplayList();
    }
}

在方法的一开始就是对hint文本的转换.由于hint是CharSequence类型的, 说明有希望可以增加一些自定义属性, 我们再看TextUtils.stringOrSpannedString这个方法:

public static CharSequence stringOrSpannedString(CharSequence source) {
    if (source == null)
        return null;
    if (source instanceof SpannedString)
        return source;
    if (source instanceof Spanned)
        return new SpannedString(source);
 
    return source.toString();
}

那么问题来了,我们只要传入的hint是SpannedString或者Spanned类型,就可以保持文本的自定义属性了吗? 答案是肯定的! 直接上代码:

EditText editText = (EditText) rootView.findViewById(R.id.et);
 
// 新建一个可以添加属性的文本对象
SpannableString ss = new SpannableString("喝酒就要喝一斤!");
 
// 新建一个属性对象,设置文字的大小
AbsoluteSizeSpan ass = new AbsoluteSizeSpan(8,true);
 
// 附加属性到文本
ss.setSpan(ass, 0, ss.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
 
// 设置hint
editText.setHint(new SpannedString(ss)); // 一定要进行转换,否则属性会消失
注意最后一步,一定要进行转换, 类型不对会被转换为String对象,这样自定义的额属性就会丢失



   <RelativeLayout
                android:layout_width="0dp"
                android:layout_height="38dp"
                android:layout_weight="1"
                android:background="@drawable/shape_white"
                android:gravity="center_vertical" >

                <ImageButton
                    android:id="@+id/ib1"
                    android:layout_width="20dp"
                    android:layout_height="20dp"
                    android:layout_alignParentLeft="true"
                    android:layout_centerVertical="true"
                    android:layout_marginRight="3dp"
                    android:background="@android:color/transparent"
                    android:paddingBottom="9dp"
                    android:paddingTop="9dp"
                    android:src="@drawable/location_oncall" />

                <EditText
                    android:id="@+id/et_search"
                    android:layout_width="match_parent"
                    android:layout_height="38dp"
                    android:layout_toRightOf="@+id/ib1"
                    android:background="@color/transparent"
                    android:gravity="center_vertical"
                    android:hint="搜索地址"
                    android:paddingLeft="5dp"
                    android:focusable="true"
                    android:focusableInTouchMode="true"
                    android:paddingRight="25dp"
                    android:singleLine="true"
                    android:textColor="@color/mdj_txt_black"
                    android:textSize="@dimen/mdj_title_three" />

                <ImageButton
                    android:id="@+id/search_clear"
                    android:layout_width="20dp"
                    android:layout_height="20dp"
                    android:layout_alignParentRight="true"
                    android:layout_centerVertical="true"
                    android:layout_marginRight="3dp"
                    android:background="@android:color/transparent"
                    android:paddingBottom="9dp"
                    android:paddingTop="9dp"
                    android:visibility="gone"
                    android:src="@drawable/appraise_x" />
            </RelativeLayout>

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值