Mah*_*ran 35 android custom-component attr
我有一个CompositeComponent(EditText + ImageButton)当点击按钮时,将清除edittext内容.它工作正常.我的问题是为我的组件设置属性.我使用declare-styleable来设置我的组件的属性.
我成功设置了minLines,maxLines和textColor.
如何通过xml将inputtype设置为我的组件.
我的attributes.xml
<?xml version="1.0" encoding="utf-8"?>
<resources>
<declare-styleable name="CET">
<attr name="MaxLines" format="integer"/>
<attr name="MinLines" format="integer"/>
<attr name="TextColor" format="color"/>
<attr name="InputType" format="integer" />
<attr name="Hint" format="string" />
</declare-styleable>
</resources>
Run Code Online (Sandbox Code Playgroud)
并在main_layout.xml中使用mycomponent:
<com.test.ui.ClearableEditText
xmlns:cet="http://schemas.android.com/apk/res/com.test.ui"
android:id="@+id/clearableEditText2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
cet:MaxLines="2"
cet:MinLines="1"
cet:TextColor="#0000FF"
cet:InputType="" <---I cant set this property--------->
cet:Hint="Clearable EditText Hint">
</com.test.ui.ClearableEditText>
Run Code Online (Sandbox Code Playgroud)
普通的Edittext用法:
<EditText
android:id="@+id/editText1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:inputType="numberSigned" <--------I want to use this property--------> >
Run Code Online (Sandbox Code Playgroud)
我不能在我的attribute.xml中使用ENUM.怎么引用android:inputType="numberSigned"我的 cet:InputType?
编辑:
这就是我在ClearableEditText.java中分配属性的方法
TypedArray a = getContext().obtainStyledAttributes(attrs,R.styleable.CET,0, 0);
int minLines = a.getInt(R.styleable.CET_MinLines, 1);
int maxLines = a.getInt(R.styleable.CET_MaxLines, 100);
String hint = a.getString(R.styleable.CET_Hint);
int textColor = a.getColor(R.styleable.CET_TextColor, Color.BLACK);
int inputType = a.getInt(R.styleable.CET_InputType, -108);
Log.i(TAG, "ClearableEditText: Min Line "+minLines +" Max Lines: "+maxLines+" Hint "+hint+" Color: "+textColor+" Input Type: "+inputType);
edit_text.setMaxLines(maxLines);
edit_text.setMinLines(minLines);
edit_text.setTextColor(textColor);
edit_text.setHint(hint);
if(inputType != -108)
edit_text.setInputType(inputType);
Run Code Online (Sandbox Code Playgroud)
您可以看到将inputType属性分配给editText没有问题.
Ale*_*xey 80
假设你有一个名为InputView的自定义视图,它不是TextView(比方说它是一个RelativeLayout).
在你的attrs.xml中:
<declare-styleable name="InputView">
<!-- any custom attributes -->
<attr name="title" format="string" />
<!-- standart attributes, note android: prefix and no format attribute -->
<attr name="android:imeOptions"/>
<attr name="android:inputType"/>
</declare-styleable>
Run Code Online (Sandbox Code Playgroud)
在要包含InputView的xml布局中:
<!-- note xmlns:custom and com.mycompany.myapp -->
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:custom="http://schemas.android.com/apk/res-auto"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<!-- note that you will be using android: prefix for standart attributes, and not custom: prefix -->
<!-- also note that you can use standart values: actionNext or textEmailAddress -->
<com.mycompany.myapp.InputView
android:id="@+id/emailField"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
custom:title="@string/my_title"
android:imeOptions="actionNext|flagNoExtractUi"
android:inputType="textEmailAddress" />
</FrameLayout>
Run Code Online (Sandbox Code Playgroud)
在自定义类中,您可以像往常一样提取属性:
...
private String title;
private int inputType;
private int imeOptions;
...
TypedArray a = getContext().obtainStyledAttributes(attrs, R.styleable.InputView);
int n = a.getIndexCount();
for (int i = 0; i < n; i++) {
int attr = a.getIndex(i);
switch (attr) {
case R.styleable.InputView_title:
title = a.getString(attr);
break;
//note that you are accessing standart attributes using your attrs identifier
case R.styleable.InputView_android_inputType:
inputType = a.getInt(attr, EditorInfo.TYPE_TEXT_VARIATION_NORMAL);
break;
case R.styleable.InputView_android_imeOptions:
imeOptions = a.getInt(attr, 0);
break;
default:
Log.d("TAG", "Unknown attribute for " + getClass().toString() + ": " + attr);
break;
}
}
a.recycle();
...
Run Code Online (Sandbox Code Playgroud)
在你的attr.xml中使用它来获取建议.在使用edittext时,使用flags会在xml中给出建议.自定义:inputType ="text",它会建议您添加类型.要添加更多标记,请使用此链接访问https://android.googlesource.com/platform/frameworks/base/+/refs/heads/master/core/res/res/values/attrs.xml
<resources>
<declare-styleable name="ClearableEditText">
<attr name="hintText" format="string" />
<attr name="inputType" format="integer">
<!-- There is no content type. The text is not editable. -->
<flag name="none" value="0x00000000" />
<!--
Just plain old text. Corresponds to
{@link android.text.InputType#TYPE_CLASS_TEXT} |
{@link android.text.InputType#TYPE_TEXT_VARIATION_NORMAL}.
-->
<flag name="text" value="0x00000001" />
<!--
Can be combined with <var>text</var> and its variations to
request capitalization of all characters. Corresponds to
{@link android.text.InputType#TYPE_TEXT_FLAG_CAP_CHARACTERS}.
-->
</attr>
</declare-styleable>
</resources>
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
30982 次 |
| 最近记录: |