android自定义输入框
来源:互联网 发布:linux安装两个tomcat 编辑:程序博客网 时间:2024/04/29 23:32
效果如图中蓝框所框
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 danielinbitiisReadOnly = 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" />1 0
- android自定义输入框
- android 间断输入框/自定义输入框
- Android 自定义手机输入框
- Android自定义密码输入框
- Android 自定义车牌输入框
- android自定义出生日期输入框
- Android:自定义输入框光标颜色
- Android 自定义EditText输入框 带清空按钮
- [Android]自定义带删除输入框
- Android自定义支付密码输入框
- 自定义android EditText输入框光标样式
- Android 自定义EditText输入框 带清空按钮
- Android自定义价格输入框EditText
- Android自定义View之密码输入框
- Android自定义数字验证码输入框
- Android-自定义控件之IP输入框
- 自定义Android Dialog EditText 密码输入框
- android自定义普通对话框,输入框对话框
- JAX-RS入门 二 :运行
- linux中断之三(s5pv210)
- 并查集-杭电1232-畅通工程-难度1
- JAX-RS入门 三 :细节
- HiWords and LoWords from Long Values
- android自定义输入框
- Android源码分析-消息队列和Looper
- JAX-RS入门 四: 注入
- android自定义标题栏
- 一个打开文件对话框的小技巧
- Java IO流包结构总结
- JAX-RS入门 五: 自动类型转换
- POJ 1789最小生成树(kruscal算法)
- JAX-RS入门 六: 数据处理(1)