0

我已经定制了一个EditText,现在当我尝试onFocusChanged()在这个Viewt 中使用函数时不起作用。为什么会这样?

我定制的代码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));
4

1 回答 1

0

完成后,只需在我自定义的 Edittext Activity 中覆盖焦点更改列表

于 2013-08-16T17:07:02.913 回答