android自定义输入框

本文介绍了如何在Android中创建自定义输入框,特别是在登录场景下,为用户名和密码输入框提供个性化设计的方法,包括布局文件中的配置和实现细节。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

效果如图中蓝框所框


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" 	       
/>

评论 5
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值