开发者

How to set Custom EditTextField properties in android?

I am new to android .can any one solve the following problem? I just create the class like below .I need to know how to set property for the Edit text field

public class CustomEditText extends EditText{

public CustomEditText(Context context) {
    super(context);
    // TODO Auto-generated constructor st开发者_运维技巧ub
}

}

Note: I mean the property like this Edittext.setText("Demo"); Thanks in advance.



You need to create member variable and methods inside your CustomEditText.
Once you have them you can access it.


So there are a couple ways this can be interpreted and I will try to cover all of them.

EditText has multiple constructors. The one you have overridden requires that you as the developer set the properties in code for the rest of this instances usage. So you can actually just call setText(someString) from within this class or since the method is public call it directly on an instance of your class.

If you override the constructor that contains an attributeSet,

 EditText(Context, AttributeSet)

You can use your component as part of an xml layout and set attributes on it there as if it were another EditText (as long as you call super(context, attributeSet). If you want to define your own custom attributes on top of that then that's actually quite neat how you do this.

In your project hierarchy, from the root you should either have or need to create a folder called "res/values" Within that folder a file named attr.xml should be created.

<declare-styleable name="myCustomComponents">
<!-- Our Custom variable, optionally we can specify that it is of integer type -->
<attr name="myCustomAttribute" format="integer"/>
...
<!-- as many as you want -->
</declare-styleable>

Now within your new constructor that makes use of the AttributeSet, you can read this new attribute, "myCustomAttribute".

public CustomEditText(Context context, AttributeSet set) {
    super(context, set);
    // This parses the attributeSet and filters out to the stuff we care about
    TypedArray typedArray = context.obtainStyledAttributes(R.stylable.myCustomComponents);
    // Now we read let's say an int into our custom member variable.
    int attr = typedArray.getInt(R.styleable.myCustomComponents_myCustomAttribute, 0);
    // Make a public mutator so that others can set this attribute programatically
    setCustomAttribute(attr);
    // Remember to recycle the TypedArray (saved memory)
    typedArray.recycle();
}

Now that we have declared our new attribute and have setup code to read it, we can actually use it programatically or in an XML layout. So let's say you have an Xml layout in file, layout.xml

<ViewGroup 
   xmlns:android="http://schemas.android.com/apk/res/android"
   xmlns:custom="http://schemas.android.com/apk/res/com.my.apk.package"
>

...

<com.my.full.apk.package.CustomEditText
   android:id="@+id/custom_edit"
   ...
   custom:myCustomAttribute="12"
/>
...
</ViewGroup>

So in this we create a layout like normal, but notice we declare a new xml namespace. This is for your new component and your apk. So in this case "custom" is being used it will look inside your defined stylables for new parameters. By doing the previous steps with attr.xml, you have declared "myCustomAttribute" as a component attribute off of the http://schemas.android.com/apk/res/com.my.apk.package namespace. After that it's up to you to decide what attributes you would like to expose and what those attributes actually mean. Hope that helps.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜