Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲
 Android教程網 >> Android技術 >> 關於Android編程 >> android自定義輸入框

android自定義輸入框

編輯:關於Android編程

效果如圖中藍框所框

\


<喎?/kf/ware/vc/" target="_blank" class="keylink">vcD4KPHA+PC9wPgo8cHJlIGNsYXNzPQ=="brush:java;">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文件中的使用方式


  1. 上一頁:
  2. 下一頁:
熱門文章
閱讀排行版
Copyright © Android教程網 All Rights Reserved