Android文本框清除图标的处理

/**
     * 输入框清除图标的处理
     * @param text
     * @param icon
     */
    public static void inputClear(final EditText text, final ImageView icon){
		
		text.setOnFocusChangeListener(new OnFocusChangeListener(){

			@Override
			public void onFocusChange(View v, boolean hasFocus) {
				String str = text.getText().toString();
				icon.setVisibility(hasFocus && str.length() > 0 ? View.VISIBLE : View.GONE);
			}
		});
		
		text.addTextChangedListener(new TextWatcher(){

			@Override
			public void afterTextChanged(Editable s) {
				String str = text.getText().toString();
				icon.setVisibility(str.length() > 0 ? View.VISIBLE : View.GONE);
			}

			@Override
			public void beforeTextChanged(CharSequence s, int start, int count,
					int after) {
				
			}

			@Override
			public void onTextChanged(CharSequence s, int start, int before,
					int count) {
				
			}
		});
		
		icon.setOnClickListener(new OnClickListener(){

			@Override
			public void onClick(View v) {
				text.setText("");
			}
		});
	}

猜你喜欢

转载自chenzheng8975.iteye.com/blog/2068063
今日推荐