效果如图中蓝框所框
import android.content.Context;
import android.content.res.TypedArray;
import android.text.InputType;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.LinearLayout.LayoutParams;
import android.widget.TextView;
public class ImageTextView extends LinearLayout {
private int width = 0;
private String textLabel = "";
private String textHintLabel = "";
private int image_id = 0;
private String edttext_inputtype = "";
private int label_width = 0;
private EditText editText = null;
private boolean isReadOnly = true;
private String defaultText = "";
private TextView txtView = null;
private ImageView img = null;
public ImageTextView(Context context){
super(context);
this.init(context);
}
public ImageTextView(Context context, AttributeSet attrs){
super(context, attrs);
TypedArray typedArray=context.obtainStyledAttributes(attrs, R.styleable.SelfImageTextAttr);
textLabel = typedArray.getString(R.styleable.SelfImageTextAttr_text_label);
textHintLabel = typedArray.getString(R.styleable.SelfImageTextAttr_text_hint_label);
image_id = typedArray.getResourceId(R.styleable.SelfImageTextAttr_image_id, 0);
edttext_inputtype = typedArray.getString(R.styleable.SelfImageTextAttr_edttext_inputtype);
label_width = Math.round(typedArray.getDimension(R.styleable.SelfImageTextAttr_label_width, 0));
defaultText = typedArray.getString(R.styleable.SelfImageTextAttr_default_text);
String txtReadOnly = typedArray.getString(R.styleable.SelfImageTextAttr_text_readonly);
if(txtReadOnly!=null&&txtReadOnly.equals("true")){//comment by danielinbiti
isReadOnly = false;
}
this.init(context);
}
public ImageTextView(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
this.init(context);
// TODO Auto-generated constructor stub
}
public void setTextLabel(String textLabel) {
this.textLabel = textLabel;
txtView.setText(textLabel);
}
public void setTextHintLabel(String textHintLabel) {
this.textHintLabel = textHintLabel;
editText.setHint(textHintLabel);
}
public void setImage_id(int image_id) {
this.image_id = image_id;
img.setBackgroundResource(image_id);
}
public void setEdttext_inputtype(String edttext_inputtype) {
this.edttext_inputtype = edttext_inputtype;
int inputType = getType();
if(inputType!=-999){
editText.setInputType(inputType);
}
}
public void setLabel_width(int label_width) {
this.label_width = label_width;
txtView.setWidth(label_width);
}
public void setReadOnly(boolean isReadOnly) {
this.isReadOnly = !isReadOnly;
editText.setEnabled(!isReadOnly);
}
public void setDefaultText(String defaultText) {
this.defaultText = defaultText;
editText.setText(defaultText);
}
private int getType(){
if(edttext_inputtype!=null&&edttext_inputtype.trim().length()>0){
if(edttext_inputtype.equals("textPassword")){
return InputType.TYPE_CLASS_TEXT | InputType.TYPE_TEXT_VARIATION_PASSWORD;
}else if(edttext_inputtype.equals("textPhonetic")){
return InputType.TYPE_CLASS_PHONE | InputType.TYPE_TEXT_VARIATION_PHONETIC;
}else if(edttext_inputtype.equals("textEmailAddress")){
return InputType.TYPE_CLASS_TEXT | InputType.TYPE_TEXT_VARIATION_EMAIL_ADDRESS;
}
}
//return InputType.TYPE_CLASS_TEXT | InputType.TYPE_NULL;
return -999;
}
private void init(Context context){
LayoutInflater inflater = LayoutInflater.from(context);
View view = inflater.inflate(R.layout.image_text, null);
//this.setOrientation(LinearLayout.HORIZONTAL);
//this.setLayoutParams(new LinearLayout.LayoutParams(width,LayoutParams.WRAP_CONTENT));
txtView = (TextView)view.findViewById(R.id.imgLabel);
editText = (EditText)view.findViewById(R.id.imgEdtText);
img = (ImageView)view.findViewById(R.id.img);
if(image_id!=0){
img.setBackgroundResource(image_id);
}
txtView.setText(textLabel);
if(label_width!=0){
txtView.setWidth(label_width);
}
editText.setHint(textHintLabel);
int inputType = getType();
if(inputType!=-999){
editText.setInputType(inputType);
}
if(defaultText!=null&&defaultText.trim().length()>0){
editText.setText(defaultText);
}
editText.setEnabled(isReadOnly);
//editText.setEnabled(enabled);
this.addView(view,new LinearLayout.LayoutParams(LayoutParams.MATCH_PARENT,LayoutParams.MATCH_PARENT));
}
public String getText(){
return editText.getText().toString();
}
public void setText(String txt){
this.editText.setText(txt);
}
public EditText getEditText(){
return editText;
}
}
在layout文件中的使用方式
<srit.collection.widget.ImageTextView
android:id="@+id/username_edit"
android:layout_width="fill_parent"
android:layout_height="43dp"
myapp:text_label = "用户名"
myapp:label_width="40dp"
myapp:text_hint_label = "输入用户名"
android:layout_alignParentTop="true"
myapp:image_id ="@drawable/nor_4"
android:layout_marginTop="20dp"
/>