onFocusChange函数不适用于自定义editText

时间:2013-06-22 07:39:49

标签: android

我已经自定义EditText,现在当我尝试在此onFocusChanged()中使用View函数时,这不起作用。为什么会这样?

我自定义EditText的代码是:

<EditText
    android:id="@+id/clearable_edit"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:paddingRight="35dip"
    android:singleLine="true" />

<Button
    android:id="@+id/clearable_button_clear"
    android:layout_width="30dip"
    android:layout_height="30dip"
    android:layout_alignParentRight="true"
    android:layout_centerInParent="true"
    android:layout_marginRight="5dip"
    android:gravity="center"
    android:text="@string/cross"
    android:textSize="12sp" />

和java是:

    public class ClearableEditText extends RelativeLayout implements
    OnFocusChangeListener {

LayoutInflater inflater = null;
EditText edit_text;
Button btn_clear;

private ClearableEditText editText;

public ClearableEditText(Context context, final ClearableEditText editText) {
    super(context);
    Log.e("===============", "entered");
    this.editText = editText;
}

@Override
public void onFocusChange(View v, boolean hasFocus) {
    // TODO Auto-generated method stub
    Log.e("===============", "done");
    Toast.makeText(getContext(), "dlknsajlkdsj", Toast.LENGTH_SHORT).show();
}

public ClearableEditText(Context context, AttributeSet attrs, int defStyle) {
    super(context, attrs, defStyle);
    if (!isInEditMode())
        initViews();
}

public ClearableEditText(Context context, AttributeSet attrs) {
    super(context, attrs);
    // TODO Auto-generated constructor stub
    if (!isInEditMode())
        initViews();

}

public ClearableEditText(Context context) {
    super(context);
    // TODO Auto-generated constructor stub
    if (!isInEditMode())
        initViews();
}

void initViews() {
    // TODO Auto-generated method stub
    inflater = (LayoutInflater) getContext().getSystemService(
            Context.LAYOUT_INFLATER_SERVICE);
    inflater.inflate(R.layout.clearable_edit_text, this, true);
    edit_text = (EditText) findViewById(R.id.clearable_edit);
    btn_clear = (Button) findViewById(R.id.clearable_button_clear);
    btn_clear.setVisibility(RelativeLayout.INVISIBLE);
    clearText();
    showHideClearButton();
}

void setValues(String hint, int type) {
    edit_text.setHint(hint);
    edit_text.setInputType(type);
}

void setValues(String hint) {
    edit_text.setHint(hint);
}

void setValues(String hint, int type, int lines) {
    edit_text.setHint(hint);
    edit_text.setInputType(type);
    edit_text.setMaxLines(lines);
}

void clearText() {
    btn_clear.setOnClickListener(new OnClickListener() {
        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub
            edit_text.setText("");
        }
    });
}

void showHideClearButton() {
    edit_text.addTextChangedListener(new TextWatcher() {

        @Override
        public void onTextChanged(CharSequence s, int start, int before,
                int count) {
            // TODO Auto-generated method stub
            if (s.length() > 0)
                btn_clear.setVisibility(RelativeLayout.VISIBLE);
            else
                btn_clear.setVisibility(RelativeLayout.INVISIBLE);
        }

        @Override
        public void beforeTextChanged(CharSequence s, int start, int count,
                int after) {
            // TODO Auto-generated method stub

        }

        @Override
        public void afterTextChanged(Editable s) {
            // TODO Auto-generated method stub

        }
    });
}

public Editable getText() {
    Editable text = edit_text.getText();
    return text;
}

public void setText(CharSequence text) {
    edit_text.setText(text);
}

}

和我的onFocusChange功能是:

   email.setOnFocusChangeListener(new ClearableEditText(
            getApplicationContext(), email));

1 个答案:

答案 0 :(得分:0)

完成后,只需在我自定义的Edittext Activity中覆盖focusChangeListener