- package com.ray.widget;
- import android.content.Context;
- import android.text.method.PasswordTransformationMethod;
- import android.util.AttributeSet;
- import android.widget.EditText;
- import android.widget.LinearLayout;
- import android.widget.TextView;
- public class LabelEdit extends LinearLayout
- {
- private final String namespace = "http://schemas.android.com/apk/res/android";
- private TextView textView;
- private EditText editText;
- private String labelText;
- private int fontSize;
- private String isPassword;
- private String labelPosition;
-
- public LabelEdit(Context context,AttributeSet attrs)
- {
- super(context,attrs);
-
- int resourceId = attrs.getAttributeResourceValue(null, "labelText", 0);
- if(resourceId == 0)
- labelText = attrs.getAttributeValue(null,"labelText");
- else
- labelText = this.getResources().getString(resourceId);
-
- if(labelText == null)
- throw new RuntimeException("必須設定labelText屬性!");
-
- resourceId = attrs.getAttributeResourceValue(null, "fontSize", 0);
- if(resourceId == 0)
- fontSize = attrs.getAttributeIntValue(null,"fontSize",14);
- else
- fontSize = this.getResources().getInteger(resourceId);
-
- resourceId = attrs.getAttributeResourceValue(null, "labelPosition", 0);
- if(resourceId == 0)
- labelPosition = attrs.getAttributeValue(null,"labelPosition");
- else
- labelPosition = this.getResources().getString(resourceId);
-
- if(labelPosition == null)
- labelPosition = "left";
-
- resourceId = attrs.getAttributeResourceValue(namespace, "password", 0);
- if (resourceId == 0)
- isPassword = attrs.getAttributeValue(namespace, "password");
- else
- isPassword = getResources().getString(resourceId);
- if (isPassword == null)
- isPassword = "false";
-
- if(labelPosition.equals("top"))
- this.setOrientation(VERTICAL);
- else
- this.setOrientation(HORIZONTAL);
-
- textView = new TextView(context);
- textView.setTextSize(fontSize);
- textView.setText(labelText);
- this.addView(textView);
-
- editText = new EditText(context);
- editText.setWidth(50000);
- if(isPassword.equals("true"))
- editText.setTransformationMethod(PasswordTransformationMethod.getInstance());
- this.addView(editText);
- }
- }
複製代碼 |