2013-06-22 27 views
0

我已经定制了EditText,现在当我尝试使用onFocusChanged()函数时,此View不起作用。为什么会发生?onFocusChange函数不能用于自定义editText

代码为我定制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)); 
+0

你说的定制EDITTEXT ,,还有哪里是定制的一部分..你已经使用原生EDITTEXT .. – CRUSADER

+0

做ü希望看到完整的java文件?等待我上传 –

回答

0

做到这一点,只是要覆盖thae focusChangeListner在我定制EDITTEXT活动